From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6D66144E653 for ; Thu, 23 Jul 2026 18:46:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784832369; cv=none; b=qvpZu70Qv7ChoByHk2mIECLhImrfBc2B/dXtdtZNibGC6EAWpKetvdQxL2b6PbzAQQr8ILchZXMq2zsdw0HIyU2gVF3VwNdsNOZ8DWjtpKHhZWtSQ3+DL0EreNLSg//dZXzJM6zoWTlg7GZqMTp/cIx+ao8sKb1Ngj/igrvxHro= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784832369; c=relaxed/simple; bh=7CSCLiBzSym1VTRlnZsHYqVM4EjgdFe8A2vqS9D0LTQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bGZ/uUM/NDFQtNai12TJ9rU0ucXIefcahoJvCDfC9s4A0JGm9OPv8fJKmTPe5AtJCr3e63fZBoIqiptgvKSWCZ+zCbNuIrP18ehLb/9QVTX/WpTB5U5cux/oZ5/2d/v8xyt+awruco7KRHR77puAgs14vYlUEy0nZvUPxScPe94= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=bF3K+rPU; arc=none smtp.client-ip=209.85.216.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bF3K+rPU" Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-38de840f2f0so798483a91.0 for ; Thu, 23 Jul 2026 11:45:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784832357; x=1785437157; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=awQoiDBDgXhHqvQuYMCoxfsFQDhNz1mjFe+tSKjVwWE=; b=bF3K+rPU0TFK5KhyGc2AzVw632t6LxJbZQFXxC7T73e7srbTx9xXldPP63XS1mxLo+ aFVpk2aiHmUgRSiVpBEaLJII5AgRbQUsFZ8aXUU1Nvn+GSakPXNHNMpVJpY25a93JIsR QeEQV1onhatMCZowK0Ax/iEVsQyG8l3v8CsthXWLkLe7KO0Q/1EedGADGMQX9TyPwKyH 9sWMZwDpl3c1dtnDDcByF3TY7I9b+Ts0OWssiqT4YhKKgAlL/kVE1appoLkyYS53/EBb jxaQQHSdsFtDekBmNpik//PAO88YIEp9VkZgGI5f4AVgrrOFg9CwOW9DIzzSrh44oEAx BsUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784832357; x=1785437157; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=awQoiDBDgXhHqvQuYMCoxfsFQDhNz1mjFe+tSKjVwWE=; b=qKwGvDSUd7VW/BXHoHIh6OuL7e2oohbQbe1SRtwGjh+dvs5LG2FR/Goh0e2an9IBQd nEZsoaJkzTBodkdurjz1nac+SUbG708r5d7bHATsGcLC6TVaeQOFxY8RaHjQf0vQssKN wR5zir9rTJvOBgMMd+1FsmQsfkSCSQ+JFcae8Ag1VANipZ2VkEpYBFDf8tffdQy6fVja z6WXYHnEqSjfSMUFAsPpBYft75GefuBeFWuDlTIEqzSvuYeylMQGC24OkaMSdj2ZJpb5 LKJ+X0G2RCB+33BWgrOWr1SBAoFzM8+fhG0hAYYjdbwtGqhVkysBrF2Nzd/bghp+Bcea fNXw== X-Forwarded-Encrypted: i=1; AHgh+RoKLHB/Xo35F0GneIdBaissdnAfATBqLFttXhBORyPCR7oCiA85a0NPzQM1kDj8jyXUbHVsVFdLv8BAsio=@vger.kernel.org X-Gm-Message-State: AOJu0YwNTHKiZWL7arr+5Gz0ICQuudePhMwfxJ/x2JWPamah/hPLfZNO +MG9hvhQzS5kvRnk/bd03fH65UsTLiqIVj2OxV4ZQfjLTXjZdeqfZwYr X-Gm-Gg: AR+sD12W7qaHhXm6f4AQhMBxX0OA0Knt1Ml0GKPzWTYPxVvPPVs8fNJL4z8QaaCSjSS 8lCnvLOCKaCy+TutLLhqIFNZ5ZfbS8k7MRBIm5MjHpsQNTceXs/hFA/C6bPEVUq6VUr22RLPo5S 4Kyc5Y/5tUsz1QnVLbjMZ1t2bT5MrfCTrK/MlP7NYaiBEWhKSHGxt7CcsjubmtqeXDnbuK/YQwz 4ZJg9Vb9C2EhB0PIZEwEV8cqtCCxFOK+6knTEW7atFWHC+ZDqJ0JGFbzV7cN0ho+hbopNnYfqXU AW3J8vulN2+4Mvcr2c8JZuOEIAmNzuy785BfX4WkMkyM6fMuBdTwFRnLSRU5uakREpDedCmYfvt p6es5lMKaLe0cU2iKAEx36QAcsKBuuH4BJ44EJny186kfRSM6x5hlrmuFfgrHolaepvD2JZtn X-Received: by 2002:a17:90b:544f:b0:37f:9cdf:f037 with SMTP id 98e67ed59e1d1-38ec660bb8dmr4570424a91.26.1784832356687; Thu, 23 Jul 2026 11:45:56 -0700 (PDT) Received: from ubuntu.. ([110.9.142.4]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-38f03af8a45sm308155a91.10.2026.07.23.11.45.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jul 2026 11:45:56 -0700 (PDT) From: Sang-Heon Jeon To: Julia.Lawall@inria.fr, Nicolas Palix Cc: cocci@inria.fr, linux-kernel@vger.kernel.org Subject: [PATCH 01/36] coccinelle: misc: add cond_return_no_effect.cocci Date: Fri, 24 Jul 2026 03:45:03 +0900 Message-ID: <20260723184538.3888637-2-ekffu200098@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260723184538.3888637-1-ekffu200098@gmail.com> References: <20260723184538.3888637-1-ekffu200098@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add a new Coccinelle script which removes a conditional return that has no effect: if (ret) return ret; return ret; Both branches return the same value, so the check can be removed. The condition can also be a negation or a comparison with a constant. Such code is usually a leftover from removing a statement between the two returns. When a local variable is assigned right before the check, the assignment and the two returns turn into a single return of the assigned expression, and the declaration is dropped if nothing else uses the variable. Otherwise only the check is removed. The fold can delete comments between the check and the final return, so the generated patch should be reviewed. Signed-off-by: Sang-Heon Jeon --- .../misc/cond_return_no_effect.cocci | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 scripts/coccinelle/misc/cond_return_no_effect.cocci diff --git a/scripts/coccinelle/misc/cond_return_no_effect.cocci b/scripts/coccinelle/misc/cond_return_no_effect.cocci new file mode 100644 index 000000000000..541aafdc32c6 --- /dev/null +++ b/scripts/coccinelle/misc/cond_return_no_effect.cocci @@ -0,0 +1,121 @@ +// SPDX-License-Identifier: GPL-2.0-only +/// +/// Remove a conditional return that has no effect: +/// +/// if (ret) +/// return ret; +/// return ret; +/// +/// Both branches return the same variable, so the check has no +/// effect. It can also be a negation or a comparison with a +/// constant. +/// +/// When a local variable is assigned right before the check, the +/// assignment and the two returns turn into a single return of the +/// assigned expression, and the declaration is dropped if nothing +/// else uses the variable. Otherwise only the check is removed. +/// +// Such code is usually a leftover from removing a statement between +// the two returns. +// +// Confidence: High +// Copyright: (C) 2026 Sang-Heon Jeon +// Comments: The fold can delete comments between the check and the +// final return, so review the generated patch. +// Options: --no-includes --include-headers + +virtual patch +virtual context +virtual org +virtual report + +//---------------------------------------------------------- +// For patch mode +//---------------------------------------------------------- + +@collect depends on patch@ +identifier ret; +expression E; +binary operator cmp = {<, <=, >, >=, ==, !=}; +constant C; +@@ + ret = E; + if (\(ret \| !ret \| ret cmp C\)) + return ret; + return ret; + +@depends on patch@ +local idexpression ret; +expression E; +binary operator cmp = {<, <=, >, >=, ==, !=}; +constant C; +@@ +- ret = E; +- if (\(ret \| !ret \| ret cmp C\)) +- return ret; +- return ret; ++ return E; + +@depends on patch@ +idexpression ret; +binary operator cmp = {<, <=, >, >=, ==, !=}; +constant C; +@@ +- if (\(ret \| !ret \| ret cmp C\)) +- return ret; + return ret; + +@depends on patch@ +type T; +identifier collect.ret; +@@ +- T ret; + ... when != ret + when strict + +@depends on patch@ +type T; +identifier collect.ret; +constant C; +@@ +- T ret = C; + ... when != ret + when strict + + +//---------------------------------------------------------- +// For context mode +//---------------------------------------------------------- + +@depends on context@ +idexpression ret; +binary operator cmp = {<, <=, >, >=, ==, !=}; +constant C; +@@ +* if (\(ret \| !ret \| ret cmp C\)) +* return ret; + return ret; + +//---------------------------------------------------------- +// For org and report mode +//---------------------------------------------------------- + +@r depends on org || report@ +idexpression ret; +binary operator cmp = {<, <=, >, >=, ==, !=}; +constant C; +position p; +@@ + if@p (\(ret \| !ret \| ret cmp C\)) + return ret; + return ret; + +@script:python depends on org@ +p << r.p; +@@ +cocci.print_main("WARNING: conditional return with no effect (both branches return the same value)", p) + +@script:python depends on report@ +p << r.p; +@@ +coccilib.report.print_report(p[0], "WARNING: conditional return with no effect (both branches return the same value)") -- 2.43.0