Skip to content

[clang-tidy] improve messages when auto-fix does not work #96917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "SimplifyBooleanExprCheck.h"
#include "clang/AST/Expr.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/DiagnosticIDs.h"
#include "clang/Lex/Lexer.h"
#include "llvm/Support/SaveAndRestore.h"

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

void SimplifyBooleanExprCheck::issueDiag(const ASTContext &Context,
/// @brief return true when replacement created.
bool SimplifyBooleanExprCheck::issueDiag(const ASTContext &Context,
SourceLocation Loc,
StringRef Description,
SourceRange ReplacementRange,
Expand All @@ -712,8 +714,10 @@ void SimplifyBooleanExprCheck::issueDiag(const ASTContext &Context,
Context.getSourceManager(), getLangOpts());

DiagnosticBuilder Diag = diag(Loc, Description);
if (!containsDiscardedTokens(Context, CharRange))
const bool HasReplacement = !containsDiscardedTokens(Context, CharRange);
if (HasReplacement)
Diag << FixItHint::CreateReplacement(CharRange, Replacement);
return HasReplacement;
}

void SimplifyBooleanExprCheck::replaceWithThenStatement(
Expand Down Expand Up @@ -751,18 +755,42 @@ void SimplifyBooleanExprCheck::replaceWithReturnCondition(
replacementExpression(Context, Negated, If->getCond());
std::string Replacement = ("return " + Condition + Terminator).str();
SourceLocation Start = BoolLiteral->getBeginLoc();
issueDiag(Context, Start, SimplifyConditionalReturnDiagnostic,
If->getSourceRange(), Replacement);

const bool HasReplacement =
issueDiag(Context, Start, SimplifyConditionalReturnDiagnostic,
If->getSourceRange(), Replacement);

if (!HasReplacement) {
const SourceRange ConditionRange = If->getCond()->getSourceRange();
if (ConditionRange.isValid())
diag(ConditionRange.getBegin(), "conditions that can be simplified",
DiagnosticIDs::Note)
<< ConditionRange;
}
}

void SimplifyBooleanExprCheck::replaceCompoundReturnWithCondition(
const ASTContext &Context, const ReturnStmt *Ret, bool Negated,
const IfStmt *If, const Expr *ThenReturn) {
const std::string Replacement =
"return " + replacementExpression(Context, Negated, If->getCond());
issueDiag(Context, ThenReturn->getBeginLoc(),
SimplifyConditionalReturnDiagnostic,
SourceRange(If->getBeginLoc(), Ret->getEndLoc()), Replacement);

const bool HasReplacement = issueDiag(
Context, ThenReturn->getBeginLoc(), SimplifyConditionalReturnDiagnostic,
SourceRange(If->getBeginLoc(), Ret->getEndLoc()), Replacement);

if (!HasReplacement) {
const SourceRange ConditionRange = If->getCond()->getSourceRange();
if (ConditionRange.isValid())
diag(ConditionRange.getBegin(), "conditions that can be simplified",
DiagnosticIDs::Note)
<< ConditionRange;
const SourceRange ReturnRange = Ret->getSourceRange();
if (ReturnRange.isValid())
diag(ReturnRange.getBegin(), "return statement that can be simplified",
DiagnosticIDs::Note)
<< ReturnRange;
}
}

void SimplifyBooleanExprCheck::replaceWithAssignment(const ASTContext &Context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class SimplifyBooleanExprCheck : public ClangTidyCheck {
const BinaryOperator *Inner, bool TryOfferFix,
const Stmt *Parent, const ParenExpr *Parens);

void issueDiag(const ASTContext &Context, SourceLocation Loc,
bool issueDiag(const ASTContext &Context, SourceLocation Loc,
StringRef Description, SourceRange ReplacementRange,
StringRef Replacement);

Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,8 @@ Changes in existing checks

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

- Improved :doc:`readability-static-definition-in-anonymous-namespace
<clang-tidy/checks/readability/static-definition-in-anonymous-namespace>`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,23 @@ bool conditional_return_statements(int i) {
// CHECK-FIXES: {{^}} return i == 0;{{$}}
// CHECK-FIXES-NEXT: {{^}$}}

bool conditional_return_statements_no_fix_1(int i) {
if (i == 0) return true;
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: {{.*}} in conditional return statement
// CHECK-MESSAGES: :[[@LINE-2]]:7: note: conditions that can be simplified
// comment
return false;
// CHECK-MESSAGES: :[[@LINE-1]]:3: note: return statement that can be simplified
}

bool conditional_return_statements_no_fix_2(int i) {
if (i == 0) return true;
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: {{.*}} in conditional return statement
// CHECK-MESSAGES: :[[@LINE-2]]:7: note: conditions that can be simplified
// comment
else return false;
}

bool conditional_return_statements_then_expr(int i, int j) {
if (i == j) return (i == 0); else return false;
}
Expand Down
Loading