checkpatch: don't flag ACQUIRE_ERR() assignments in if conditions
PATCH RESEND 1/1 checkpatch: don't flag ACQUIRE_ERR() assignments in if conditions
ACQUIRE_ERR() and its wrappers, PM_RUNTIME_ACQUIRE_ERR() and IIO_DEV_ACQUIRE_FAILED(), report whether a conditional cleanup.h guard was acquired, and drivers consume the result directly in an if condition:
if ((rc = ACQUIRE_ERR(mutex_intr, &lock))) return rc;
That combined form is the established style at the 49 in-tree call sites under drivers/cxl and drivers/pci/tsm.c, so ASSIGN_IN_IF fires there only as a false positive, and every patch touching those lines carries noise that reviewers have to wave off manually.
Skip the check when the assigned expression is a call to a macro from that family, matched by the *_ACQUIRE_ERR() / *_ACQUIRE_FAILED() naming convention of its wrappers. Plain assignments and near-miss identifiers still get flagged.
---
Discussion leading to this patch, where the false positive showed up on a cxl patch and covering the wrapper macros was requested as well:
https://lore.kernel.org/linux-cxl/[email protected]/
scripts/checkpatch.pl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
scripts/checkpatch.pl +6 -1
@@ -5777,7 +5777,12 @@ sub process { my ($s, $c) = ($stat, $cond); my $fixed_assign_in_if = 0; - if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { + # ACQUIRE_ERR() and its wrappers, e.g. PM_RUNTIME_ACQUIRE_ERR() + # and IIO_DEV_ACQUIRE_FAILED(), are intended to be evaluated in + # an if condition, with the error assigned in the condition: + # if ((rc = ACQUIRE_ERR(name, &lock))) + if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s && + $c !~ /=\s*\w*ACQUIRE_(?:ERR|FAILED)\s*\(/) { if (ERROR("ASSIGN_IN_IF", "do not use assignment in if condition\n" . $herecurr) && $fix && $perl_version_ok) {
base-commit: 50d05c7c76c96b90462f24debacca971d2e86713
Signature · 1 line
2.43.7
Quoted 13 lines · Guixin Liu
ACQUIRE_ERR()and its wrappers,PM_RUNTIME_ACQUIRE_ERR()andIIO_DEV_ACQUIRE_FAILED(), report whether a conditionalcleanup.hguard was acquired, and drivers consume the result directly in an if condition:if ((rc = ACQUIRE_ERR(mutex_intr, &lock))) return rc;That combined form is the established style at the 49 in-tree call sites under
drivers/cxlanddrivers/pci/tsm.c, soASSIGN_IN_IFfires there only as a false positive, and every patch touching those lines carries noise that reviewers have to wave off manually.
Quoted 4 lines
Skip the check when the assigned expression is a call to a macro from that family, matched by the
*_ACQUIRE_ERR()/*_ACQUIRE_FAILED()naming convention of its wrappers. Plain assignments and near-miss identifiers still get flagged.
Hi Guixin,
Glad to see you picking this up!
I tried to address the same checkpatch false positive last year. Joe Perches gave some useful feedback on my v1, including a suggestion to use checkpatch's $Lval, and I incorporated that into v2 along with some test cases. That may be useful for your next version.
More inline -
Quoted 16 lines
Signed-off-by Guixin Liu <[email protected]>---
Discussion leading to this patch, where the false positive showed up on a cxl patch and covering the wrapper macros was requested as well:https://lore.kernel.org/linux-cxl/[email protected]/
scripts/checkpatch.pl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)scripts/checkpatch.pl +0
-0
scripts/checkpatch.pl sub process { · line 5785
Quoted 10 lines
scripts/checkpatch.pl +6
-1@@ -5777,7 +5777,12 @@ sub process { my ($s, $c) = ($stat, $cond); my $fixed_assign_in_if = 0; - if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { + # ACQUIRE_ERR() and its wrappers, e.g. PM_RUNTIME_ACQUIRE_ERR() + # and IIO_DEV_ACQUIRE_FAILED(), are intended to be evaluated in + # an if condition, with the error assigned in the condition: + # if ((rc = ACQUIRE_ERR(name, &lock))) + if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s && + $c !~ /=\s*\w*ACQUIRE_(?:ERR|FAILED)\s*\(/) {
What happens if there is also an unrelated assignment in the condition? For example:
if ((rc = regular_function()) || (ret = ACQUIRE_ERR(lock_type, &lock)))
It looks like the ACQUIRE_ERR() match suppresses ASSIGN_IN_IF for the whole condition, including the unrelated assignment.
Could you use $Lval, as Joe suggested on my v1, to make this more precise?
I also suggest running this against a small test program with the expected-pass cases, ordinary assignments, and mixed cases like the one above. Feel free to reuse or extend the test cases from my v2 [1].
Could you also Cc linux-cxl on the next version? There are several users of this form there, so it may help get some review and testing tags on the patch.
-- Alison
Quoted 8 lines
scripts/checkpatch.pl +0
-0if (ERROR("ASSIGN_IN_IF", "do not use assignment in if condition\n" . $herecurr) && $fix && $perl_version_ok) {base-commit:
50d05c7c76c96b90462f24debacca971d2e86713Signature · 1 line
2.43.7
Quoted 20 lines · Alison Schofield
Quoted 13 lines · Guixin Liu
ACQUIRE_ERR()and its wrappers,PM_RUNTIME_ACQUIRE_ERR()andIIO_DEV_ACQUIRE_FAILED(), report whether a conditionalcleanup.hguard was acquired, and drivers consume the result directly in an if condition:if ((rc = ACQUIRE_ERR(mutex_intr, &lock))) return rc;That combined form is the established style at the 49 in-tree call sites under
drivers/cxlanddrivers/pci/tsm.c, soASSIGN_IN_IFfires there only as a false positive, and every patch touching those lines carries noise that reviewers have to wave off manually.Quoted 4 lines
Skip the check when the assigned expression is a call to a macro from that family, matched by the
*_ACQUIRE_ERR()/*_ACQUIRE_FAILED()naming convention of its wrappers. Plain assignments and near-miss identifiers still get flagged.Hi Guixin,
Quoted 8 lines
Glad to see you picking this up!
I tried to address the same checkpatch false positive last year. Joe Perches gave some useful feedback on my v1, including a suggestion to use checkpatch's $Lval, and I incorporated that into v2 along with some test cases. That may be useful for your next version.
More inline -
Yes, got that, thanks.
Quoted 27 lines
Quoted 16 lines
Signed-off-by Guixin Liu <[email protected]>---
Discussion leading to this patch, where the false positive showed up on a cxl patch and covering the wrapper macros was requested as well:https://lore.kernel.org/linux-cxl/[email protected]/
scripts/checkpatch.pl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)scripts/checkpatch.pl +0
-0scripts/checkpatch.pl sub process { · line 5785
Quoted 10 lines
scripts/checkpatch.pl +6
-1@@ -5777,7 +5777,12 @@ sub process { my ($s, $c) = ($stat, $cond); my $fixed_assign_in_if = 0; - if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { + # ACQUIRE_ERR() and its wrappers, e.g. PM_RUNTIME_ACQUIRE_ERR() + # and IIO_DEV_ACQUIRE_FAILED(), are intended to be evaluated in + # an if condition, with the error assigned in the condition: + # if ((rc = ACQUIRE_ERR(name, &lock))) + if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s && + $c !~ /=\s*\w*ACQUIRE_(?:ERR|FAILED)\s*\(/) {What happens if there is also an unrelated assignment in the condition? For example:
Quoted 6 lines
if ((rc = regular_function()) || (ret = ACQUIRE_ERR(lock_type, &lock)))It looks like the
ACQUIRE_ERR()match suppressesASSIGN_IN_IFfor the whole condition, including the unrelated assignment.Could you use $Lval, as Joe suggested on my v1, to make this more precise?
OK, changed in v2, thanks.
Quoted 3 lines
I also suggest running this against a small test program with the expected-pass cases, ordinary assignments, and mixed cases like the one above. Feel free to reuse or extend the test cases from my v2 [1].
Sure, I will try to test as much as possible.
Quoted 3 lines
Could you also Cc linux-cxl on the next version? There are several users of this form there, so it may help get some review and testing tags on the patch.
OK, will cc linux-cxl in v2, thanks.
Best Regards,
Guixin Liu
Quoted 17 lines
-- Alison
[1] https://lore.kernel.org/linux-cxl/[email protected]/
Quoted 8 lines
scripts/checkpatch.pl +0
-0if (ERROR("ASSIGN_IN_IF", "do not use assignment in if condition\n" . $herecurr) && $fix && $perl_version_ok) {base-commit:
50d05c7c76c96b90462f24debacca971d2e86713Signature · 1 line
2.43.7