All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] checkpatch: Attempt to find missing switch/case break;
@ 2013-12-13 19:06 Joe Perches
  2013-12-13 21:00 ` Jiri Kosina
  0 siblings, 1 reply; 2+ messages in thread
From: Joe Perches @ 2013-12-13 19:06 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Andy Whitcroft, Jiri Kosina, LKML, Benjamin Tissoires, Dave Jones

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 &&



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] checkpatch: Attempt to find missing switch/case break;
  2013-12-13 19:06 [PATCH] checkpatch: Attempt to find missing switch/case break; Joe Perches
@ 2013-12-13 21:00 ` Jiri Kosina
  0 siblings, 0 replies; 2+ messages in thread
From: Jiri Kosina @ 2013-12-13 21:00 UTC (permalink / raw)
  To: Joe Perches
  Cc: Andrew Morton, Andy Whitcroft, LKML, Benjamin Tissoires, Dave Jones

On Fri, 13 Dec 2013, Joe Perches wrote:

> 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;
> }

This is indeed a bug. I'll fix it with your Reported-by:, thanks.

-- 
Jiri Kosina
SUSE Labs

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-12-13 21:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-12-13 19:06 [PATCH] checkpatch: Attempt to find missing switch/case break; Joe Perches
2013-12-13 21:00 ` Jiri Kosina

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.