From: Sang-Heon Jeon <ekffu200098@gmail.com>
To: Julia.Lawall@inria.fr, Nicolas Palix <nicolas.palix@imag.fr>
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 [thread overview]
Message-ID: <20260723184538.3888637-2-ekffu200098@gmail.com> (raw)
In-Reply-To: <20260723184538.3888637-1-ekffu200098@gmail.com>
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 <ekffu200098@gmail.com>
---
.../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
WARNING: multiple messages have this Message-ID (diff)
From: Sang-Heon Jeon <ekffu200098@gmail.com>
To: Julia.Lawall@inria.fr, Nicolas Palix <nicolas.palix@imag.fr>
Cc: cocci@inria.fr, linux-kernel@vger.kernel.org
Subject: [cocci] [PATCH 01/36] coccinelle: misc: add cond_return_no_effect.cocci
Date: Fri, 24 Jul 2026 03:45:03 +0900 [thread overview]
Message-ID: <20260723184538.3888637-2-ekffu200098@gmail.com> (raw)
In-Reply-To: <20260723184538.3888637-1-ekffu200098@gmail.com>
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 <ekffu200098@gmail.com>
---
.../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
next prev parent reply other threads:[~2026-07-23 18:46 UTC|newest]
Thread overview: 119+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-23 18:45 [PATCH 00/36] treewide: remove conditional returns with no effect Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [Intel-wired-lan] " Sang-Heon Jeon
2026-07-23 18:45 ` Sang-Heon Jeon [this message]
2026-07-23 18:45 ` [cocci] [PATCH 01/36] coccinelle: misc: add cond_return_no_effect.cocci Sang-Heon Jeon
2026-07-24 11:19 ` Markus Elfring
2026-07-24 11:38 ` Julia Lawall
2026-07-24 11:47 ` Markus Elfring
2026-07-24 14:42 ` Sang-Heon Jeon
2026-07-24 15:32 ` [cocci] [1/36] " Markus Elfring
2026-07-24 16:33 ` Sang-Heon Jeon
2026-07-24 16:50 ` Markus Elfring
2026-07-24 14:41 ` [PATCH 01/36] " Sang-Heon Jeon
2026-07-24 14:55 ` Julia Lawall
2026-07-24 14:55 ` [cocci] " Julia Lawall
2026-07-24 16:02 ` [cocci] [1/36] " Markus Elfring
2026-07-24 16:48 ` [PATCH 01/36] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 02/36] drm/amd: remove conditional return with no effect Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 03/36] drm/radeon: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 04/36] dpll: zl3073x: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 05/36] drm/i915: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 06/36] drm: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 07/36] net: ethernet: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 20:24 ` Niklas Söderlund
2026-07-23 20:24 ` [cocci] " Niklas Söderlund
2026-07-23 20:35 ` Kiyanovski, Arthur
2026-07-23 20:35 ` [cocci] " Kiyanovski, Arthur
2026-07-23 18:45 ` [PATCH 08/36] net: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 19:04 ` Andrew Lunn
2026-07-23 19:04 ` [cocci] " Andrew Lunn
2026-07-23 18:45 ` [Intel-wired-lan] [PATCH 09/36] net: intel: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 10/36] wifi: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-24 0:37 ` Ping-Ke Shih
2026-07-24 0:37 ` [cocci] " Ping-Ke Shih
2026-07-23 18:45 ` [PATCH 11/36] ipvs: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 12/36] media: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 20:26 ` Niklas Söderlund
2026-07-23 20:26 ` [cocci] " Niklas Söderlund
2026-07-23 18:45 ` [PATCH 13/36] ALSA: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 14/36] ASoC: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-24 5:05 ` Mukunda,Vijendar
2026-07-24 5:05 ` [cocci] " Mukunda,Vijendar
2026-07-23 18:45 ` [PATCH 15/36] iio: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 21:46 ` Joshua Crofts
2026-07-23 21:46 ` [cocci] " Joshua Crofts
2026-07-24 0:38 ` Jonathan Cameron
2026-07-24 0:38 ` [cocci] " Jonathan Cameron
2026-07-24 15:22 ` Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 16/36] Input: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-24 19:30 ` Dmitry Torokhov
2026-07-23 18:45 ` [PATCH 17/36] clk: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-24 15:49 ` Brian Masney
2026-07-23 18:45 ` [PATCH 18/36] crypto: drivers - " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 19/36] dmaengine: qcom_hidma: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 20/36] stm class: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 21/36] RDMA/ocrdma: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 22/36] iommu/s390: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 23/36] dm vdo: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 24/36] pinctrl: mediatek: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 25/36] platform/x86: toshiba_haps: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-24 15:44 ` Ilpo Järvinen
2026-07-24 16:21 ` Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 26/36] power: supply: pm8916_lbc: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 27/36] RAS/AMD/ATL: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 19:44 ` Borislav Petkov
2026-07-23 19:44 ` [cocci] " Borislav Petkov
2026-07-23 18:45 ` [PATCH 28/36] regulator: wm831x-isink: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-24 8:24 ` Charles Keepax
2026-07-24 8:24 ` [cocci] " Charles Keepax
2026-07-23 18:45 ` [PATCH 29/36] rtc: pcf2127: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 30/36] scsi: mpt3sas: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 31/36] thermal/drivers/k3_bandgap: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 32/36] USB: serial: ch341: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 33/36] usb: typec: fusb302: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 34/36] smb: client: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 20:27 ` Steve French
2026-07-23 20:27 ` [cocci] " Steve French
2026-07-24 17:12 ` Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 35/36] cpupower: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-23 18:45 ` [PATCH 36/36] memblock: " Sang-Heon Jeon
2026-07-23 18:45 ` [cocci] " Sang-Heon Jeon
2026-07-24 21:48 ` [PATCH 00/36] treewide: remove conditional returns " Jakub Kicinski
2026-07-24 21:48 ` [Intel-wired-lan] " Jakub Kicinski
2026-07-24 23:35 ` (subset) " Sebastian Reichel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260723184538.3888637-2-ekffu200098@gmail.com \
--to=ekffu200098@gmail.com \
--cc=Julia.Lawall@inria.fr \
--cc=cocci@inria.fr \
--cc=linux-kernel@vger.kernel.org \
--cc=nicolas.palix@imag.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.