summaryrefslogtreecommitdiff
path: root/scripts/coccinelle/misc
diff options
context:
space:
mode:
authorAndrew F. Davis <afd@ti.com>2016-10-17 11:52:24 -0500
committerMichal Marek <mmarek@suse.com>2016-12-11 12:07:00 +0100
commit75238b9e6aadca6d9255d4b385e026385e78bb15 (patch)
treeb70aba337eaa03d0c0258280c8e5ce37637cb06a /scripts/coccinelle/misc
parent1001354ca34179f3db924eb66672442a173147dc (diff)
Coccinelle: Add misc/boolconv.cocci
Add a script to check for unneeded conversions to bool. Signed-off-by: Andrew F. Davis <afd@ti.com> Acked-by: Julia Lawall <julia.lawall@lip6.fr> Signed-off-by: Michal Marek <mmarek@suse.com>
Diffstat (limited to 'scripts/coccinelle/misc')
-rw-r--r--scripts/coccinelle/misc/boolconv.cocci90
1 files changed, 90 insertions, 0 deletions
diff --git a/scripts/coccinelle/misc/boolconv.cocci b/scripts/coccinelle/misc/boolconv.cocci
new file mode 100644
index 000000000000..33c464d6bc71
--- /dev/null
+++ b/scripts/coccinelle/misc/boolconv.cocci
@@ -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 <afd@ti.com> GPLv2.
+
+virtual patch
+virtual context
+virtual org
+virtual report
+
+//----------------------------------------------------------
+// For patch mode
+//----------------------------------------------------------
+
+@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@p
+
+//----------------------------------------------------------
+// For org mode
+//----------------------------------------------------------
+
+@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)