Skip to content

Commit e697943

Browse files
authored
[clang-tidy] improve messages when auto-fix does not work (#96917)
Fixes: #93157
1 parent 7ae4b8e commit e697943

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "SimplifyBooleanExprCheck.h"
1010
#include "clang/AST/Expr.h"
1111
#include "clang/AST/RecursiveASTVisitor.h"
12+
#include "clang/Basic/DiagnosticIDs.h"
1213
#include "clang/Lex/Lexer.h"
1314
#include "llvm/Support/SaveAndRestore.h"
1415

@@ -702,7 +703,8 @@ bool SimplifyBooleanExprCheck::canBeBypassed(const Stmt *S) const {
702703
return IgnoreMacros && S->getBeginLoc().isMacroID();
703704
}
704705

705-
void SimplifyBooleanExprCheck::issueDiag(const ASTContext &Context,
706+
/// @brief return true when replacement created.
707+
bool SimplifyBooleanExprCheck::issueDiag(const ASTContext &Context,
706708
SourceLocation Loc,
707709
StringRef Description,
708710
SourceRange ReplacementRange,
@@ -712,8 +714,10 @@ void SimplifyBooleanExprCheck::issueDiag(const ASTContext &Context,
712714
Context.getSourceManager(), getLangOpts());
713715

714716
DiagnosticBuilder Diag = diag(Loc, Description);
715-
if (!containsDiscardedTokens(Context, CharRange))
717+
const bool HasReplacement = !containsDiscardedTokens(Context, CharRange);
718+
if (HasReplacement)
716719
Diag << FixItHint::CreateReplacement(CharRange, Replacement);
720+
return HasReplacement;
717721
}
718722

719723
void SimplifyBooleanExprCheck::replaceWithThenStatement(
@@ -751,18 +755,42 @@ void SimplifyBooleanExprCheck::replaceWithReturnCondition(
751755
replacementExpression(Context, Negated, If->getCond());
752756
std::string Replacement = ("return " + Condition + Terminator).str();
753757
SourceLocation Start = BoolLiteral->getBeginLoc();
754-
issueDiag(Context, Start, SimplifyConditionalReturnDiagnostic,
755-
If->getSourceRange(), Replacement);
758+
759+
const bool HasReplacement =
760+
issueDiag(Context, Start, SimplifyConditionalReturnDiagnostic,
761+
If->getSourceRange(), Replacement);
762+
763+
if (!HasReplacement) {
764+
const SourceRange ConditionRange = If->getCond()->getSourceRange();
765+
if (ConditionRange.isValid())
766+
diag(ConditionRange.getBegin(), "conditions that can be simplified",
767+
DiagnosticIDs::Note)
768+
<< ConditionRange;
769+
}
756770
}
757771

758772
void SimplifyBooleanExprCheck::replaceCompoundReturnWithCondition(
759773
const ASTContext &Context, const ReturnStmt *Ret, bool Negated,
760774
const IfStmt *If, const Expr *ThenReturn) {
761775
const std::string Replacement =
762776
"return " + replacementExpression(Context, Negated, If->getCond());
763-
issueDiag(Context, ThenReturn->getBeginLoc(),
764-
SimplifyConditionalReturnDiagnostic,
765-
SourceRange(If->getBeginLoc(), Ret->getEndLoc()), Replacement);
777+
778+
const bool HasReplacement = issueDiag(
779+
Context, ThenReturn->getBeginLoc(), SimplifyConditionalReturnDiagnostic,
780+
SourceRange(If->getBeginLoc(), Ret->getEndLoc()), Replacement);
781+
782+
if (!HasReplacement) {
783+
const SourceRange ConditionRange = If->getCond()->getSourceRange();
784+
if (ConditionRange.isValid())
785+
diag(ConditionRange.getBegin(), "conditions that can be simplified",
786+
DiagnosticIDs::Note)
787+
<< ConditionRange;
788+
const SourceRange ReturnRange = Ret->getSourceRange();
789+
if (ReturnRange.isValid())
790+
diag(ReturnRange.getBegin(), "return statement that can be simplified",
791+
DiagnosticIDs::Note)
792+
<< ReturnRange;
793+
}
766794
}
767795

768796
void SimplifyBooleanExprCheck::replaceWithAssignment(const ASTContext &Context,

clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class SimplifyBooleanExprCheck : public ClangTidyCheck {
6060
const BinaryOperator *Inner, bool TryOfferFix,
6161
const Stmt *Parent, const ParenExpr *Parens);
6262

63-
void issueDiag(const ASTContext &Context, SourceLocation Loc,
63+
bool issueDiag(const ASTContext &Context, SourceLocation Loc,
6464
StringRef Description, SourceRange ReplacementRange,
6565
StringRef Replacement);
6666

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,8 @@ Changes in existing checks
455455

456456
- Improved :doc:`readability-simplify-boolean-expr
457457
<clang-tidy/checks/readability/simplify-boolean-expr>` check to avoid to emit
458-
warning for macro when IgnoreMacro option is enabled.
458+
warning for macro when IgnoreMacro option is enabled and improve messages
459+
when auto-fix does not work.
459460

460461
- Improved :doc:`readability-static-definition-in-anonymous-namespace
461462
<clang-tidy/checks/readability/static-definition-in-anonymous-namespace>`

clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,23 @@ bool conditional_return_statements(int i) {
353353
// CHECK-FIXES: {{^}} return i == 0;{{$}}
354354
// CHECK-FIXES-NEXT: {{^}$}}
355355

356+
bool conditional_return_statements_no_fix_1(int i) {
357+
if (i == 0) return true;
358+
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: {{.*}} in conditional return statement
359+
// CHECK-MESSAGES: :[[@LINE-2]]:7: note: conditions that can be simplified
360+
// comment
361+
return false;
362+
// CHECK-MESSAGES: :[[@LINE-1]]:3: note: return statement that can be simplified
363+
}
364+
365+
bool conditional_return_statements_no_fix_2(int i) {
366+
if (i == 0) return true;
367+
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: {{.*}} in conditional return statement
368+
// CHECK-MESSAGES: :[[@LINE-2]]:7: note: conditions that can be simplified
369+
// comment
370+
else return false;
371+
}
372+
356373
bool conditional_return_statements_then_expr(int i, int j) {
357374
if (i == j) return (i == 0); else return false;
358375
}

0 commit comments

Comments
 (0)