Coccinelle: Add misc/boolconv.cocci
PATCH 1/1 Coccinelle: Add misc/boolconv.cocci
Add a script to check for unneeded conversions to bool.
Signed-off-by Andrew F. Davis <[email protected]>
--- scripts/coccinelle/misc/boolconv.cocci | 90 ++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 scripts/coccinelle/misc/boolconv.cocci
scripts/coccinelle/misc/boolconv.cocci new file +90 -0
@@ -0,0 +1,90 @@ +/// Remove unneeded conversion to bool +/// +//# Relational and logical operators evaluate to bool, +//# explicit conversion is overly verbose and unneeded. +// +// Copyright: (C) 2016 Andrew F. Davis <[email protected]> GPLv2. + +virtual patch +virtual context +virtual org +virtual report + +//---------------------------------------------------------- +// For patch mode +//---------------------------------------------------------- + + at depends on patch@ +expression A, B; +symbol true, false; +@@ + +( + A == B +| + A != B +| + A > B +| + A < B +| + A >= B +| + A <= B +| + A && B +| + A || B +) +- ? true : false + +//---------------------------------------------------------- +// For context mode +//---------------------------------------------------------- + +@r depends on !patch@ +expression A, B; +symbol true, false; +position p; +@@ + +( + A == B +| + A != B +| + A > B +| + A < B +| + A >= B +| + A <= B +| + A && B +| + A || B +) +* ? true : false at p + +//---------------------------------------------------------- +// For org mode +//---------------------------------------------------------- + + at script:python depends on r&&org@ +p << r.p; +@@ + +msg = "WARNING: conversion to bool not needed here" +coccilib.org.print_todo(p[0], msg) + +//---------------------------------------------------------- +// For report mode +//---------------------------------------------------------- + +@script:python depends on r&&report@ +p << r.p; +@@ + +msg = "WARNING: conversion to bool not needed here" +coccilib.report.print_report(p[0], msg)
Signature · 1 line
2.9.0
Quoted 1 line · Andrew F. Davis
Add a script to check for unneeded conversions to bool.
this could also be extended for bool functions that:
return !!expression; and return expression != 0;
Quoted 9 lines · Joe Perches
Quoted 1 line · Andrew F. Davis
Add a script to check for unneeded conversions to bool.
this could also be extended for bool functions that:
return !!expression; and return expression != 0;
Agreed, it's in the works, I was going to post this current set with some patches to the kernel that is detects, then add additional checks and their kernel fixes incrementally.
For the second suggestion I think we could have additions to boolinit.cocci that include the results of comparison/logical operations.
Thanks,
Andrew
Quoted 3 lines · Andrew F. Davis
Add a script to check for unneeded conversions to bool.
Signed-off-by Andrew F. Davis <[email protected]>
Acked-by Julia Lawall <[email protected]>
Quoted 105 lines
--- scripts/coccinelle/misc/boolconv.cocci | 90 ++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 scripts/coccinelle/misc/boolconv.cocciscripts/coccinelle/misc/boolconv.cocci new file +90
-0@@ -0,0 +1,90 @@ +/// Remove unneeded conversion to bool +/// +//# Relational and logical operators evaluate to bool, +//# explicit conversion is overly verbose and unneeded. +// +// Copyright: (C) 2016 Andrew F. Davis <[email protected]> GPLv2. + +virtual patch +virtual context +virtual org +virtual report + +//---------------------------------------------------------- +// For patch mode +//---------------------------------------------------------- + + at depends on patch@ +expression A, B; +symbol true, false; +@@ + +( + A == B +| + A != B +| + A > B +| + A < B +| + A >= B +| + A <= B +| + A && B +| + A || B +) +- ? true : false + +//---------------------------------------------------------- +// For context mode +//---------------------------------------------------------- + + at r depends on !patch@ +expression A, B; +symbol true, false; +position p; +@@ + +( + A == B +| + A != B +| + A > B +| + A < B +| + A >= B +| + A <= B +| + A && B +| + A || B +) +* ? true : false at p + +//---------------------------------------------------------- +// For org mode +//---------------------------------------------------------- + + at script:python depends on r&&org@ +p << r.p; +@@ + +msg = "WARNING: conversion to bool not needed here" +coccilib.org.print_todo(p[0], msg) + +//---------------------------------------------------------- +// For report mode +//---------------------------------------------------------- + + at script:python depends on r&&report@ +p << r.p; +@@ + +msg = "WARNING: conversion to bool not needed here" +coccilib.report.print_report(p[0], msg)Signature · 1 line
2.9.0