// If the if statement is the last statement its enclosing statements
// scope, we can pull the decl out of the if statement.
DiagnosticBuilder Diag = diag(ElseLoc, WarningMessage)
- << ControlFlowInterruptor;
+ << ControlFlowInterruptor
+ << SourceRange(ElseLoc);
if (checkInitDeclUsageInElse(If) != nullptr) {
Diag << tooling::fixit::createReplacement(
SourceRange(If->getIfLoc()),
// If the if statement is the last statement its enclosing statements
// scope, we can pull the decl out of the if statement.
DiagnosticBuilder Diag = diag(ElseLoc, WarningMessage)
- << ControlFlowInterruptor;
+ << ControlFlowInterruptor
+ << SourceRange(ElseLoc);
Diag << tooling::fixit::createReplacement(
SourceRange(If->getIfLoc()),
(tooling::fixit::getText(*If->getInit(), *Result.Context) +
}
DiagnosticBuilder Diag = diag(ElseLoc, WarningMessage)
- << ControlFlowInterruptor;
+ << ControlFlowInterruptor << SourceRange(ElseLoc);
removeElseAndBrackets(Diag, *Result.Context, Else, ElseLoc);
}
EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre()); // no-crash
}
+TEST(DiagnosticTest, ElseAfterReturnRange) {
+ Annotations Main(R"cpp(
+ int foo(int cond) {
+ if (cond == 1) {
+ return 42;
+ } [[else]] if (cond == 2) {
+ return 43;
+ }
+ return 44;
+ }
+ )cpp");
+ TestTU TU = TestTU::withCode(Main.code());
+ TU.ClangTidyProvider = addTidyChecks("llvm-else-after-return");
+ EXPECT_THAT(
+ TU.build().getDiagnostics(),
+ ElementsAre(Diag(Main.range(), "do not use 'else' after 'return'")));
+}
+
TEST(DiagnosticsTest, Preprocessor) {
// This looks like a preamble, but there's an #else in the middle!
// Check that: