public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Josh Triplett <josh@joshtriplett.org>
To: Julia Lawall <Julia.Lawall@lip6.fr>,
	Gilles Muller <Gilles.Muller@lip6.fr>,
	Nicolas Palix <nicolas.palix@imag.fr>,
	Michal Marek <mmarek@suse.cz>,
	linux-kernel@vger.kernel.org, cocci@systeme.lip6.fr
Subject: [PATCH] coccinelle: Add a script to find and remove unnecessary ifs with no body
Date: Sat, 2 Nov 2013 07:32:37 -0700	[thread overview]
Message-ID: <20131102143236.GA18084@leaf> (raw)

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.

Signed-off-by: Josh Triplett <josh@joshtriplett.org>
---
 scripts/coccinelle/misc/unnecessary-if.cocci | 40 ++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 scripts/coccinelle/misc/unnecessary-if.cocci

diff --git a/scripts/coccinelle/misc/unnecessary-if.cocci b/scripts/coccinelle/misc/unnecessary-if.cocci
new file mode 100644
index 0000000..7ed721f
--- /dev/null
+++ b/scripts/coccinelle/misc/unnecessary-if.cocci
@@ -0,0 +1,40 @@
+/// Remove unnecessary ifs with no statements.
+//
+// Confidence: High
+// Options: --no-includes --include-headers
+
+virtual patch
+virtual report
+virtual context
+
+@r1 depends on patch@
+expression E, E2, f;
+@@
+(
+- if (
+      <+... \(f(...)\|++E\|--E\|E++\|E--\|E=E2\) ...+>
+- ) {}
++ ;
+|
+- if (E) {}
+)
+
+@r2 depends on report || context@
+expression E, E2, f;
+position p;
+@@
+(
+- if@p (
+      <+... \(f(...)\|++E\|--E\|E++\|E--\|E=E2\) ...+>
+- ) {}
++ ;
+|
+- if@p (E) {}
+)
+
+@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


             reply	other threads:[~2013-11-02 14:32 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-02 14:32 Josh Triplett [this message]
2013-11-02 14:51 ` [PATCH] coccinelle: Add a script to find and remove unnecessary ifs with no body Julia Lawall
2013-11-02 15:05   ` Josh Triplett
2013-11-02 15:09     ` Julia Lawall
2013-11-02 15:15   ` [PATCHv2] coccinelle: Add a script to find " Josh Triplett
2013-11-02 16:49     ` Julia Lawall
2013-11-08 13:52       ` Michal Marek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20131102143236.GA18084@leaf \
    --to=josh@joshtriplett.org \
    --cc=Gilles.Muller@lip6.fr \
    --cc=Julia.Lawall@lip6.fr \
    --cc=cocci@systeme.lip6.fr \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mmarek@suse.cz \
    --cc=nicolas.palix@imag.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox