Skip to content

Commit 7d595c0

Browse files
committed
fix(fe): fix fallthrough warning after returning switch
1 parent eb5f54d commit 7d595c0

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

docs/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ Semantic Versioning.
6868
* `for await (async of []);` no longer falsely reports [E0082][] ("assigning to
6969
'async' in a for-of loop requires parentheses"). (`for (async of []);` still
7070
reports the diagnostic.)
71+
* A `return` statement inside a nested `switch` no longer falsely repots
72+
[E0427][] ("missing 'break;' or '// fallthrough' comment between " "statement
73+
and 'case'").
7174
* TypeScript support (still experimental):
7275
* Types named `await`, `implements`, `interface`, `let`, `package`, `private`,
7376
`protected`, `public`, `static`, and `yield` are now recognized in type

src/quick-lint-js/fe/parse-statement.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3071,12 +3071,15 @@ void Parser::parse_and_visit_switch(Parse_Visitor_Base &v) {
30713071
case Token_Type::kw_throw:
30723072
case Token_Type::kw_break:
30733073
case Token_Type::kw_case:
3074+
30743075
// Temporarily return true to omit diag with these statments
3076+
case Token_Type::kw_do:
3077+
case Token_Type::kw_for:
30753078
case Token_Type::kw_if:
3079+
case Token_Type::kw_switch:
30763080
case Token_Type::kw_try:
30773081
case Token_Type::kw_while:
3078-
case Token_Type::kw_do:
3079-
case Token_Type::kw_for:
3082+
case Token_Type::kw_with:
30803083
case Token_Type::left_curly:
30813084
return true;
30823085
default:

test/test-parse-warning.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,30 @@ TEST_F(Test_Parse_Warning, Diag_Fallthrough_Without_Comment_In_Switch) {
485485
default:})"_sv,
486486
no_diags);
487487
}
488+
489+
TEST_F(Test_Parse_Warning, early_exit_does_not_trigger_fallthrough_warning) {
490+
for (String8_View exiting_code : {
491+
u8"break;"_sv,
492+
u8"continue;"_sv,
493+
u8"do { return; } while (true);"_sv,
494+
u8"for (;;) { return; }"_sv,
495+
u8"if (false) { } else { return; }"_sv,
496+
u8"if (true) { return; }"_sv,
497+
u8"return x;"_sv,
498+
u8"return;"_sv,
499+
u8"switch (c) { default: return; }"_sv,
500+
u8"throw err;"_sv,
501+
u8"try { return; } catch (e) {}"_sv,
502+
u8"while (true) { return; }"_sv,
503+
u8"with (o) { return; }"_sv,
504+
u8"{ return; }"_sv,
505+
}) {
506+
test_parse_and_visit_statement(
507+
concat(u8"for (;;) { switch (a) { case 1: "_sv, exiting_code,
508+
u8" default: break; } }"_sv),
509+
no_diags);
510+
}
511+
}
488512
}
489513
}
490514

0 commit comments

Comments
 (0)