From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3CAECC433F5 for ; Mon, 21 Mar 2022 15:30:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350266AbiCUPcU (ORCPT ); Mon, 21 Mar 2022 11:32:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238427AbiCUPcR (ORCPT ); Mon, 21 Mar 2022 11:32:17 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E264A26ADE for ; Mon, 21 Mar 2022 08:30:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1647876651; x=1679412651; h=date:from:to:cc:subject:message-id:mime-version: content-id; bh=6R4a25yf57fnkmnxguh1KEXG3Yi7RK574IAzZuSCxo0=; b=iHLVzCQzJO8mJF11+7FlSj1QBO1nNIYq8zzkDIFpURUW/H2TAaJnu9UA iCYbEdAAJ6hwGSjqH4oA30ASDa2cKa6tWrJqSO3WKq8RlQO+dlwHDBsBm HFCJBH8W3ZhI0eyip5rAqeo3fF9ipbYcu69ycUx9lUEWpztE4vXm0XfJV 0RcDNsxzV4txEPSPE7ekDB5R9HZKlNKvreNuI51/x6p/+rv9Hf4PwUz6A apHcGsYQfI4uvnpjM0EsSjFx7P7tlmTT+KRyZ0c3witwHV9BqEuAUcMBO ADd+qMPpMM10nfBVeazJIujKHPE1MwwXYMrgwy05SYVHIToqS/6wAOsXs g==; X-IronPort-AV: E=McAfee;i="6200,9189,10292"; a="320782235" X-IronPort-AV: E=Sophos;i="5.90,198,1643702400"; d="scan'208";a="320782235" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Mar 2022 08:30:51 -0700 X-IronPort-AV: E=Sophos;i="5.90,198,1643702400"; d="scan'208";a="518479086" Received: from jfecht-mobl.ger.corp.intel.com ([10.252.56.144]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Mar 2022 08:30:49 -0700 Date: Mon, 21 Mar 2022 17:30:44 +0200 (EET) From: =?ISO-8859-15?Q?Ilpo_J=E4rvinen?= To: Joe Perches cc: Andy Whitcroft , Dwaipayan Ray , Lukas Bulwahn , LKML Subject: [PATCH v2 1/1] checkpatch: don't suggest else is not useful with chained Message-ID: <139ce92d-32f-4187-2f38-48e6ec354b74@linux.intel.com> MIME-Version: 1.0 Content-Type: multipart/mixed; BOUNDARY="8323329-481241047-1647876567=:10813" Content-ID: <39465e99-31a-ad9f-4da-d2131c594354@linux.intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323329-481241047-1647876567=:10813 Content-Type: text/plain; CHARSET=ISO-8859-15 Content-Transfer-Encoding: 8BIT Content-ID: When if blocks are chained with "else if" like this: if (a) c = 0; else if (b) break; else c = 1; checkpatch recommends removing else: WARNING: else is not generally useful after a break or return Removing else will easily introduce logic errors in this situation so it's better to check if the preceding line has "else if" before issuing that warning. Fixes: 032a4c0f9a77 ("checkpatch: warn on unnecessary else after return or break") Signed-off-by: Ilpo Järvinen --- v2: - Don't match spuriously to removed lines - Extend check backwards to the first line with the same or lower indent as "else" line scripts/checkpatch.pl | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index b01c36a15d9d..ec0d9ac7e555 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -4037,14 +4037,23 @@ sub process { # check indentation of any line with a bare else # (but not if it is a multiple line "if (foo) return bar; else return baz;") # if the previous line is a break or return and is indented 1 tab more... +# but don't warn when there is "else if" before that line to avoid logic errors if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { my $tabs = length($1) + 1; if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && defined $lines[$linenr] && $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { - WARN("UNNECESSARY_ELSE", - "else is not generally useful after a break or return\n" . $hereprev); + my $ifline = $linenr - 3; + $tabs--; + while ($ifline >= 1 && + $lines[$ifline] !~ /^[^-]\t{0,$tabs}[^\t]/) { + $ifline--; + } + if ($lines[$ifline] !~ /^[\+ ]\t{$tabs,$tabs}(\} *|)else if\s*\(/) { + WARN("UNNECESSARY_ELSE", + "else is not generally useful after a break or return\n" . $hereprev); + } } } -- 2.30.2 --8323329-481241047-1647876567=:10813--