9
9
#include " SimplifyBooleanExprCheck.h"
10
10
#include " clang/AST/Expr.h"
11
11
#include " clang/AST/RecursiveASTVisitor.h"
12
+ #include " clang/Basic/DiagnosticIDs.h"
12
13
#include " clang/Lex/Lexer.h"
13
14
#include " llvm/Support/SaveAndRestore.h"
14
15
@@ -702,7 +703,8 @@ bool SimplifyBooleanExprCheck::canBeBypassed(const Stmt *S) const {
702
703
return IgnoreMacros && S->getBeginLoc ().isMacroID ();
703
704
}
704
705
705
- void SimplifyBooleanExprCheck::issueDiag (const ASTContext &Context,
706
+ // / @brief return true when replacement created.
707
+ bool SimplifyBooleanExprCheck::issueDiag (const ASTContext &Context,
706
708
SourceLocation Loc,
707
709
StringRef Description,
708
710
SourceRange ReplacementRange,
@@ -712,8 +714,10 @@ void SimplifyBooleanExprCheck::issueDiag(const ASTContext &Context,
712
714
Context.getSourceManager (), getLangOpts ());
713
715
714
716
DiagnosticBuilder Diag = diag (Loc, Description);
715
- if (!containsDiscardedTokens (Context, CharRange))
717
+ const bool HasReplacement = !containsDiscardedTokens (Context, CharRange);
718
+ if (HasReplacement)
716
719
Diag << FixItHint::CreateReplacement (CharRange, Replacement);
720
+ return HasReplacement;
717
721
}
718
722
719
723
void SimplifyBooleanExprCheck::replaceWithThenStatement (
@@ -751,18 +755,42 @@ void SimplifyBooleanExprCheck::replaceWithReturnCondition(
751
755
replacementExpression (Context, Negated, If->getCond ());
752
756
std::string Replacement = (" return " + Condition + Terminator).str ();
753
757
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
+ }
756
770
}
757
771
758
772
void SimplifyBooleanExprCheck::replaceCompoundReturnWithCondition (
759
773
const ASTContext &Context, const ReturnStmt *Ret, bool Negated,
760
774
const IfStmt *If, const Expr *ThenReturn) {
761
775
const std::string Replacement =
762
776
" 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
+ }
766
794
}
767
795
768
796
void SimplifyBooleanExprCheck::replaceWithAssignment (const ASTContext &Context,
0 commit comments