Skip to contentSkip to conversation

checkpatch: add warning for unnecessary use of %h[xudi] and %hh[xudi]

PATCH Superseded Dwaipayan Ray 2 participants 2 messages Nov 26, 2020 Checkpatch

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 checkpatch: add warning for unnecessary use of %h[xudi] and %hh[xudi]

Modifiers %h and %hh should never be used.

Commit cbacb5ab0aa0 ("docs: printk-formats: Stop encouraging use of unnecessary %h[xudi] and %hh[xudi]") specifies that:

"Standard integer promotion is already done and %hx and %hhx is useless so do not encourage the use of %hh[xudi] or %h[xudi]."

"The "h" and "hh" things should never be used. The only reason for them being used if you have an "int", but you want to print it out as a "char" (and honestly, that is a really bad reason, you'd be better off just using a proper cast to make the code more obvious)."

Add a new check to emit a warning on finding an unneeded use of %h or %hh modifier.

---
Changes in v2:
- Use $logFunctions instead of the manual list.
- Relocate the check to after logging continuations check.
- Remove perl_version_ok condition which is unneeded here.

 scripts/checkpatch.pl | 12 ++++++++++++
 1 file changed, 12 insertions(+)
scripts/checkpatch.pl +12 -0
index 7dc094445d83..d1ecb852f384 100755
@@ -6027,6 +6027,18 @@ sub process {
 			     "Avoid logging continuation uses where feasible\n" . $herecurr);
 		}
 
+# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
+		if (defined $stat &&
+		    $line =~ /\b$logFunctions\s*\(/) {
+			my $lc = $stat =~ tr@\n@@;
+			$lc = $lc + $linenr;
+			my $stat_real = get_stat_real($linenr, $lc);
+			if ($stat_real =~ /\"[^\"]*%[\d\.\*\-]*h+[idux].*\"/i) {
+				WARN("UNNECESSARY_MODIFIER",
+				     "Unnecessary use of modifiers %h[xudi] or %hh[xudi]\n" . "$here\n$stat_real\n");
+			}
+		}
+
 # check for mask then right shift without a parentheses
 		if ($perl_version_ok &&
 		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
Signature · 1 line
2.27.0
maintainer
#1 lore raw
Quoted 18 lines · Dwaipayan Ray

Modifiers %h and %hh should never be used.

Commit cbacb5ab0aa0 ("docs: printk-formats: Stop encouraging use of unnecessary %h[xudi] and %hh[xudi]") specifies that:

"Standard integer promotion is already done and %hx and %hhx is useless so do not encourage the use of %hh[xudi] or %h[xudi]."

"The "h" and "hh" things should never be used. The only reason for them being used if you have an "int", but you want to print it out as a "char" (and honestly, that is a really bad reason, you'd be better off just using a proper cast to make the code more obvious)."

Add a new check to emit a warning on finding an unneeded use of %h or %hh modifier.

Quoted 7 lines

---
Changes in v2:
- Use $logFunctions instead of the manual list.
- Relocate the check to after logging continuations check.
- Remove perl_version_ok condition which is unneeded here.

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

scripts/checkpatch.pl sub process { · line 6038

Quoted 13 lines
scripts/checkpatch.pl +9 -0
@@ -6027,6 +6027,18 @@ sub process {
  			     "Avoid logging continuation uses where feasible\n" . $herecurr);
  		}
 
+# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
+		if (defined $stat &&
+		    $line =~ /\b$logFunctions\s*\(/) {
+			my $lc = $stat =~ tr@\n@@;
+			$lc = $lc + $linenr;
+			my $stat_real = get_stat_real($linenr, $lc);
+			if ($stat_real =~ /\"[^\"]*%[\d\.\*\-]*h+[idux].*\"/i) {
+				WARN("UNNECESSARY_MODIFIER",
+				     "Unnecessary use of modifiers %h[xudi] or %hh[xudi]\n" . "$here\n$stat_real\n");

o Why not capture the group and show the actual integer format portion here? (%[\d\.\*\-]*h+[idux])
o This might also show every unnecessary use of %h. (using while (.../g)
o The .*\" isn't useful.
o The /i is probably wrong.
o Perhaps this could use a --fix line if the format is on the same line as $logFunctions
o Perhaps the message should say something about integer promotion. Maybe something like:
"Integer promotion: using 'h' in '$1' is unnecessary\n"