diff options
| author | Joe Perches <joe@perches.com> | 2015-09-09 18:37:58 -0400 |
|---|---|---|
| committer | Linus Torvalds <torvalds@linux-foundation.org> | 2015-09-10 16:29:01 -0400 |
| commit | c5595fa2f1ce3c1a74dbd2ddc949257e80d81dc0 (patch) | |
| tree | 3351ed229a4f64df859ad49de118210dd50fa367 /scripts | |
| parent | 54507b5183cc4f8e4f1a58a312e1f30c130658b7 (diff) | |
checkpatch: add constant comparison on left side test
"CONST <comparison> variable" checks like:
if (NULL != foo)
and
while (0 < bar(...))
where a constant (or what appears to be a constant like an upper case
identifier) is on the left of a comparison are generally preferred to be
written using the constant on the right side like:
if (foo != NULL)
and
while (bar(...) > 0)
Add a test for this.
Add a --fix option too, but only do it when the code is immediately
surrounded by parentheses to avoid misfixing things like "(0 < bar() +
constant)"
Signed-off-by: Joe Perches <joe@perches.com>
Cc: Nicolas Morey Chaisemartin <nmorey@kalray.eu>
Cc: Viresh Kumar <viresh.kumar@linaro.org>
Cc: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/checkpatch.pl | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 97ee377c4f51..f2a1131b2f8b 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
| @@ -4231,6 +4231,35 @@ sub process { | |||
| 4231 | } | 4231 | } |
| 4232 | } | 4232 | } |
| 4233 | 4233 | ||
| 4234 | # comparisons with a constant or upper case identifier on the left | ||
| 4235 | # avoid cases like "foo + BAR < baz" | ||
| 4236 | # only fix matches surrounded by parentheses to avoid incorrect | ||
| 4237 | # conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" | ||
| 4238 | if ($^V && $^V ge 5.10.0 && | ||
| 4239 | $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { | ||
| 4240 | my $lead = $1; | ||
| 4241 | my $const = $2; | ||
| 4242 | my $comp = $3; | ||
| 4243 | my $to = $4; | ||
| 4244 | my $newcomp = $comp; | ||
| 4245 | if ($lead !~ /$Operators\s*$/ && | ||
| 4246 | $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && | ||
| 4247 | WARN("CONSTANT_COMPARISON", | ||
| 4248 | "Comparisons should place the constant on the right side of the test\n" . $herecurr) && | ||
| 4249 | $fix) { | ||
| 4250 | if ($comp eq "<") { | ||
| 4251 | $newcomp = ">"; | ||
| 4252 | } elsif ($comp eq "<=") { | ||
| 4253 | $newcomp = ">="; | ||
| 4254 | } elsif ($comp eq ">") { | ||
| 4255 | $newcomp = "<"; | ||
| 4256 | } elsif ($comp eq ">=") { | ||
| 4257 | $newcomp = "<="; | ||
| 4258 | } | ||
| 4259 | $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; | ||
| 4260 | } | ||
| 4261 | } | ||
| 4262 | |||
| 4234 | # Return of what appears to be an errno should normally be negative | 4263 | # Return of what appears to be an errno should normally be negative |
| 4235 | if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { | 4264 | if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { |
| 4236 | my $name = $1; | 4265 | my $name = $1; |
