mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Denis Efremov <efremov@linux.com>, Andy Whitcroft <apw@canonical.com>
Cc: linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH] checkpatch: check for trivial sizeofs
Date: Sat, 30 May 2020 16:10:37 -0700	[thread overview]
Message-ID: <a8d399d956bff24382caeda9a37d85cf3581fa99.camel@perches.com> (raw)
In-Reply-To: <20200530212129.7498-1-efremov@linux.com>

On Sun, 2020-05-31 at 00:21 +0300, Denis Efremov wrote:
> sizeof(char) and its variations in most cases doesn't make code more clear.
> It only makes code wordy.

There are about 1000 of these uses in the kernel.

Not sure I like this though as many/most of the uses
seem _less_ readable with a 1 in their place.

$ git grep -P 'sizeof\s*\(\s*(?:(?:unsigned\s+)?char\s*|(?:__)?u8|u?int8_t|[us]?byte(?:_t)?)\s*\)' | wc -l
970

Try the grep without the wc and see if you agree.

And if this is actually going to be used, I'd prefer
using a separate $typeChar for the search.

Maybe something like:
---
 scripts/checkpatch.pl | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index dd750241958b..de2e9242350b 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -466,6 +466,13 @@ our $typeTypedefs = qr{(?x:
 	$typeKernelTypedefs\b
 )};
 
+our $typeChar = qr{(?x:
+	(?:(?:un)?signed\s+)?char |
+	(?:__)?u8 |
+	(?:u_|u)?int8_t |
+	[us]?byte(?:_t)?
+)};
+
 our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
 
 our $logFunctions = qr{(?x:
@@ -6106,6 +6113,16 @@ sub process {
 			}
 		}
 
+# check for trivial sizeof(char) == 1
+		if ($line =~ /\bsizeof\s*\(\s*($typeChar)\s*\)/) {
+			my $byte = $1;
+			if (CHK("SIZEOF_CHAR",
+				"sizeof($byte) could be the constant 1 instead\n" . $herecurr) &&
+			    $fix) {
+				$fixed[$fixlinenr] =~ s/sizeof\s*\(\s*\Q$byte\E\s*\)/1/;
+			}
+		}
+
 # check for struct spinlock declarations
 		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
 			WARN("USE_SPINLOCK_T",



      reply	other threads:[~2020-05-30 23:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-30 21:21 Denis Efremov
2020-05-30 23:10 ` Joe Perches [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a8d399d956bff24382caeda9a37d85cf3581fa99.camel@perches.com \
    --to=joe@perches.com \
    --cc=apw@canonical.com \
    --cc=efremov@linux.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®