* [PATCH] coccinelle: add style check for assignment in if
@ 2015-08-15 17:18 Kris Borer
2015-08-15 17:30 ` Julia Lawall
0 siblings, 1 reply; 4+ messages in thread
From: Kris Borer @ 2015-08-15 17:18 UTC (permalink / raw)
To: Julia.Lawall
Cc: Gilles.Muller, nicolas.palix, mmarek, linux-kernel, cocci, Kris Borer
Add a semantic patch for fixing some cases of checkpatch.pl error:
ERROR: do not use assignment in if condition
Reviewed-by: Julia Lawall <julia.lawall@lip6.fr>
Signed-off-by: Kris Borer <kborer@gmail.com>
---
scripts/coccinelle/style/assignment_in_if.cocci | 92 +++++++++++++++++++++++++
1 file changed, 92 insertions(+)
create mode 100644 scripts/coccinelle/style/assignment_in_if.cocci
diff --git a/scripts/coccinelle/style/assignment_in_if.cocci b/scripts/coccinelle/style/assignment_in_if.cocci
new file mode 100644
index 0000000..22ab161
--- /dev/null
+++ b/scripts/coccinelle/style/assignment_in_if.cocci
@@ -0,0 +1,92 @@
+// find checkpatch.pl errors of the type:
+// ERROR: do not use assignment in if condition
+//
+// This script is designed to correct code where assignments exist in if
+// conditions. It is only capable of handling a subset of such problems.
+//
+// For example:
+//
+// if(result = myfun())
+//
+// would become:
+//
+// result = myfun();
+// if(result)
+//
+// Confidence: Moderate
+
+
+// if ( ret = call() )
+@if1@
+identifier i;
+expression E;
+statement S1, S2;
+@@
+
++ i = E;
+ if (
+- (i = E)
++ i
+ ) S1 else S2
+
+
+// if ( (ret = call()) < 0 )
+@if2@
+identifier i;
+expression E;
+statement S1, S2;
+binary operator b;
+@@
+
++ i = E;
+ if (
+- (i = E)
++ i
+ b ... ) S1 else S2
+
+// if ( ptr->fun && (ret = ptr->fun()) < 0 )
+@if3@
+identifier i, i2;
+expression E1, E2;
+constant c;
+binary operator b;
+@@
+
++ if( E1->i ) {
++ i2 = E2;
++ if (i2 b c) {
+- if( E1->i && ((i2 = E2) b c) ) {
+ ...
+- }
++ }
++ }
+
+// if ( (ret = call()) < 0 && ret != -1 )
+@if4@
+identifier i;
+expression E, E2;
+statement S1, S2;
+binary operator b;
+@@
+
++ i = E;
+ if (
+- (i = E)
++ i
+ b
+ ... && E2 ) S1 else S2
+
+// if ( (ret = call()) < 0 && ret != -1 && ret != -2 )
+@if5@
+identifier i;
+expression E, E2, E3;
+statement S1, S2;
+binary operator b;
+@@
+
++ i = E;
+ if (
+- (i = E)
++ i
+ b
+ ... && E2 && E3 ) S1 else S2
--
1.9.1
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] coccinelle: add style check for assignment in if
2015-08-15 17:18 [PATCH] coccinelle: add style check for assignment in if Kris Borer
@ 2015-08-15 17:30 ` Julia Lawall
2015-08-15 17:33 ` Kris Borer
0 siblings, 1 reply; 4+ messages in thread
From: Julia Lawall @ 2015-08-15 17:30 UTC (permalink / raw)
To: Kris Borer; +Cc: Gilles Muller, nicolas.palix, mmarek, linux-kernel, cocci
On Sat, 15 Aug 2015, Kris Borer wrote:
> Add a semantic patch for fixing some cases of checkpatch.pl error:
>
> ERROR: do not use assignment in if condition
>
> Reviewed-by: Julia Lawall <julia.lawall@lip6.fr>
Sorry, but I'm not done reviewing it. I have a lot of changes to propose.
Is this different than the RFC v2?
julia
> Signed-off-by: Kris Borer <kborer@gmail.com>
> ---
> scripts/coccinelle/style/assignment_in_if.cocci | 92 +++++++++++++++++++++++++
> 1 file changed, 92 insertions(+)
> create mode 100644 scripts/coccinelle/style/assignment_in_if.cocci
>
> diff --git a/scripts/coccinelle/style/assignment_in_if.cocci b/scripts/coccinelle/style/assignment_in_if.cocci
> new file mode 100644
> index 0000000..22ab161
> --- /dev/null
> +++ b/scripts/coccinelle/style/assignment_in_if.cocci
> @@ -0,0 +1,92 @@
> +// find checkpatch.pl errors of the type:
> +// ERROR: do not use assignment in if condition
> +//
> +// This script is designed to correct code where assignments exist in if
> +// conditions. It is only capable of handling a subset of such problems.
> +//
> +// For example:
> +//
> +// if(result = myfun())
> +//
> +// would become:
> +//
> +// result = myfun();
> +// if(result)
> +//
> +// Confidence: Moderate
> +
> +
> +// if ( ret = call() )
> +@if1@
> +identifier i;
> +expression E;
> +statement S1, S2;
> +@@
> +
> ++ i = E;
> + if (
> +- (i = E)
> ++ i
> + ) S1 else S2
> +
> +
> +// if ( (ret = call()) < 0 )
> +@if2@
> +identifier i;
> +expression E;
> +statement S1, S2;
> +binary operator b;
> +@@
> +
> ++ i = E;
> + if (
> +- (i = E)
> ++ i
> + b ... ) S1 else S2
> +
> +// if ( ptr->fun && (ret = ptr->fun()) < 0 )
> +@if3@
> +identifier i, i2;
> +expression E1, E2;
> +constant c;
> +binary operator b;
> +@@
> +
> ++ if( E1->i ) {
> ++ i2 = E2;
> ++ if (i2 b c) {
> +- if( E1->i && ((i2 = E2) b c) ) {
> + ...
> +- }
> ++ }
> ++ }
> +
> +// if ( (ret = call()) < 0 && ret != -1 )
> +@if4@
> +identifier i;
> +expression E, E2;
> +statement S1, S2;
> +binary operator b;
> +@@
> +
> ++ i = E;
> + if (
> +- (i = E)
> ++ i
> + b
> + ... && E2 ) S1 else S2
> +
> +// if ( (ret = call()) < 0 && ret != -1 && ret != -2 )
> +@if5@
> +identifier i;
> +expression E, E2, E3;
> +statement S1, S2;
> +binary operator b;
> +@@
> +
> ++ i = E;
> + if (
> +- (i = E)
> ++ i
> + b
> + ... && E2 && E3 ) S1 else S2
> --
> 1.9.1
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] coccinelle: add style check for assignment in if
2015-08-15 17:30 ` Julia Lawall
@ 2015-08-15 17:33 ` Kris Borer
2015-08-15 17:46 ` Julia Lawall
0 siblings, 1 reply; 4+ messages in thread
From: Kris Borer @ 2015-08-15 17:33 UTC (permalink / raw)
To: Julia Lawall
Cc: Gilles Muller, nicolas.palix, Michal Marek, linux-kernel, cocci
I apologize, I misunderstood your email. It is the same as RFC v2.
On Sat, Aug 15, 2015 at 1:30 PM, Julia Lawall <julia.lawall@lip6.fr> wrote:
>
>
> On Sat, 15 Aug 2015, Kris Borer wrote:
>
>> Add a semantic patch for fixing some cases of checkpatch.pl error:
>>
>> ERROR: do not use assignment in if condition
>>
>> Reviewed-by: Julia Lawall <julia.lawall@lip6.fr>
>
> Sorry, but I'm not done reviewing it. I have a lot of changes to propose.
> Is this different than the RFC v2?
>
> julia
>
>> Signed-off-by: Kris Borer <kborer@gmail.com>
>> ---
>> scripts/coccinelle/style/assignment_in_if.cocci | 92 +++++++++++++++++++++++++
>> 1 file changed, 92 insertions(+)
>> create mode 100644 scripts/coccinelle/style/assignment_in_if.cocci
>>
>> diff --git a/scripts/coccinelle/style/assignment_in_if.cocci b/scripts/coccinelle/style/assignment_in_if.cocci
>> new file mode 100644
>> index 0000000..22ab161
>> --- /dev/null
>> +++ b/scripts/coccinelle/style/assignment_in_if.cocci
>> @@ -0,0 +1,92 @@
>> +// find checkpatch.pl errors of the type:
>> +// ERROR: do not use assignment in if condition
>> +//
>> +// This script is designed to correct code where assignments exist in if
>> +// conditions. It is only capable of handling a subset of such problems.
>> +//
>> +// For example:
>> +//
>> +// if(result = myfun())
>> +//
>> +// would become:
>> +//
>> +// result = myfun();
>> +// if(result)
>> +//
>> +// Confidence: Moderate
>> +
>> +
>> +// if ( ret = call() )
>> +@if1@
>> +identifier i;
>> +expression E;
>> +statement S1, S2;
>> +@@
>> +
>> ++ i = E;
>> + if (
>> +- (i = E)
>> ++ i
>> + ) S1 else S2
>> +
>> +
>> +// if ( (ret = call()) < 0 )
>> +@if2@
>> +identifier i;
>> +expression E;
>> +statement S1, S2;
>> +binary operator b;
>> +@@
>> +
>> ++ i = E;
>> + if (
>> +- (i = E)
>> ++ i
>> + b ... ) S1 else S2
>> +
>> +// if ( ptr->fun && (ret = ptr->fun()) < 0 )
>> +@if3@
>> +identifier i, i2;
>> +expression E1, E2;
>> +constant c;
>> +binary operator b;
>> +@@
>> +
>> ++ if( E1->i ) {
>> ++ i2 = E2;
>> ++ if (i2 b c) {
>> +- if( E1->i && ((i2 = E2) b c) ) {
>> + ...
>> +- }
>> ++ }
>> ++ }
>> +
>> +// if ( (ret = call()) < 0 && ret != -1 )
>> +@if4@
>> +identifier i;
>> +expression E, E2;
>> +statement S1, S2;
>> +binary operator b;
>> +@@
>> +
>> ++ i = E;
>> + if (
>> +- (i = E)
>> ++ i
>> + b
>> + ... && E2 ) S1 else S2
>> +
>> +// if ( (ret = call()) < 0 && ret != -1 && ret != -2 )
>> +@if5@
>> +identifier i;
>> +expression E, E2, E3;
>> +statement S1, S2;
>> +binary operator b;
>> +@@
>> +
>> ++ i = E;
>> + if (
>> +- (i = E)
>> ++ i
>> + b
>> + ... && E2 && E3 ) S1 else S2
>> --
>> 1.9.1
>>
>>
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] coccinelle: add style check for assignment in if
2015-08-15 17:33 ` Kris Borer
@ 2015-08-15 17:46 ` Julia Lawall
0 siblings, 0 replies; 4+ messages in thread
From: Julia Lawall @ 2015-08-15 17:46 UTC (permalink / raw)
To: Kris Borer
Cc: Gilles Muller, nicolas.palix, Michal Marek, linux-kernel,
Joe Perches, cocci
My version is below. It adds parentheses in a few more places, and allows
assignments without a binary operator (b) in the && cases.
Also I threw in a case for things like: if (ret = -ENOENT, !data)
Another thing is that the header is not really correct. It should be in
the following format:
/// Short comment of gernal interest
//# More information, eg about potential false positives
///
// Confidence: Moderate
// Copyright: whatever you want, it would be nice to have your name
// URL: http://coccinelle.lip6.fr/
// Comments:
// Options: --no-includes --include-headers
Then you should have:
virtual patch
Otherwise, the kernel infrastructure will not think that the patch mode is
supported. No other changes are needed, since no other modes are
supported. I don't think that supporting other modes is necessary, since
I think that checkpatch complains about this issue too.
julia
------------------------
// find checkpatch.pl errors of the type:
// ERROR: do not use assignment in if condition
//
// This script is designed to correct code where assignments exist in if
// conditions. It is only capable of handling a subset of such problems.
//
// For example:
//
// if(result = myfun())
//
// would become:
//
// result = myfun();
// if(result)
//
// Confidence: Moderate
// if ( (ret = call()) < 0 )
@if2@
expression i;
expression E, E2;
statement S1, S2;
binary operator b;
@@
+ i = E;
if (
(
... b
- (i = E)
+ i
|
- (i = E)
+ i
b ...
|
- (i = E)
+ i
|
- (i = E),
E2
)
) S1 else S2
// if ( ptr->fun && (ret = ptr->fun()) < 0 )
@if3@
expression i2;
expression E1, E2;
constant c;
binary operator b;
@@
+ if( E1 ) {
+ i2 = E2;
+ if (i2 b c) {
- if( E1 && ((i2 = E2) b c) ) {
...
- }
+ }
+ }
// if ( (ret = call()) < 0 && ret != -1 )
@if4@
expression i;
expression E, E2;
statement S1, S2;
binary operator b;
@@
+ i = E;
if (
(
(...
b
- (i = E)
+ i
)
|
(
- (i = E)
+ i
b
...)
|
- (i = E)
+ i
) && E2 ) S1 else S2
// if ( (ret = call()) < 0 && ret != -1 && ret != -2 )
@if5@
expression i;
expression E, E2, E3;
statement S1, S2;
binary operator b;
@@
+ i = E;
if (
(
(...
b
- (i = E)
+ i
)
|
(
- (i = E)
+ i
b
...)
|
- (i = E)
+ i
) && E2 && E3 ) S1 else S2
@@
expression i,e;
statement S1,S2;
@@
if (<+...
-i = e
+BAD
...+>) S1 else S2
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2015-08-15 17:46 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-15 17:18 [PATCH] coccinelle: add style check for assignment in if Kris Borer
2015-08-15 17:30 ` Julia Lawall
2015-08-15 17:33 ` Kris Borer
2015-08-15 17:46 ` Julia Lawall
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®