[<prev] [next>] [day] [month] [year] [list]
Message-Id: <20220322075915.2338847-1-chi.minghao@zte.com.cn>
Date: Tue, 22 Mar 2022 07:59:15 +0000
From: cgel.zte@...il.com
To: Julia.Lawall@...ia.fr
Cc: nicolas.palix@...g.fr, linux-kernel@...r.kernel.org,
cocci@...ia.fr, Minghao Chi <chi.minghao@....com.cn>,
Zeal Robot <zealci@....com.cn>
Subject: [PATCH] coccicheck: Add redundant return variable test
From: Minghao Chi <chi.minghao@....com.cn>
This semantic patch looks for variables that are only used
as parameter returned by the function.
Reported-by: Zeal Robot <zealci@....com.cn>
Signed-off-by: Minghao Chi <chi.minghao@....com.cn>
---
scripts/coccinelle/misc/redundant.cocci | 57 +++++++++++++++++++++++++
1 file changed, 57 insertions(+)
create mode 100644 scripts/coccinelle/misc/redundant.cocci
diff --git a/scripts/coccinelle/misc/redundant.cocci b/scripts/coccinelle/misc/redundant.cocci
new file mode 100644
index 000000000000..57a4546783cb
--- /dev/null
+++ b/scripts/coccinelle/misc/redundant.cocci
@@ -0,0 +1,57 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Remove redundant variables and return the value directly.
+///
+// Confidence: Moderate
+// Copyright: (C) 2022 Minghao Chi, ZTE.
+// URL: http://coccinelle.lip6.fr/
+// Comments:
+// Options: --no-includes --include-headers
+
+virtual report
+virtual org
+
+@...like@
+position p;
+identifier var;
+expression E;
+@@
+
+if(...)
+{
+ var = E;@p
+ return var;
+}
+
+@...e@
+identifier var, stru;
+expression E;
+position p!=dislike.p, p2;
+Type T;
+constant C;
+@@
+
+...when != T* var
+ when != struct stru* var;
+ when != var=C
+(
+ var = E;@p
+ return var;@p2
+)
+
+@...ipt:python depends on report@
+p << hope.p;
+p2 << hope.p2;
+var << hope.var;
+@@
+
+coccilib.report.print_report(p[0], "Redundant variable: \"" + var + "\". Return \"" + var + "\" on line " + p2[0].line)
+
+@...ipt:python depends on org@
+p << hope.p;
+p2 << hope.p2;
+var << hope.var;
+@@
+
+cocci.print_main("redundant \"" + var + "\" variable", p)
+cocci.print_sec("return " + var + " here ", p2)
--
2.25.1
Powered by blists - more mailing lists