diff --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp index 870402588e763..d4450b31e5f38 100644 --- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp +++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp @@ -191,6 +191,26 @@ o]](); "change 'fod' to 'foo'"))))); } +// Verify that the -Wswitch case-not-covered diagnostic range covers the +// whole expression. This is important because the "populate-switch" tweak +// fires for the full expression range (see tweaks/PopulateSwitchTests.cpp). +// The quickfix flow only works end-to-end if the tweak can be triggered on +// the diagnostic's range. +TEST(DiagnosticsTest, WSwitch) { + Annotations Test(R"cpp( + enum A { X }; + struct B { A a; }; + void foo(B b) { + switch ([[b.a]]) {} + } + )cpp"); + auto TU = TestTU::withCode(Test.code()); + TU.ExtraArgs = {"-Wswitch"}; + EXPECT_THAT(*TU.build().getDiagnostics(), + ElementsAre(Diag(Test.range(), + "enumeration value 'X' not handled in switch"))); +} + TEST(DiagnosticsTest, FlagsMatter) { Annotations Test("[[void]] main() {} // error-ok"); auto TU = TestTU::withCode(Test.code()); diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp index 3baccec2d7bb4..f4f7e353a2c17 100644 --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -1563,7 +1563,7 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch, auto DB = Diag(CondExpr->getExprLoc(), TheDefaultStmt ? diag::warn_def_missing_case : diag::warn_missing_case) - << (int)UnhandledNames.size(); + << CondExpr->getSourceRange() << (int)UnhandledNames.size(); for (size_t I = 0, E = std::min(UnhandledNames.size(), (size_t)3); I != E; ++I)