From: Joe Perches <joe@perches.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Whitcroft <apw@shadowen.org>, Jiri Kosina <jkosina@suse.cz>,
LKML <linux-kernel@vger.kernel.org>,
Benjamin Tissoires <benjamin.tissoires@redhat.com>,
Dave Jones <davej@redhat.com>
Subject: [PATCH] checkpatch: Attempt to find missing switch/case break;
Date: Fri, 13 Dec 2013 11:06:20 -0800 [thread overview]
Message-ID: <1386961580.2479.42.camel@joe-AO722> (raw)
switch case statements missing a break statement are an unfortunately
common error.
e.g.:
commit 4a2c94c9b6c0 ("HID: kye: Add report fixup for Genius Manticore Keyboard")
case blocks should end in a break/return/goto/continue.
If a fall-through is used, it should have a comment showing
that it is intentional. Ideally that comment should be
something like: "/* fall-through */"
Add a test to look for missing break statements.
This looks only at the context lines before an inserted case
so it's possible to have false positives when the context
contains a close brace and the break is before the brace
and not part of the patch context.
Looking at recent patches, this is a pretty rare occurrence.
The normal kernel style uses a break as the last line of the
previous block.
Signed-off-by: Joe Perches <joe@perche.com>
---
Also tested on all files in drivers/hid/
There is a hit for drivers/hid/hid-microsoft.c
I've no idea what's right here but adding a break or return
or a "fall-through" comment would be nicer.
static int ms_ergonomy_kb_quirk(struct hid_input *hi, struct hid_usage *usage,
unsigned long **bit, int *max)
{
struct input_dev *input = hi->input;
switch (usage->hid & HID_USAGE) {
case 0xfd06: ms_map_key_clear(KEY_CHAT); break;
case 0xfd07: ms_map_key_clear(KEY_PHONE); break;
case 0xff05:
set_bit(EV_REP, input->evbit);
ms_map_key_clear(KEY_F13);
set_bit(KEY_F14, input->keybit);
set_bit(KEY_F15, input->keybit);
set_bit(KEY_F16, input->keybit);
set_bit(KEY_F17, input->keybit);
set_bit(KEY_F18, input->keybit);
default:
return 0;
}
return 1;
}
scripts/checkpatch.pl | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 38be5d5..94225d2 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -4132,6 +4132,31 @@ sub process {
}
}
+# check for case / default statements not preceeded by break/fallthrough/switch
+ if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
+ my $has_break = 0;
+ my $has_statement = 0;
+ my $count = 0;
+ my $prevline = $linenr;
+ while ($prevline > 1 && $count < 3 && !$has_break) {
+ $prevline--;
+ my $rline = $rawlines[$prevline - 1];
+ my $fline = $lines[$prevline - 1];
+ last if ($fline =~ /^\@\@/);
+ next if ($fline =~ /^\-/);
+ next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
+ $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
+ next if ($fline =~ /^.[\s$;]*$/);
+ $has_statement = 1;
+ $count++;
+ $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
+ }
+ if (!$has_break && $has_statement) {
+ WARN("MISSING_BREAK",
+ "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
+ }
+ }
+
# check for switch/default statements without a break;
if ($^V && $^V ge 5.10.0 &&
defined $stat &&
next reply other threads:[~2013-12-13 19:06 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-12-13 19:06 Joe Perches [this message]
2013-12-13 21:00 ` Jiri Kosina
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=1386961580.2479.42.camel@joe-AO722 \
--to=joe@perches.com \
--cc=akpm@linux-foundation.org \
--cc=apw@shadowen.org \
--cc=benjamin.tissoires@redhat.com \
--cc=davej@redhat.com \
--cc=jkosina@suse.cz \
--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®