diff --git a/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp b/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp --- a/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp @@ -10,6 +10,7 @@ #include "../utils/Matchers.h" #include "../utils/OptionsUtils.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/ExprConcepts.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Basic/LLVM.h" #include "clang/Basic/SourceLocation.h" @@ -440,6 +441,8 @@ return Node.isIntegerConstantExpr(Finder->getASTContext()); } +AST_MATCHER(Expr, isRequiresExpr) { return isa(Node); } + AST_MATCHER(BinaryOperator, operandsAreEquivalent) { return areEquivalentExpr(Node.getLHS(), Node.getRHS()); } @@ -858,6 +861,7 @@ const auto BannedIntegerLiteral = integerLiteral(expandedByMacro(KnownBannedMacroNames)); + const auto BannedAncestor = expr(isRequiresExpr()); // Binary with equivalent operands, like (X != 2 && X != 2). Finder->addMatcher( @@ -873,7 +877,8 @@ unless(hasType(realFloatingPointType())), unless(hasEitherOperand(hasType(realFloatingPointType()))), unless(hasLHS(AnyLiteralExpr)), - unless(hasDescendant(BannedIntegerLiteral))) + unless(hasDescendant(BannedIntegerLiteral)), + unless(hasAncestor(BannedAncestor))) .bind("binary")), this); @@ -886,7 +891,8 @@ unless(isInTemplateInstantiation()), unless(binaryOperatorIsInMacro()), // TODO: if the banned macros are themselves duplicated - unless(hasDescendant(BannedIntegerLiteral))) + unless(hasDescendant(BannedIntegerLiteral)), + unless(hasAncestor(BannedAncestor))) .bind("nested-duplicates"), this); @@ -896,7 +902,8 @@ conditionalOperator(expressionsAreEquivalent(), // Filter noisy false positives. unless(conditionalOperatorIsInMacro()), - unless(isInTemplateInstantiation())) + unless(isInTemplateInstantiation()), + unless(hasAncestor(BannedAncestor))) .bind("cond")), this); @@ -909,7 +916,8 @@ ">=", "&&", "||", "="), parametersAreEquivalent(), // Filter noisy false positives. - unless(isMacro()), unless(isInTemplateInstantiation())) + unless(isMacro()), unless(isInTemplateInstantiation()), + unless(hasAncestor(BannedAncestor))) .bind("call")), this); @@ -919,7 +927,8 @@ hasAnyOverloadedOperatorName("|", "&", "||", "&&", "^"), nestedParametersAreEquivalent(), argumentCountIs(2), // Filter noisy false positives. - unless(isMacro()), unless(isInTemplateInstantiation())) + unless(isMacro()), unless(isInTemplateInstantiation()), + unless(hasAncestor(BannedAncestor))) .bind("nested-duplicates"), this); @@ -936,7 +945,8 @@ binaryOperator(hasAnyOperatorName("|", "&")), integerLiteral())), hasRHS(integerLiteral()))))) - .bind("logical-bitwise-confusion")))), + .bind("logical-bitwise-confusion")), + unless(hasAncestor(BannedAncestor)))), this); // Match expressions like: (X << 8) & 0xFF @@ -949,7 +959,8 @@ hasRHS(ignoringParenImpCasts( integerLiteral().bind("shift-const"))))), ignoringParenImpCasts( - integerLiteral().bind("and-const")))) + integerLiteral().bind("and-const"))), + unless(hasAncestor(BannedAncestor))) .bind("left-right-shift-confusion")), this); @@ -967,7 +978,8 @@ // Match expressions like: x 0xFF == 0xF00. Finder->addMatcher( traverse(TK_AsIs, binaryOperator(isComparisonOperator(), - hasOperands(BinOpCstLeft, CstRight)) + hasOperands(BinOpCstLeft, CstRight), + unless(hasAncestor(BannedAncestor))) .bind("binop-const-compare-to-const")), this); @@ -977,7 +989,8 @@ TK_AsIs, binaryOperator(isComparisonOperator(), anyOf(allOf(hasLHS(BinOpCstLeft), hasRHS(SymRight)), - allOf(hasLHS(SymRight), hasRHS(BinOpCstLeft)))) + allOf(hasLHS(SymRight), hasRHS(BinOpCstLeft))), + unless(hasAncestor(BannedAncestor))) .bind("binop-const-compare-to-sym")), this); @@ -987,7 +1000,8 @@ binaryOperator(isComparisonOperator(), hasLHS(BinOpCstLeft), hasRHS(BinOpCstRight), // Already reported as redundant. - unless(operandsAreEquivalent())) + unless(operandsAreEquivalent()), + unless(hasAncestor(BannedAncestor))) .bind("binop-const-compare-to-binop-const")), this); @@ -1003,7 +1017,8 @@ binaryOperator(hasAnyOperatorName("||", "&&"), hasLHS(ComparisonLeft), hasRHS(ComparisonRight), // Already reported as redundant. - unless(operandsAreEquivalent())) + unless(operandsAreEquivalent()), + unless(hasAncestor(BannedAncestor))) .bind("comparisons-of-symbol-and-const")), this); } diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -149,6 +149,12 @@ copy assignment operators with nonstandard return types. The check is restricted to c++11-or-later. +- Improved :doc:`misc-redundant-expression ` + check. + + The check now skips concept definitions since redundant expressions still make sense + inside them. + Removed checks ^^^^^^^^^^^^^^ diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression-cxx20.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression-cxx20.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression-cxx20.cpp @@ -0,0 +1,11 @@ +// RUN: clang-tidy %s -checks=-*,misc-redundant-expression -- -std=c++20 | count 0 + +namespace concepts { +// redundant expressions inside concepts make sense, ignore them +template +concept TestConcept = requires(I i) { + {i - i}; + {i && i}; + {i ? i : i}; +}; +} // namespace concepts