diff options
author | Nicholas Mc Guire <hofrat@osadl.org> | 2016-10-08 11:51:45 -0400 |
---|---|---|
committer | Michal Marek <mmarek@suse.com> | 2016-10-11 03:57:17 -0400 |
commit | c8990359d4b12f14656386526ddf904635076902 (patch) | |
tree | 40be3d2b71d8e7b2156e7f8249f3a4104680b259 | |
parent | 1e01892e7ad521a96760da0d791b42badf755d3f (diff) |
Coccinelle: flag conditions with no effect
Report code constructs where the if and else branch are functionally
identical. In cases where this is intended it really should be
documented - most reported cases probably are bugs.
Signed-off-by: Nicholas Mc Guire <hofrat@osadl.org>
Signed-off-by: Michal Marek <mmarek@suse.com>
-rw-r--r-- | scripts/coccinelle/misc/cond_no_effect.cocci | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/scripts/coccinelle/misc/cond_no_effect.cocci b/scripts/coccinelle/misc/cond_no_effect.cocci new file mode 100644 index 000000000000..8467dbd1c465 --- /dev/null +++ b/scripts/coccinelle/misc/cond_no_effect.cocci | |||
@@ -0,0 +1,64 @@ | |||
1 | ///Find conditions where if and else branch are functionally | ||
2 | // identical. | ||
3 | // | ||
4 | // There can be false positives in cases where the positional | ||
5 | // information is used (as with lockdep) or where the identity | ||
6 | // is a placeholder for not yet handled cases. | ||
7 | // Unfortunately there also seems to be a tendency to use | ||
8 | // the last if else/else as a "default behavior" - which some | ||
9 | // might consider a legitimate coding pattern. From discussion | ||
10 | // on kernelnewbies though it seems that this is not really an | ||
11 | // accepted pattern and if at all it would need to be commented | ||
12 | // | ||
13 | // In the Linux kernel it does not seem to actually report | ||
14 | // false positives except for those that were documented as | ||
15 | // being intentional. | ||
16 | // the two known cases are: | ||
17 | // arch/sh/kernel/traps_64.c:read_opcode() | ||
18 | // } else if ((pc & 1) == 0) { | ||
19 | // /* SHcompact */ | ||
20 | // /* TODO : provide handling for this. We don't really support | ||
21 | // user-mode SHcompact yet, and for a kernel fault, this would | ||
22 | // have to come from a module built for SHcompact. */ | ||
23 | // return -EFAULT; | ||
24 | // } else { | ||
25 | // /* misaligned */ | ||
26 | // return -EFAULT; | ||
27 | // } | ||
28 | // fs/kernfs/file.c:kernfs_fop_open() | ||
29 | // * Both paths of the branch look the same. They're supposed to | ||
30 | // * look that way and give @of->mutex different static lockdep keys. | ||
31 | // */ | ||
32 | // if (has_mmap) | ||
33 | // mutex_init(&of->mutex); | ||
34 | // else | ||
35 | // mutex_init(&of->mutex); | ||
36 | // | ||
37 | // All other cases look like bugs or at least lack of documentation | ||
38 | // | ||
39 | // Confidence: Moderate | ||
40 | // Copyright: (C) 2016 Nicholas Mc Guire, OSADL. GPLv2. | ||
41 | // Comments: | ||
42 | // Options: --no-includes --include-headers | ||
43 | |||
44 | virtual org | ||
45 | virtual report | ||
46 | |||
47 | @cond@ | ||
48 | statement S1; | ||
49 | position p; | ||
50 | @@ | ||
51 | |||
52 | * if@p (...) S1 else S1 | ||
53 | |||
54 | @script:python depends on org@ | ||
55 | p << cond.p; | ||
56 | @@ | ||
57 | |||
58 | cocci.print_main("WARNING: possible condition with no effect (if == else)",p) | ||
59 | |||
60 | @script:python depends on report@ | ||
61 | p << cond.p; | ||
62 | @@ | ||
63 | |||
64 | coccilib.report.print_report(p[0],"WARNING: possible condition with no effect (if == else)") | ||