* [RFC] Replacing special goto statements by direct returns?
@ 2026-11-03 14:23 Markus Elfring
2026-11-03 16:38 ` Julia Lawall
0 siblings, 1 reply; 3+ messages in thread
From: Markus Elfring @ 2026-11-03 14:23 UTC (permalink / raw)
To: cocci, tools; +Cc: kernel-janitors, LKML
Hello,
The documentation for the Linux coding style contains the following information.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v7.4-rc1#n526
“…
The goto statement comes in handy when a function exits from multiple
locations and some common work such as cleanup has to be done. If there is no
cleanup needed then just return directly.
…”
Thus it seems that the following script for the semantic patch language
can work to some degree.
@replacement exists@
identifier label, var;
@@
-goto label;
+return var;
...
-label:
return var;
Unfortunately, patches would be generated then by such a simple SmPL code
which would still preserve return statements that became unreachable.
Test result example:
Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time spatch --allow-inconsistent-paths fs/xfs/libxfs/xfs_btree.c …/Projekte/Coccinelle/janitor/replace_goto_by_return2.cocci
…
@@ -1735,7 +1735,7 @@ xfs_btree_increment(
--lev;
error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
if (error)
- goto error0;
+ return error;
xfs_btree_setbuf(cur, lev, bp);
cur->bc_levels[lev].ptr = 1;
@@ -1747,8 +1747,6 @@ out1:
out0:
*stat = 0;
return 0;
-
-error0:
return error;
}
…
real 0m1,074s
user 0m0,995s
sys 0m0,071s
How should such a source code transformation approach be improved accordingly?
Regards,
Markus
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [RFC] Replacing special goto statements by direct returns?
2026-11-03 14:23 [RFC] Replacing special goto statements by direct returns? Markus Elfring
@ 2026-11-03 16:38 ` Julia Lawall
2026-11-03 17:48 ` Markus Elfring
0 siblings, 1 reply; 3+ messages in thread
From: Julia Lawall @ 2026-11-03 16:38 UTC (permalink / raw)
To: Markus Elfring; +Cc: cocci, tools, kernel-janitors, LKML
[-- Attachment #1: Type: text/plain, Size: 2104 bytes --]
On Tue, 3 Nov 2026, Markus Elfring wrote:
> Hello,
>
> The documentation for the Linux coding style contains the following information.
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v7.4-rc1#n526
>
> “…
> The goto statement comes in handy when a function exits from multiple
> locations and some common work such as cleanup has to be done. If there is no
> cleanup needed then just return directly.
> …”
>
>
> Thus it seems that the following script for the semantic patch language
> can work to some degree.
>
>
> @replacement exists@
> identifier label, var;
> @@
> -goto label;
> +return var;
> ...
> -label:
> return var;
>
>
>
> Unfortunately, patches would be generated then by such a simple SmPL code
> which would still preserve return statements that became unreachable.
>
> Test result example:
> Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time spatch --allow-inconsistent-paths fs/xfs/libxfs/xfs_btree.c …/Projekte/Coccinelle/janitor/replace_goto_by_return2.cocci
> …
> @@ -1735,7 +1735,7 @@ xfs_btree_increment(
> --lev;
> error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
> if (error)
> - goto error0;
> + return error;
>
> xfs_btree_setbuf(cur, lev, bp);
> cur->bc_levels[lev].ptr = 1;
> @@ -1747,8 +1747,6 @@ out1:
> out0:
> *stat = 0;
> return 0;
> -
> -error0:
> return error;
> }
I think you can do:
@useful@
statement S;
expression error;
identifier error0;
position p;
@@
S
error0:@p
return error;
@useless@
position p1 != useful.p;
expression error;
identifier error0;
@@
f(...) {
(
<...
- goto error0;
+ return error;
...>
&
...
error0:@p1
return error;
)
}
I have not tested this. It might not work, or it might be very slow.
julia
> …
> real 0m1,074s
> user 0m0,995s
> sys 0m0,071s
>
>
>
> How should such a source code transformation approach be improved accordingly?
>
> Regards,
> Markus
>
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [RFC] Replacing special goto statements by direct returns?
2026-11-03 16:38 ` Julia Lawall
@ 2026-11-03 17:48 ` Markus Elfring
0 siblings, 0 replies; 3+ messages in thread
From: Markus Elfring @ 2026-11-03 17:48 UTC (permalink / raw)
To: Julia Lawall, cocci; +Cc: tools, kernel-janitors, LKML
> I think you can do:
>
> @useful@
> statement S;
> expression error;
> identifier error0;
> position p;
> @@
>
> S
> error0:@p
> return error;
>
> @useless@
> position p1 != useful.p;
> expression error;
> identifier error0;
> @@
>
> f(...) {
> (
> <...
> - goto error0;
> + return error;
> ...>
> &
> ...
> error0:@p1
> return error;
> )
> }
>
> I have not tested this.
This is a pity.
> It might not work,
I got corresponding development concerns.
* You proposed to use a statement variable in the SmPL rule “useful”.
How safe is the control flow connection to the subsequent code?
* Another metavariable declaration is probably helpful for “f” in the SmPL rule “useless”.
Questionable test result:
Markus_Elfring@Sonne:…/Projekte/Coccinelle/janitor> spatch --parse-cocci replace_goto_by_return-Julia.cocci
…
minus: parse error:
File "replace_goto_by_return-Julia.cocci", line 25, column 2, charpos = 400
around = '...',
whole content = & ...
Would you be looking for a better SmPL conjunction?
> or it might be very slow.
I hope that experiences can grow also for more desirable software run time characteristics.
Regards,
Markus
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-03 17:48 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-03 14:23 [RFC] Replacing special goto statements by direct returns? Markus Elfring
2026-11-03 16:38 ` Julia Lawall
2026-11-03 17:48 ` Markus Elfring
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®