checkpatch: allow an assignment in if condition for ACQUIRE_ERR()
PATCH 1/1 checkpatch: allow an assignment in if condition for ACQUIRE_ERR()
From: Alison Schofield <[email protected]>
New helpers, ACQUIRE() and ACQUIRE_ERR(), were recently introduced to clean up conditional locking paths [1].
That led to checkpatch ERRORS:
ERROR: do not use assignment in if condition
on usages like this:
ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region);
if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem)))
return rc;That compact format was a deliberate choice by the authors. By making this a checkpatch exception, existing ERRORs are quieted, and future users of the macro will not be dissuaded by checkpatch from using the preferred compact format.
[1] Commit d03fcf50ba56 ("cxl: Convert to ACQUIRE() for conditional rwsem locking")
--- scripts/checkpatch.pl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
scripts/checkpatch.pl +8 -1
@@ -5696,7 +5696,14 @@ sub process { my ($s, $c) = ($stat, $cond); my $fixed_assign_in_if = 0; - if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { + if ($c =~ /\bif\s*\((.*[^<>!=]=[^=].*)\)/s) { + my $expr = $1; + + # Allow ACQUIRE_ERR() macro syntax + if ($expr =~ /\w+\s*=\s*ACQUIRE_ERR\s*\(/) { + next; + } + if (ERROR("ASSIGN_IN_IF", "do not use assignment in if condition\n" . $herecurr) && $fix && $perl_version_ok) {
Signature · 1 line
2.37.3
Quoted 4 lines · On Tue, 2025-08-12 at 17:38 -0700, [email protected] wrote:
From: Alison Schofield <[email protected]>
New helpers,
ACQUIRE()andACQUIRE_ERR(), were recently introduced to clean up conditional locking paths [1].
[]
Quoted 4 lines
That compact format was a deliberate choice by the authors. By making this a checkpatch exception, existing ERRORs are quieted, and future users of the macro will not be dissuaded by checkpatch from using the preferred compact format.
not stylish IMO.
scripts/checkpatch.pl
Quoted 1 line
scripts/checkpatch.pl +0
-0
[]
scripts/checkpatch.pl sub process { · line 5705
Quoted 12 lines
scripts/checkpatch.pl +7
-1@@ -5696,7 +5696,14 @@ sub process { my ($s, $c) = ($stat, $cond); my $fixed_assign_in_if = 0; - if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { + if ($c =~ /\bif\s*\((.*[^<>!=]=[^=].*)\)/s) { + my $expr = $1; + + # Allow ACQUIRE_ERR() macro syntax + if ($expr =~ /\w+\s*=\s*ACQUIRE_ERR\s*\(/) { + next; + }
nak.
Using next would not do any additional checks on this line. Likely use basic block or a constuct like
if ($c =~ /\bif\s*\((.*[^<>!=]=[^=].*)/s &&
$1 !~ /\bACQUIRE_ERR\b/) {and perhaps the \w+ should be $Lval
Quoted 4 lines
scripts/checkpatch.pl +1
-0+ if (ERROR("ASSIGN_IN_IF", "do not use assignment in if condition\n" . $herecurr) && $fix && $perl_version_ok) {
Quoted 5 lines · Joe Perches
Quoted 4 lines · On Tue, 2025-08-12 at 17:38 -0700, [email protected] wrote:
From: Alison Schofield <[email protected]>
New helpers,
ACQUIRE()andACQUIRE_ERR(), were recently introduced to clean up conditional locking paths [1].
Quoted 7 lines
[]
Quoted 4 lines
That compact format was a deliberate choice by the authors. By making this a checkpatch exception, existing ERRORs are quieted, and future users of the macro will not be dissuaded by checkpatch from using the preferred compact format.
not stylish IMO.
I knew when suggesting this patch, that the 'no assignment in if condition' hasn't had any previous exceptions. I appreciate your helping in working out the implementation details here.
more below -
Quoted 18 lines
scripts/checkpatch.pl
Quoted 2 lines
scripts/checkpatch.pl +0
-0[]
scripts/checkpatch.pl sub process { · line 5705
Quoted 12 lines
scripts/checkpatch.pl +7
-1@@ -5696,7 +5696,14 @@ sub process { my ($s, $c) = ($stat, $cond); my $fixed_assign_in_if = 0; - if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { + if ($c =~ /\bif\s*\((.*[^<>!=]=[^=].*)\)/s) { + my $expr = $1; + + # Allow ACQUIRE_ERR() macro syntax + if ($expr =~ /\w+\s*=\s*ACQUIRE_ERR\s*\(/) { + next; + }nak.
Quoted 1 line
Using next would not do any additional checks on this line.
I see. I'll get rid of the next and use conditional blocks instead.
Quoted 7 lines
Likely use basic block or a constuct like
if ($c =~ /\bif\s*\((.*[^<>!=]=[^=].*)/s && $1 !~ /\bACQUIRE_ERR\b/) {and perhaps the \w+ should be $Lval
Thanks for that. The v2 approach uses the $Lval and captures the full if condition expression and iterates thru each assignment.
I did more diligence in v2 wrt testing and will post my test cases along with that so you can see what the patch intends to allow and what triggers ERRORs, including ERRORs that the v1 usage of 'next' got ignored.
-- Alison
Quoted 6 lines
Quoted 5 lines
scripts/checkpatch.pl +1
-0+ if (ERROR("ASSIGN_IN_IF", "do not use assignment in if condition\n" . $herecurr) && $fix && $perl_version_ok) {