Skip to contentSkip to conversation

checkpatch: Improve TYPECAST_INT_CONSTANT test message

PATCH Applied Joe Perches 2 participants 2 messages Jan 18, 2021 3 messages not in the archive Checkpatch

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

Joe Perches

author maintainer
#3 lore raw

PATCH 1/1 checkpatch: Improve TYPECAST_INT_CONSTANT test message

Improve the TYPECAST_INT_CONSTANT test by showing the suggested conversion for various type of uses like (unsigned int)1 to 1U.

---

Douglas Gilbert sent me a private email (and in that email said he 'loves to hate checkpatch' ;) complaining that checkpatch warned on the use of the cast of '(unsigned int)1' so make it more obvious why the message is emitted by always showing the suggested conversion.

 scripts/checkpatch.pl | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
scripts/checkpatch.pl +10 -10
index 016115a62a9f..4f8494527139 100755
@@ -6527,18 +6527,18 @@ sub process {
 		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
 			my $cast = $1;
 			my $const = $2;
+			my $suffix = "";
+			my $newconst = $const;
+			$newconst =~ s/${Int_type}$//;
+			$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
+			if ($cast =~ /\blong\s+long\b/) {
+			    $suffix .= 'LL';
+			} elsif ($cast =~ /\blong\b/) {
+			    $suffix .= 'L';
+			}
 			if (WARN("TYPECAST_INT_CONSTANT",
-				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
+				 "Unnecessary typecast of c90 int constant - '$cast$const' could be '$const$suffix'\n" . $herecurr) &&
 			    $fix) {
-				my $suffix = "";
-				my $newconst = $const;
-				$newconst =~ s/${Int_type}$//;
-				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
-				if ($cast =~ /\blong\s+long\b/) {
-					$suffix .= 'LL';
-				} elsif ($cast =~ /\blong\b/) {
-					$suffix .= 'L';
-				}
 				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
 			}
 		}
#4 lore raw
Quoted 2 lines · Joe Perches

Improve the TYPECAST_INT_CONSTANT test by showing the suggested conversion for various type of uses like (unsigned int)1 to 1U.

The questionable code snippet was:

        unsigned int nent, nalloc;

        ....
        if (check_add_overflow(nent, (unsigned int)1, &nalloc))

where the check_add_overflow() macro [include/linux/overflow.h] uses typeid to check its first and second arguments have the same type. So it is likely others could meet this issue.

Doug Gilbert

Quoted 46 lines

---

Douglas Gilbert sent me a private email (and in that email said he 'loves to hate checkpatch' ;) complaining that checkpatch warned on the use of the cast of '(unsigned int)1' so make it more obvious why the message is emitted by always showing the suggested conversion.

  scripts/checkpatch.pl | 20 ++++++++++----------
  1 file changed, 10 insertions(+), 10 deletions(-)
scripts/checkpatch.pl +10 -10
index 016115a62a9f..4f8494527139 100755
@@ -6527,18 +6527,18 @@ sub process {
  		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
  			my $cast = $1;
  			my $const = $2;
+			my $suffix = "";
+			my $newconst = $const;
+			$newconst =~ s/${Int_type}$//;
+			$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
+			if ($cast =~ /\blong\s+long\b/) {
+			    $suffix .= 'LL';
+			} elsif ($cast =~ /\blong\b/) {
+			    $suffix .= 'L';
+			}
  			if (WARN("TYPECAST_INT_CONSTANT",
-				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
+				 "Unnecessary typecast of c90 int constant - '$cast$const' could be '$const$suffix'\n" . $herecurr) &&
  			    $fix) {
-				my $suffix = "";
-				my $newconst = $const;
-				$newconst =~ s/${Int_type}$//;
-				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
-				if ($cast =~ /\blong\s+long\b/) {
-					$suffix .= 'LL';
-				} elsif ($cast =~ /\blong\b/) {
-					$suffix .= 'L';
-				}
  				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
  			}
  		}