Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1206128 > unrolled thread

[RFC] coccinelle: add style check for assignment in if

Started byKris Borer <kborer@gmail.com>
First post2015-08-12 16:00 +0200
Last post2015-08-12 17:20 +0200
Articles 5 — 3 participants

Back to article view | Back to linux.kernel


Contents

  [RFC] coccinelle: add style check for assignment in if Kris Borer <kborer@gmail.com> - 2015-08-12 16:00 +0200
    Re: [RFC] coccinelle: add style check for assignment in if Julia Lawall <julia.lawall@lip6.fr> - 2015-08-12 16:10 +0200
    Re: [RFC] coccinelle: add style check for assignment in if Michal Marek <mmarek@suse.cz> - 2015-08-12 16:20 +0200
      Re: [RFC] coccinelle: add style check for assignment in if Michal Marek <mmarek@suse.cz> - 2015-08-12 17:10 +0200
        Re: [RFC] coccinelle: add style check for assignment in if Julia Lawall <julia.lawall@lip6.fr> - 2015-08-12 17:20 +0200

#1206128 — [RFC] coccinelle: add style check for assignment in if

FromKris Borer <kborer@gmail.com>
Date2015-08-12 16:00 +0200
Subject[RFC] coccinelle: add style check for assignment in if
Message-ID<pWEJs-6cZ-5@gated-at.bofh.it>
Add a semantic patch for fixing some cases of checkpatch.pl error:

ERROR: do not use assignment in if condition

Signed-off-by: Kris Borer <kborer@gmail.com>
---
 scripts/coccinelle/style/assignment_in_if.cocci | 82 +++++++++++++++++++++++++
 1 file changed, 82 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..d9895e7
--- /dev/null
+++ b/scripts/coccinelle/style/assignment_in_if.cocci
@@ -0,0 +1,82 @@
+// find checkpatch.pl errors of the type:
+//	ERROR: do not use assignment in if condition
+//
+// 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 < c) {
+- if( E1->i && ((i2 = E2) b c) ) {
+  ...
+- }
++ 	}
++ }
+
+// if ( (ret = call()) < 0 && ret != 0 )
+@if4@
+identifier i;
+expression E, E2, E3;
+statement S1, S2;
+binary operator b;
+@@
+
++ i = E;
+  if (
+- (i = E)
++ i
+  b
+  ... && E2 && E3 ) S1 else S2
+
+// if ( (ret = call()) < 0 && ret != 0 && ret != 0 )
+@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

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [next] | [standalone]


#1206140

FromJulia Lawall <julia.lawall@lip6.fr>
Date2015-08-12 16:10 +0200
Message-ID<pWET7-6Dx-7@gated-at.bofh.it>
In reply to#1206128
Thanks for the contribution.  Have you checked very carefully that it
doesn't eg pull XXX out of if (E && XXX)?  (I don't know if it does or it
doesn't, but it is a common pitfall with this issue).

thanks,
julia

On Wed, 12 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
>
> Signed-off-by: Kris Borer <kborer@gmail.com>
> ---
>  scripts/coccinelle/style/assignment_in_if.cocci | 82 +++++++++++++++++++++++++
>  1 file changed, 82 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..d9895e7
> --- /dev/null
> +++ b/scripts/coccinelle/style/assignment_in_if.cocci
> @@ -0,0 +1,82 @@
> +// find checkpatch.pl errors of the type:
> +//	ERROR: do not use assignment in if condition
> +//
> +// 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 < c) {
> +- if( E1->i && ((i2 = E2) b c) ) {
> +  ...
> +- }
> ++ 	}
> ++ }
> +
> +// if ( (ret = call()) < 0 && ret != 0 )
> +@if4@
> +identifier i;
> +expression E, E2, E3;
> +statement S1, S2;
> +binary operator b;
> +@@
> +
> ++ i = E;
> +  if (
> +- (i = E)
> ++ i
> +  b
> +  ... && E2 && E3 ) S1 else S2
> +
> +// if ( (ret = call()) < 0 && ret != 0 && ret != 0 )
> +@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
>
>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1206148

FromMichal Marek <mmarek@suse.cz>
Date2015-08-12 16:20 +0200
Message-ID<pWF2O-6ON-23@gated-at.bofh.it>
In reply to#1206128
On 2015-08-12 15:51, Kris Borer wrote:
> Add a semantic patch for fixing some cases of checkpatch.pl error:
> 
> ERROR: do not use assignment in if condition

There is a gcc warning for this already.

Michal

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1206195

FromMichal Marek <mmarek@suse.cz>
Date2015-08-12 17:10 +0200
Message-ID<pWFPc-7Zw-1@gated-at.bofh.it>
In reply to#1206148
On 2015-08-12 16:53, Kris Borer wrote:
> On Wed, Aug 12, 2015 at 10:12 AM, Michal Marek <mmarek@suse.cz
> <mailto:mmarek@suse.cz>> wrote:
> 
>     On 2015-08-12 15:51, Kris Borer wrote:
>     > Add a semantic patch for fixing some cases of checkpatch.pl <http://checkpatch.pl> error:
>     >
>     > ERROR: do not use assignment in if condition
> 
>     There is a gcc warning for this already.
> 
>     Michal
> 
> 
> ​My intention was not to create another way to uncover problems but
> rather to ​provide a tool for people to use to fix them. Let me know if
> I am misunderstanding the purpose of this subsystem.

OK, so this is fixing a style issue, and not cases of accidental
assignment instead of '==' (for which there is a gcc warning and we
hopefully do not have such errors in the kernel). While I'm probably
ignorant and no not see how one style is better than the other, I see
that some maintainers already applied your patches based on this check.
So I'll merge it once Julia acks it.

P.S.: Please switch of HTML email, otherwise vger.kernel.org won't
accept your messages.

Michal
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1206210

FromJulia Lawall <julia.lawall@lip6.fr>
Date2015-08-12 17:20 +0200
Message-ID<pWFYS-8aR-23@gated-at.bofh.it>
In reply to#1206195

[Multipart message — attachments visible in raw view] — view raw

On Wed, 12 Aug 2015, Michal Marek wrote:

> On 2015-08-12 16:53, Kris Borer wrote:
> > On Wed, Aug 12, 2015 at 10:12 AM, Michal Marek <mmarek@suse.cz
> > <mailto:mmarek@suse.cz>> wrote:
> >
> >     On 2015-08-12 15:51, Kris Borer wrote:
> >     > Add a semantic patch for fixing some cases of checkpatch.pl <http://checkpatch.pl> error:
> >     >
> >     > ERROR: do not use assignment in if condition
> >
> >     There is a gcc warning for this already.
> >
> >     Michal
> >
> >
> > ​My intention was not to create another way to uncover problems but
> > rather to ​provide a tool for people to use to fix them. Let me know if
> > I am misunderstanding the purpose of this subsystem.
>
> OK, so this is fixing a style issue, and not cases of accidental
> assignment instead of '==' (for which there is a gcc warning and we
> hopefully do not have such errors in the kernel). While I'm probably
> ignorant and no not see how one style is better than the other, I see
> that some maintainers already applied your patches based on this check.
> So I'll merge it once Julia acks it.

Actually, assignments inside if tests are really annoying for Coccinelle,
because there become two different control flows from the assignment to
the test on the result.  So I would be happy to see these go away.

I'll check the semantic patch as soon as possible.

julia

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web