coccinelle: Add a script to find and remove unnecessary ifs with no body
PATCH 1/1 coccinelle: Add a script to find and remove unnecessary ifs with no body
This script matches code like:
if (foo) {}
and either eliminates it (if foo has no side effects) or replaces it with foo (if foo has side effects).
Works perfectly in report mode; the results of patch mode are semantically correct but may still benefit from further simplification.
--- scripts/coccinelle/misc/unnecessary-if.cocci | 40 ++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 scripts/coccinelle/misc/unnecessary-if.cocci
scripts/coccinelle/misc/unnecessary-if.cocci new file +40 -0
@@ -0,0 +1,40 @@ +/// Remove unnecessary ifs with no statements. +// +// Confidence: High +// Options: --no-includes --include-headers + +virtual patch +virtual report +virtual context + + at r1 depends on patch@ +expression E, E2, f; +@@ +( +- if ( + <+... \(f(...)\|++E\|--E\|E++\|E--\|E=E2\) ...+> +- ) {} ++ ; +| +- if (E) {} +) + + at r2 depends on report || context@ +expression E, E2, f; +position p; +@@ +( +- if at p ( + <+... \(f(...)\|++E\|--E\|E++\|E--\|E=E2\) ...+> +- ) {} ++ ; +| +- if at p (E) {} +) + + at script:python depends on report@ +p << r2.p; +@@ + +msg = "WARNING: Unnecessary if with no body." +coccilib.report.print_report(p[0], msg)
Signature · 1 line
1.8.4.2
Quoted 9 lines · Josh Triplett
This script matches code like:
if (foo) {}
and either eliminates it (if foo has no side effects) or replaces it with foo (if foo has side effects).
Works perfectly in report mode; the results of patch mode are semantically correct but may still benefit from further simplification.
Actually, I would get rid of the patch mode. Looking through some of the results, I don't see any where just deleting the code is the right thing to do. They all look like cases where the developer wanted to remember to think more about the problem.
julia
Quoted 57 lines
Signed-off-by Josh Triplett <[email protected]>--- scripts/coccinelle/misc/unnecessary-if.cocci | 40 ++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 scripts/coccinelle/misc/unnecessary-if.cocciscripts/coccinelle/misc/unnecessary-if.cocci new file +40
-1@@ -0,0 +1,40 @@ +/// Remove unnecessary ifs with no statements. +// +// Confidence: High +// Options: --no-includes --include-headers + +virtual patch +virtual report +virtual context + + at r1 depends on patch@ +expression E, E2, f; +@@ +( +- if ( + <+... \(f(...)\|++E\|--E\|E++\|E--\|E=E2\) ...+> +- ) {} ++ ; +| +- if (E) {} +) + + at r2 depends on report || context@ +expression E, E2, f; +position p; +@@ +( +- if at p ( + <+... \(f(...)\|++E\|--E\|E++\|E--\|E=E2\) ...+> +- ) {} ++ ; +| +- if at p (E) {} +) + + at script:python depends on report@ +p << r2.p; +@@ + +msg = "WARNING: Unnecessary if with no body." +coccilib.report.print_report(p[0], msg) --1.8.4.2
Quoted 9 lines · Julia Lawall
Quoted 7 lines · Josh Triplett
This script matches code like:
if (foo) {}
and either eliminates it (if foo has no side effects) or replaces it with foo (if foo has side effects).
Quoted 7 lines
Quoted 2 lines
Works perfectly in report mode; the results of patch mode are semantically correct but may still benefit from further simplification.
Actually, I would get rid of the patch mode. Looking through some of the results, I don't see any where just deleting the code is the right thing to do. They all look like cases where the developer wanted to remember to think more about the problem.
I observed several where the patch seemed entirely correct, with no FIXME/TODO/XXX comments associated with the conditional.
Nonetheless, I don't mind removing the patch mode if you don't think it's worthwhile. I'll send a v2.
- Josh Triplett
Quoted 5 lines
I observed several where the patch seemed entirely correct, with no FIXME/TODO/XXX comments associated with the conditional.
Nonetheless, I don't mind removing the patch mode if you don't think it's worthwhile. I'll send a v2.
I think it would be better without it.
thanks,
julia
PATCH v2 1/1 coccinelle: Add a script to find unnecessary ifs with no body
In reply to Julia LawallThis script finds code like this, with an unnecessary if:
if (foo) {}
Provides report mode only, since patching often eliminates conditionals that contain TODO comments or similar.
---
v2: Drop patch mode.
scripts/coccinelle/misc/unnecessary-if.cocci | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 scripts/coccinelle/misc/unnecessary-if.cocci
scripts/coccinelle/misc/unnecessary-if.cocci new file +23 -0
@@ -0,0 +1,23 @@ +/// Find unnecessary ifs with no statements. +// +// Report mode only, since patch mode often deletes ifs that contain TODO +// comments. +// +// Confidence: High +// Options: --no-includes --include-headers + +virtual report +virtual context + + at r depends on report || context@ +expression E; +position p; +@@ +if at p (E) {} + + at script:python depends on report@ +p << r.p; +@@ + +msg = "WARNING: Unnecessary if with no body." +coccilib.report.print_report(p[0], msg)
Signature · 1 line
1.8.4.2
Re: [PATCHv2] coccinelle: Add a script to find unnecessary ifs with no body
scripts/coccinelle/misc/unnecessary-if.cocci line 16
Quoted 5 lines
+ at r depends on report || context@
scripts/coccinelle/misc/unnecessary-if.cocci new file +4
-0+expression E; +position p; +@@ +if at p (E) {}
There should be a * in front of the if, to support context mode.
julia
Re: [PATCHv2] coccinelle: Add a script to find unnecessary ifs with no body
Quoted 7 lines · Julia Lawall
scripts/coccinelle/misc/unnecessary-if.cocci line 16
Quoted 5 lines
+ at r depends on report || context@
scripts/coccinelle/misc/unnecessary-if.cocci new file +4
-0+expression E; +position p; +@@ +if at p (E) {}There should be a * in front of the if, to support context mode.
Josh, will you send a v3 with the above fix?
Thanks,
Michal