Skip to contentSkip to conversation

checkpatch: handle variables that begin with 'case'

PATCH New Tiffany Yang 1 participant 3 messages Nov 14, 2025 Checkpatch

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 checkpatch: handle variables that begin with 'case'

The current script returns switch statement-related errors when variables that start with "case" are used in a ternary. For example:

    ./scripts/checkpatch.pl --git d1934ed9803c
    ERROR: space prohibited before that ':' (ctx:WxW)
    #289: FILE: drivers/android/tests/binder_alloc_kunit.c:321:
    +				  case_failed ? "FAILED" : "PASSED",
     				                         ^

    ERROR: trailing statements should be on next line
    #289: FILE: drivers/android/tests/binder_alloc_kunit.c:321:
    +				  case_failed ? "FAILED" : "PASSED",

Fix these false positives.

---
 scripts/checkpatch.pl | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +2 -2
index 92669904eecc..b026b1235079 100755
@@ -2179,7 +2179,7 @@ sub annotate_values {
 			$av_pending = 'E';
 			$type = 'N';
 
-		} elsif ($cur =~/^(case)/o) {
+		} elsif ($cur =~/^(case)\b/o) {
 			print "CASE($1)\n" if ($dbg_values > 1);
 			$av_pend_colon = 'C';
 			$type = 'N';
@@ -5797,7 +5797,7 @@ sub process {
 				$herecurr);
 		}
 # case and default should not have general statements after them
-		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
+		if ($line =~ /^.\s*(?:case\b\s*.*|default\s*):/g &&
 		    $line !~ /\G(?:
 			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
 			\s*return\s+
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
#1 lore raw

PATCH 2/3 checkpatch: check indentation on case without space

The script was not catching switch statement indentation errors when 'case' isn't followed by a space.

For example, checkpatch is silent for this change:

    +switch (foo) {
    +case(1):
    +	break;
    +		case(2):
    +	break;
    +}

But reports an error for the following:

    +switch (foo) {
    +case 1:
    +	break;
    +		case 2:
    +	break;
    +}

ERROR: switch and case should be at the same indent #39: FILE: kernel/checkpatch_case_fix/test_cases.c:21:
+switch (foo) {
[...]
+ case 2:

Update the check so that the error is reported for both.

---
 scripts/checkpatch.pl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/checkpatch.pl +1 -1
index b026b1235079..f9d14115c416 100755
@@ -4311,7 +4311,7 @@ sub process {
 			shift(@ctx);
 			for my $ctx (@ctx) {
 				my ($clen, $cindent) = line_stats($ctx);
-				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
+				if ($ctx =~ /^\+\s*(case\b\s*|default:)/ &&
 							$indent != $cindent) {
 					$err .= "$sep$ctx\n";
 					$sep = '';
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
#2 lore raw

PATCH 1/1 checkpatch: Allow bare SHA-1 hashes in checkpatch commits

In reply to Tiffany Yang

Commit messages for changes to checkpatch may use the output of the "--git" option to illustrate the changed behavior. For example:

        ./scripts/checkpatch.pl --git d1934ed9803c

Don't report an improperly formed commit description when the SHA-1 hash is being used as an example input to the checkpatch script.

---
 scripts/checkpatch.pl | 1 +
 1 file changed, 1 insertion(+)
scripts/checkpatch.pl +1 -0
index f9d14115c416..c81787538b35 100755
@@ -3358,6 +3358,7 @@ sub process {
 		    $in_commit_log && !$commit_log_possible_stack_dump &&
 		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
 		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
+		    $line !~ /^\s*(?:(\$|%)\s+)?\.\/scripts\/checkpatch\.pl\s(?:-g|--git)\s[0-9a-f]{5,40}/ &&
 		    (($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
 		      ($line =~ /\bcommit\s*$/i && defined($rawlines[$linenr]) && $rawlines[$linenr] =~ /^\s*[0-9a-f]{5,}\b/i)) ||
 		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog