From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753251AbbHVPyT (ORCPT ); Sat, 22 Aug 2015 11:54:19 -0400 Received: from mail3-relais-sop.national.inria.fr ([192.134.164.104]:65529 "EHLO mail3-relais-sop.national.inria.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752596AbbHVPyS (ORCPT ); Sat, 22 Aug 2015 11:54:18 -0400 X-IronPort-AV: E=Sophos;i="5.15,728,1432591200"; d="scan'208";a="143520008" Date: Sat, 22 Aug 2015 08:54:08 -0700 (PDT) From: Julia Lawall X-X-Sender: jll@hadrien To: Kris Borer cc: Julia.Lawall@lip6.fr, Gilles.Muller@lip6.fr, nicolas.palix@imag.fr, mmarek@suse.cz, linux-kernel@vger.kernel.org, cocci@systeme.lip6.fr Subject: Re: [RFC v3] coccinelle: add style check for assignment in if In-Reply-To: <1440074038-646-1-git-send-email-kborer@gmail.com> Message-ID: References: <1440074038-646-1-git-send-email-kborer@gmail.com> User-Agent: Alpine 2.10 (DEB 1266 2009-07-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Acked-by: Julia Lawall However, I think it would be better to remove the "BAD" rulle at the end. julia On Thu, 20 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 > --- > Differences from Julia's version: > - removed some disjuction variations that led to improper transformations > - fixed header format > - added and updated comments > - reformatted some rules > > scripts/coccinelle/style/assignment_in_if.cocci | 140 ++++++++++++++++++++++++ > 1 file changed, 140 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..57f692d > --- /dev/null > +++ b/scripts/coccinelle/style/assignment_in_if.cocci > @@ -0,0 +1,140 @@ > +/// move assignments out of if conditions > +/// > +//# This script is designed to correct code where assignments exist in if > +//# conditions. It is only capable of handling a subset of such problems. > +//# Ideally it would handle all checkpatch errors of the following type: > +//# ERROR: do not use assignment in if condition > +//# > +//# For example: > +//# if(result = myfun()) > +//# > +//# would become: > +//# result = myfun(); > +//# if(result) > +// > +// Confidence: Moderate > +// Copyright: (C) 2015 Kris Borer. GPLv2. > +// URL: http://coccinelle.lip6.fr/ > +// Comments: > +// Options: --no-includes --include-headers > + > +virtual patch > + > + > +// if ( (ret = call()) ) > +// if ( (ret = call()) < 0 ) > +@if1@ > +expression i; > +expression E, E2; > +statement S1, S2; > +binary operator b; > +@@ > + > ++ i = E; > + if ( > +( > +- (i = E) > ++ i > +| > +- (i = E) > ++ i > + b ... > +| > +- (i = E), > + E2 > +) > + ) S1 else S2 > + > + > +// if ( ptr->fun && (ret = ptr->fun()) ) > +@if2@ > +expression i2; > +expression E1, E2; > +@@ > + > ++ if( E1 ) { > ++ i2 = E2; > ++ if (i2) { > +- if( E1 && (i2 = E2) ) { > + ... > +- } > ++ } > ++ } > + > + > +// 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()) && ret != -1 ) > +// if ( (ret = call()) < 0 && ret != -1 ) > +@if4@ > +expression i; > +expression E, E2; > +statement S1, S2; > +binary operator b; > +@@ > + > ++ i = E; > + if ( > +( > +- (i = E) > ++ i > +| > + ( > +- (i = E) > ++ i > + b > + ...) > +) > + && E2 ) S1 else S2 > + > + > +// if ( (ret = call()) && ret != -1 && ret != -2 ) > +// if ( (ret = call()) < 0 && ret != -1 && ret != -2 ) > +@if5@ > +expression i; > +expression E, E2, E3; > +statement S1, S2; > +binary operator b; > +@@ > + > ++ i = E; > + if ( > +( > +- (i = E) > ++ i > +| > + ( > +- (i = E) > ++ i > + b > + ...) > +) > + && E2 && E3 ) S1 else S2 > + > + > +// warn about any assignments that were missed > +@if6@ > +expression i,e; > +statement S1,S2; > +@@ > + > + if (<+... > +- i = e > ++ BAD > + ...+>) S1 else S2 > -- > 1.9.1 > >