Skip to contentSkip to conversation

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

RFC PATCH In review Dwaipayan Ray 2 participants 3 messages Nov 25, 2020 – Nov 26, 2020 Checkpatch

lore
Share & tools
lore
#0 lore raw

RFC PATCH 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.

---
 scripts/checkpatch.pl | 13 +++++++++++++
 1 file changed, 13 insertions(+)
scripts/checkpatch.pl +13 -0
index 7dc094445d83..47c1017a9973 100755
@@ -6570,6 +6570,19 @@ sub process {
 			}
 		}
 
+# check for unnecessary use of %h[xudi] and %hh[xudi]
+		if ($perl_version_ok &&
+		    defined $stat &&
+		    $line =~ /(?:[sd]?print[kfd]|(?:pr_|drm_)(?:info|debug|warn|error))/i) {
+			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 length modifiers %h[xudi] or %hh[xudi]\n" . "$here\n$stat_real\n");
+			}
+		}
+
 # check for naked sscanf
 		if ($perl_version_ok &&
 		    defined $stat &&
Signature · 6 lines
2.27.0

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#1 lore raw
Quoted 8 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]."

Quoted 8 lines

"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.

Looks good. You might want to add the link to the original email discussion on lore.kernel.org in the commit message with Link:

Lukas

Quoted 30 lines
---
 scripts/checkpatch.pl | 13 +++++++++++++
 1 file changed, 13 insertions(+)
scripts/checkpatch.pl +13 -0
index 7dc094445d83..47c1017a9973 100755
@@ -6570,6 +6570,19 @@ sub process {
                        }
                }
 
+# check for unnecessary use of %h[xudi] and %hh[xudi]
+               if ($perl_version_ok &&
+                   defined $stat &&
+                   $line =~
 /(?:[sd]?print[kfd]|(?:pr_|drm_)(?:info|debug|warn|error))/i) {
+                       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 length modifiers
 %h[xudi] or %hh[xudi]\n" . "$here\n$stat_real\n");
+                       }
+               }
+
Quoted 7 lines
 # check for naked sscanf
                if ($perl_version_ok &&
                    defined $stat &&
--
2.27.0

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#2 lore raw
Quoted 18 lines · Lukas Bulwahn
Quoted 14 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)."

Quoted 7 lines
Quoted 2 lines

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

Looks good. You might want to add the link to the original email discussion on lore.kernel.org in the commit message with Link:

Lukas

Thanks, I will do that and send it off then.

Regards,
Dwaipayan.
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees