From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 388EC48B37B for ; Thu, 6 Aug 2026 17:50:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786038610; cv=none; b=QXbzhdvGjSvqfavzp2DtAcnH9QnTpf4/6w9k0jfhAUUirm2pTyJg4Vcrk69U3a1d52Pw7o0QOYyPN0WO71Wx2xt1hE8Qe6/t4g6espWuaLv/VWmv+hoaK0VtPUkHzqJwDsGOF8LoNP77TTTkgV9p4Y9ywmn5VsKxXpaPKCcVeio= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786038610; c=relaxed/simple; bh=DkXYnhzTEbb4QMULkUxFK5LrS+aABrLCP2PFwJFAsuE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Veh0S/ILhoATbsUX+OInt7p9SiIfXQ8+sHuvq/rVcAMKhaz5Itv8/i/XI9rEfIUOeOjHP0qRvUgpT8cPbQ/dH85EFVbpeKH4dr6dqpKixtokH4YF/oDfrrqQy8Ja7KBE3HvmbcFdNfr5IbuZ96qTjsR6q6VYPlkkHrPVw/wFUGU= 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=me3k1XHh; arc=none smtp.client-ip=209.85.214.179 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="me3k1XHh" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2cc891373e0so31229605ad.2 for ; Thu, 06 Aug 2026 10:50:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786038608; x=1786643408; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=0o4NBheCwSE10XnDe2BerT3Ry5/7biA01jIUqSNl/wI=; b=me3k1XHh19GvMiHhCIkQfKhViQ0A7QDB9TmLJ0qvSh2irZUvsDlWkXOzgYp08u5JA3 /nQDq1n9VhsTGPkSAAyMoW0QtdeAZY8XrpAUsHQcAXHum9EDt9V2cdQzXNSdOhau6/ll nizqqxrYuOiKW0s9UIZThQ367HbRmouzfe0KNVmZyVYSyz83dPELgAAxaLYLje4eboVD zM4Nxglydkj9y2p+NGhaa+ozBGg809l5tg6y/XkDLK3BwgfAog07y00UR/kfbYM6C44l JGDR0s3Pn2croUWCOWX1PkY0lKHaVAhU3MU1cgXLNjGkko5cQELrU6d6MhtKgOjM46cL 9UOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786038608; x=1786643408; h=content-transfer-encoding:mime-version: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=0o4NBheCwSE10XnDe2BerT3Ry5/7biA01jIUqSNl/wI=; b=H8BZrnRnoGhS4Gj8AH0Blg+SjWq/8VP0E0sHTgaddFCqhVKVkYv44D2B3GCSxsa8O0 gLoq6VvLRUDnsmvTYU7zzLPYbldZqxH4Vu9JSrw+vA+dz8ZlzEEbfLmuGdVy90aK/3Jr S7Lmoqrww5CDudMG9HOzFSbFtkFBK9kMfNjXI76IvwGpAMYU1jmET3slojrM2yUBNA61 UJxl42lfMbArjpq3yFzBtJV7vevkVsUNxlL9yt2NfKGdDqylKGGVJmuS/zCo9Jt76JdK HbbluLMQThjhYPy2FbB90hjo8rhtpLp5ayWQnklA+j5r+jxzWaUWv0kF7k6asITB6mdh n6mg== X-Forwarded-Encrypted: i=1; AHgh+RrXrV/63QWdYAcneLrKvQl5+yb4gZM2OWDPSf1MLFYGsHYGJVROjrn490KQ8EwvabeGg2sICZAk+pElxe8=@vger.kernel.org X-Gm-Message-State: AOJu0Yw26fpKJnSopFtT+xwmL7E90oEcCtWpfCTuiql/mZ6Sr6dcqpv+ +xhXtSZlbvEPaobLZ8SKL1s3zouNrsy2JYVy4a5YAr7Pvksz0KLiosfs X-Gm-Gg: AR+sD108j3NXogbUfYdprZ1XGj6AYtxDAgtz2Eh0+lDxSvjoBENf9Wrcr2hZr1W+rJs P4asVqtQC5Vj8ern1XUic8Y6VtoDvflKXGIf3sq6DjxpLPtTgtn4p0O8SOmyfig9ObNM50pEoqV 1mmjPk8YVOue1FP0SgGZ5e91HLobe4D+He32OwmbDEBgBegu2cnWxVeujkO9tSHG68jgej50Cz/ 6vDsfdHWzwY4I9/EbVJWiW54vi9Nky23etQwGWnidX7SnxJ7EMbayfFbT0uqufXHIEYSbnEJnEd sZEk07qEE8oBBew/7n4cfMUqZvwTp90QChk1a7ClRZeC9i4j0G9sgMPAqG40CYbu4ofZPf8Bd/9 IOPsi6yRRv4W5xgsY8VxEQmKWw5aQpcIkwx38PfpTQFtMOkMmHQG0ScQWd3sPbhtl41ZuHb+4jh /BkMD/BppM7yet2ERK/j06OQV90oUyCmNxMcCXGuakv0qW7qEN3i2uikg= X-Received: by 2002:a17:903:2ac5:b0:2c9:b8b7:5d27 with SMTP id d9443c01a7336-2d0ca712d8fmr205630375ad.1.1786038608277; Thu, 06 Aug 2026 10:50:08 -0700 (PDT) Received: from ubuntu.. ([110.9.142.4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2d120c611casm24675ad.83.2026.08.06.10.50.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Aug 2026 10:50:07 -0700 (PDT) From: Sang-Heon Jeon To: Julia Lawall , Nicolas Palix Cc: Jani Nikula , cocci@inria.fr, linux-kernel@vger.kernel.org Subject: [PATCH v2] coccinelle: misc: add cond_return_no_effect.cocci Date: Fri, 7 Aug 2026 02:49:51 +0900 Message-ID: <20260806174953.401869-1-ekffu200098@gmail.com> X-Mailer: git-send-email 2.43.0 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 --- Changes from v1 [1] - fix unexpected removal of global or static declarations, as Julia suggested - send the patch separately from the treewide series, as Mark suggested [1] https://lore.kernel.org/all/20260723184538.3888637-1-ekffu200098@gmail.com/ --- In the v1 thread, Jani shared the history of removing a similar script that matched an explicit return 0 at the end [1]. Current status of the cleanup patches, two weeks after v1: - 17/35 merged (2 sites changed to explicit return 0 as requested) - 3/35 reviewed - 15/35 no response yet [1] https://lore.kernel.org/all/0ee1ef4aa7daa908bf28397ccc639c89b6aabd9c@intel.com/ --- .../misc/cond_return_no_effect.cocci | 143 ++++++++++++++++++ 1 file changed, 143 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..334a9bcd2d6e --- /dev/null +++ b/scripts/coccinelle/misc/cond_return_no_effect.cocci @@ -0,0 +1,143 @@ +// 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 disable optional_storage@ +type T; +identifier collect.ret; +declaration D; +statement S; +@@ +( +- T ret; +( + D +| + S +) +& + T ret; + ... when != ret + when strict +) + +@depends on patch disable optional_storage@ +type T; +identifier collect.ret; +constant C; +declaration D; +statement S; +@@ +( +- T ret = C; +( + D +| + S +) +& + 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