From: Julius Werner <jwerner@chromium.org>
To: Andy Whitcroft <apw@canonical.com>, Joe Perches <joe@perches.com>
Cc: Ivo Sieben <meltedpianoman@gmail.com>,
linux-kernel@vger.kernel.org,
Julius Werner <jwerner@chromium.org>
Subject: [PATCH 3/3] checkpatch: Ignore labels when checking indentation
Date: Thu, 25 Mar 2021 20:50:35 -0700 [thread overview]
Message-ID: <20210326035035.3057800-4-jwerner@chromium.org> (raw)
In-Reply-To: <20210326035035.3057800-1-jwerner@chromium.org>
Goto labels are commonly written in the leftmost column (sometimes with
one space in front), regardless of indentation level. Sometimes they're
on a line of their own, but sometimes the same line is shared with a
normal code statement that then starts at the expected indentation
level. When checking indentation, we should check where that normal
piece of code starts, not where the label starts (there's a separate
INDENTED_LABEL test to check the label itself). Therefore, the
line_stats() function that is used to get indentation level should treat
goto labels like whitespace. The SUSPICIOUS_CODE_INDENT test also needs
to explicitly ignore labels to make sure it doesn't get confused by
them.
Signed-off-by: Julius Werner <jwerner@chromium.org>
---
scripts/checkpatch.pl | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index c1dfc0107be41d..d89367a59e7d37 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -1396,8 +1396,12 @@ sub copy_spacing {
sub line_stats {
my ($line) = @_;
- # Drop the diff line leader and expand tabs
+ # Drop the diff line leader
$line =~ s/^.//;
+
+ # Treat labels like whitespace when counting indentation
+ $line =~ s/^( ?$Ident:)/" " x length($1)/e;
+
$line = expand_tabs($line);
# Pick the indent from the front of the line.
@@ -4197,6 +4201,9 @@ sub process {
# Remove any comments
$s_next =~ s/$;//g;
+ # Remove any leading labels
+ $s_next =~ s/\n( ?$Ident:)/"\n" . " " x length($1)/eg;
+
# Skip this check for in case next statement starts with 'else'
if ($s_next !~ /\s*\belse\b/) {
--
2.29.2
next prev parent reply other threads:[~2021-03-26 3:52 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-26 3:50 [PATCH 0/3] Detect suspicious indentation after conditional Julius Werner
2021-03-26 3:50 ` [PATCH 1/3] checkpatch: ctx_statement_block: Fix preprocessor guard tracking Julius Werner
2021-03-26 3:50 ` [PATCH 2/3] Suspicious indentation detection after conditional statement Julius Werner
2021-03-26 3:50 ` Julius Werner [this message]
2021-04-14 21:18 ` [PATCH 0/3] Detect suspicious indentation after conditional Julius Werner
2021-04-15 23:55 ` Joe Perches
2021-04-30 21:12 ` Julius Werner
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=20210326035035.3057800-4-jwerner@chromium.org \
--to=jwerner@chromium.org \
--cc=apw@canonical.com \
--cc=joe@perches.com \
--cc=linux-kernel@vger.kernel.org \
--cc=meltedpianoman@gmail.com \
/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®