Skip to content

Commit 0cecb42

Browse files
committed
[Sema] Include full range of the switch condition in -Wswitch diagnostic
1 parent 6d5b14d commit 0cecb42

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,26 @@ o]]();
191191
"change 'fod' to 'foo'")))));
192192
}
193193

194+
// Verify that the -Wswitch case-not-covered diagnostic range covers the
195+
// whole expression. This is important because the "populate-switch" tweak
196+
// fires for the full expression range (see tweaks/PopulateSwitchTests.cpp).
197+
// The quickfix flow only works end-to-end if the tweak can be triggered on
198+
// the diagnostic's range.
199+
TEST(DiagnosticsTest, WSwitch) {
200+
Annotations Test(R"cpp(
201+
enum A { X };
202+
struct B { A a; };
203+
void foo(B b) {
204+
switch ([[b.a]]) {}
205+
}
206+
)cpp");
207+
auto TU = TestTU::withCode(Test.code());
208+
TU.ExtraArgs = {"-Wswitch"};
209+
EXPECT_THAT(*TU.build().getDiagnostics(),
210+
ElementsAre(Diag(Test.range(),
211+
"enumeration value 'X' not handled in switch")));
212+
}
213+
194214
TEST(DiagnosticsTest, FlagsMatter) {
195215
Annotations Test("[[void]] main() {} // error-ok");
196216
auto TU = TestTU::withCode(Test.code());

clang/lib/Sema/SemaStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,7 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
15631563
auto DB = Diag(CondExpr->getExprLoc(), TheDefaultStmt
15641564
? diag::warn_def_missing_case
15651565
: diag::warn_missing_case)
1566-
<< (int)UnhandledNames.size();
1566+
<< CondExpr->getSourceRange() << (int)UnhandledNames.size();
15671567

15681568
for (size_t I = 0, E = std::min(UnhandledNames.size(), (size_t)3);
15691569
I != E; ++I)

0 commit comments

Comments
 (0)