xref: /openbmc/linux/scripts/coccinelle/misc/cond_no_effect.cocci (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*7f904d7eSThomas Gleixner// SPDX-License-Identifier: GPL-2.0-only
2c8990359SNicholas Mc Guire///Find conditions where if and else branch are functionally
3c8990359SNicholas Mc Guire// identical.
4c8990359SNicholas Mc Guire//
5c8990359SNicholas Mc Guire// There can be false positives in cases where the positional
6c8990359SNicholas Mc Guire// information is used (as with lockdep) or where the identity
7c8990359SNicholas Mc Guire// is a placeholder for not yet handled cases.
8c8990359SNicholas Mc Guire// Unfortunately there also seems to be a tendency to use
9c8990359SNicholas Mc Guire// the last if else/else as a "default behavior" - which some
10c8990359SNicholas Mc Guire// might consider a legitimate coding pattern. From discussion
11c8990359SNicholas Mc Guire// on kernelnewbies though it seems that this is not really an
12c8990359SNicholas Mc Guire// accepted pattern and if at all it would need to be commented
13c8990359SNicholas Mc Guire//
14c8990359SNicholas Mc Guire// In the Linux kernel it does not seem to actually report
15c8990359SNicholas Mc Guire// false positives except for those that were documented as
16c8990359SNicholas Mc Guire// being intentional.
17c8990359SNicholas Mc Guire// the two known cases are:
18c8990359SNicholas Mc Guire//   arch/sh/kernel/traps_64.c:read_opcode()
19c8990359SNicholas Mc Guire//        } else if ((pc & 1) == 0) {
20c8990359SNicholas Mc Guire//              /* SHcompact */
21c8990359SNicholas Mc Guire//              /* TODO : provide handling for this.  We don't really support
22c8990359SNicholas Mc Guire//                 user-mode SHcompact yet, and for a kernel fault, this would
23c8990359SNicholas Mc Guire//                 have to come from a module built for SHcompact.  */
24c8990359SNicholas Mc Guire//              return -EFAULT;
25c8990359SNicholas Mc Guire//      } else {
26c8990359SNicholas Mc Guire//              /* misaligned */
27c8990359SNicholas Mc Guire//              return -EFAULT;
28c8990359SNicholas Mc Guire//      }
29c8990359SNicholas Mc Guire//   fs/kernfs/file.c:kernfs_fop_open()
30c8990359SNicholas Mc Guire//       * Both paths of the branch look the same.  They're supposed to
31c8990359SNicholas Mc Guire//       * look that way and give @of->mutex different static lockdep keys.
32c8990359SNicholas Mc Guire//       */
33c8990359SNicholas Mc Guire//      if (has_mmap)
34c8990359SNicholas Mc Guire//              mutex_init(&of->mutex);
35c8990359SNicholas Mc Guire//      else
36c8990359SNicholas Mc Guire//              mutex_init(&of->mutex);
37c8990359SNicholas Mc Guire//
38c8990359SNicholas Mc Guire// All other cases look like bugs or at least lack of documentation
39c8990359SNicholas Mc Guire//
40c8990359SNicholas Mc Guire// Confidence: Moderate
41*7f904d7eSThomas Gleixner// Copyright: (C) 2016 Nicholas Mc Guire, OSADL.
42c8990359SNicholas Mc Guire// Comments:
43c8990359SNicholas Mc Guire// Options: --no-includes --include-headers
44c8990359SNicholas Mc Guire
45c8990359SNicholas Mc Guirevirtual org
46c8990359SNicholas Mc Guirevirtual report
47c8990359SNicholas Mc Guire
48c8990359SNicholas Mc Guire@cond@
49c8990359SNicholas Mc Guirestatement S1;
50c8990359SNicholas Mc Guireposition p;
51c8990359SNicholas Mc Guire@@
52c8990359SNicholas Mc Guire
53c8990359SNicholas Mc Guire* if@p (...) S1 else S1
54c8990359SNicholas Mc Guire
55c8990359SNicholas Mc Guire@script:python depends on org@
56c8990359SNicholas Mc Guirep << cond.p;
57c8990359SNicholas Mc Guire@@
58c8990359SNicholas Mc Guire
59c8990359SNicholas Mc Guirecocci.print_main("WARNING: possible condition with no effect (if == else)",p)
60c8990359SNicholas Mc Guire
61c8990359SNicholas Mc Guire@script:python depends on report@
62c8990359SNicholas Mc Guirep << cond.p;
63c8990359SNicholas Mc Guire@@
64c8990359SNicholas Mc Guire
65c8990359SNicholas Mc Guirecoccilib.report.print_report(p[0],"WARNING: possible condition with no effect (if == else)")
66