cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
From: Markus Elfring <Markus.Elfring@web.de>
To: Julia Lawall <julia.lawall@lip6.fr>,
	Coccinelle <cocci@systeme.lip6.fr>,
	kernel-janitors@vger.kernel.org,
	Gilles Muller <Gilles.Muller@lip6.fr>,
	Masahiro Yamada <yamada.masahiro@socionext.com>,
	Michal Marek <michal.lkml@markovi.net>,
	Nicolas Palix <nicolas.palix@imag.fr>
Cc: Kate Stewart <kstewart@linuxfoundation.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	LKML <linux-kernel@vger.kernel.org>,
	Allison Randal <allison@lohutok.net>
Subject: [Cocci] [PATCH] Coccinelle: Add a SmPL script for the reconsideration of specific combinations of assignment and return statements
Date: Wed, 25 Sep 2019 13:33:45 +0200	[thread overview]
Message-ID: <4977fb04-cc29-3861-0aaf-cd93a0b0b1c7@web.de> (raw)
In-Reply-To: <alpine.DEB.2.21.1909231058380.2283@hadrien>

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Wed, 25 Sep 2019 11:55:24 +0200

Values from expressions were occasionally assigned to local variables
before they will be returned by the subsequent statement.
Such expressions can be directly specified in the return statement instead.

Adjust affected source code by the means of the semantic patch language
(Coccinelle software).

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 .../coccinelle/misc/move_code_to_return.cocci | 67 +++++++++++++++++++
 1 file changed, 67 insertions(+)
 create mode 100644 scripts/coccinelle/misc/move_code_to_return.cocci

diff --git a/scripts/coccinelle/misc/move_code_to_return.cocci b/scripts/coccinelle/misc/move_code_to_return.cocci
new file mode 100644
index 000000000000..78cdf84f9aaa
--- /dev/null
+++ b/scripts/coccinelle/misc/move_code_to_return.cocci
@@ -0,0 +1,67 @@
+// SPDX-License-Identifier: GPL-2.0
+/// Return expressions directly instead of assigning them to
+/// local variables immediately before affected statements.
+//
+// Keywords: return statements variable assignments coding style
+// Confidence: Medium
+
+virtual patch
+
+@replacement1 depends on patch@
+expression x;
+identifier f, rc;
+local idexpression lrc;
+type rt;
+@@
+ rt f(...)
+ {
+ ... when any
+ if (...)
+-{
+-lrc@rc = x;
+ return
+-       rc
++       x
+ ;
+-}
+ ... when any
+ }
+
+@replacement2 depends on patch@
+expression x;
+identifier f, rc;
+local idexpression lrc;
+type rt;
+@@
+ rt f(...)
+ {
+ ... when any
+-lrc@rc = x;
+ return
+-       rc
++       x
+ ;
+ ... when any
+ }
+
+@deletion2 depends on patch@
+identifier replacement2.f, replacement2.rc;
+type replacement2.rt, t;
+@@
+ rt f(...)
+ {
+ ... when any
+-t rc;
+ ... when != rc
+ }
+
+@deletion1 depends on patch@
+identifier replacement1.f, replacement1.rc;
+type replacement1.rt, t;
+@@
+ rt f(...)
+ {
+ ... when any
+-t rc;
+ ... when != rc
+ }
--
2.23.0

_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

  parent reply	other threads:[~2019-09-25 11:34 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-23  8:50 [Cocci] Reducing source code around return statements with SmPL? Markus Elfring
2019-09-23  8:59 ` Julia Lawall
2019-09-23 11:17   ` Markus Elfring
2019-09-24 15:27   ` Markus Elfring
2019-09-24 15:30     ` Julia Lawall
2019-09-24 15:40       ` Markus Elfring
     [not found]         ` <alpine.DEB.2.21.1909241750490.2281@hadrien>
2019-09-24 16:00           ` Markus Elfring
     [not found]             ` <alpine.DEB.2.21.1909241804490.2281@hadrien>
2019-09-24 16:14               ` Markus Elfring
2019-09-24 19:24       ` Markus Elfring
2019-09-24 20:16         ` Julia Lawall
2019-09-24 20:35           ` Markus Elfring
2019-09-24 20:49             ` Julia Lawall
2019-09-24 20:55               ` Markus Elfring
2019-09-25  6:13               ` Markus Elfring
2019-09-25  6:22                 ` Julia Lawall
2019-09-25  6:28                   ` Markus Elfring
     [not found]                     ` <alpine.DEB.2.21.1909250837140.2482@hadrien>
2019-09-25  6:40                       ` Markus Elfring
     [not found]                         ` <alpine.DEB.2.21.1909250854190.2482@hadrien>
2019-09-25  7:00                           ` Markus Elfring
     [not found]                             ` <alpine.DEB.2.21.1909251138120.2654@hadrien>
2019-09-25 11:00                               ` Markus Elfring
2019-09-24 18:20   ` Markus Elfring
2019-09-24 20:21     ` Julia Lawall
2019-09-24 20:48       ` Markus Elfring
2019-09-24 20:52         ` Julia Lawall
2019-09-24 21:00           ` Markus Elfring
2019-09-24 21:03             ` Julia Lawall
2019-09-25  6:04               ` Markus Elfring
2019-09-25  8:22               ` Markus Elfring
2019-09-25  8:22               ` Markus Elfring
2019-09-24 18:33   ` Markus Elfring
2019-09-25 11:33   ` Markus Elfring [this message]
2019-10-01 14:30     ` [Cocci] [PATCH v2] Coccinelle: Add a SmPL script for the reconsideration of specific combinations of assignment and return statements Markus Elfring

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=4977fb04-cc29-3861-0aaf-cd93a0b0b1c7@web.de \
    --to=markus.elfring@web.de \
    --cc=Gilles.Muller@lip6.fr \
    --cc=allison@lohutok.net \
    --cc=cocci@systeme.lip6.fr \
    --cc=gregkh@linuxfoundation.org \
    --cc=julia.lawall@lip6.fr \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=kstewart@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michal.lkml@markovi.net \
    --cc=nicolas.palix@imag.fr \
    --cc=yamada.masahiro@socionext.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).