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
719723void 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
758772void 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
768796void SimplifyBooleanExprCheck::replaceWithAssignment (const ASTContext &Context,
0 commit comments