forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
coccicheck: Add unneeded return variable test
This semantic patch looks for variables that are initialized with a constant, are never updated, and are only used as parameter of return. Return the constant instead of using a variable. Signed-off-by: Peter Senna Tschudin <[email protected]> Signed-off-by: Michal Marek <[email protected]>
- Loading branch information
1 parent
4be7f0a
commit 90829ad
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/// | ||
/// Removes unneeded variable used to store return value. | ||
/// | ||
// Confidence: Moderate | ||
// Copyright: (C) 2012 Peter Senna Tschudin, INRIA/LIP6. GPLv2. | ||
// URL: http://coccinelle.lip6.fr/ | ||
// Comments: Comments on code can be deleted if near code that is removed. | ||
// "when strict" can be removed to get more hits, but adds false | ||
// positives | ||
// Options: --no-includes --include-headers | ||
|
||
virtual patch | ||
virtual report | ||
virtual context | ||
virtual org | ||
|
||
@depends on patch@ | ||
type T; | ||
constant C; | ||
identifier ret; | ||
@@ | ||
- T ret = C; | ||
... when != ret | ||
when strict | ||
return | ||
- ret | ||
+ C | ||
; | ||
|
||
@depends on context@ | ||
type T; | ||
constant C; | ||
identifier ret; | ||
@@ | ||
* T ret = C; | ||
... when != ret | ||
when strict | ||
* return ret; | ||
|
||
@r1 depends on report || org@ | ||
type T; | ||
constant C; | ||
identifier ret; | ||
position p1, p2; | ||
@@ | ||
T ret@p1 = C; | ||
... when != ret | ||
when strict | ||
return ret@p2; | ||
|
||
@script:python depends on report@ | ||
p1 << r1.p1; | ||
p2 << r1.p2; | ||
C << r1.C; | ||
ret << r1.ret; | ||
@@ | ||
coccilib.report.print_report(p1[0], "Unneeded variable: \"" + ret + "\". Return \"" + C + "\" on line " + p2[0].line) | ||
@script:python depends on org@ | ||
p1 << r1.p1; | ||
p2 << r1.p2; | ||
C << r1.C; | ||
ret << r1.ret; | ||
@@ | ||
cocci.print_main("unneeded \"" + ret + "\" variable", p1) | ||
cocci.print_sec("return " + C + " here", p2) |