From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932090AbbHNNlA (ORCPT ); Fri, 14 Aug 2015 09:41:00 -0400 Received: from mail-qk0-f182.google.com ([209.85.220.182]:35756 "EHLO mail-qk0-f182.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751892AbbHNNk6 (ORCPT ); Fri, 14 Aug 2015 09:40:58 -0400 From: Kris Borer To: Julia.Lawall@lip6.fr Cc: Gilles.Muller@lip6.fr, nicolas.palix@imag.fr, mmarek@suse.cz, linux-kernel@vger.kernel.org, cocci@systeme.lip6.fr, Kris Borer Subject: [RFC v2] coccinelle: add style check for assignment in if Date: Fri, 14 Aug 2015 09:40:20 -0400 Message-Id: <1439559620-7293-1-git-send-email-kborer@gmail.com> X-Mailer: git-send-email 1.9.1 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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