Skip to content

Commit 6f02663

Browse files
PiotrZSLrazmser
authored andcommitted
[clang-tidy] Ignore decltype in misc-redundant-expression
Modify check to ignore any parent typeLoc and other unevaluated context. Fixes: llvm#35857 Reviewed By: carlosgalvezp Differential Revision: https://reviews.llvm.org/D157374
1 parent f55100d commit 6f02663

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,6 @@ AST_MATCHER(Expr, isIntegerConstantExpr) {
441441
return Node.isIntegerConstantExpr(Finder->getASTContext());
442442
}
443443

444-
AST_MATCHER(Expr, isRequiresExpr) { return isa<RequiresExpr>(Node); }
445-
446444
AST_MATCHER(BinaryOperator, operandsAreEquivalent) {
447445
return areEquivalentExpr(Node.getLHS(), Node.getRHS());
448446
}
@@ -862,7 +860,8 @@ void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
862860

863861
const auto BannedIntegerLiteral =
864862
integerLiteral(expandedByMacro(KnownBannedMacroNames));
865-
const auto BannedAncestor = expr(isRequiresExpr());
863+
const auto IsInUnevaluatedContext = expr(anyOf(
864+
hasAncestor(expr(hasUnevaluatedContext())), hasAncestor(typeLoc())));
866865

867866
// Binary with equivalent operands, like (X != 2 && X != 2).
868867
Finder->addMatcher(
@@ -879,7 +878,7 @@ void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
879878
unless(hasEitherOperand(hasType(realFloatingPointType()))),
880879
unless(hasLHS(AnyLiteralExpr)),
881880
unless(hasDescendant(BannedIntegerLiteral)),
882-
unless(hasAncestor(BannedAncestor)))
881+
unless(IsInUnevaluatedContext))
883882
.bind("binary")),
884883
this);
885884

@@ -893,7 +892,7 @@ void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
893892
unless(binaryOperatorIsInMacro()),
894893
// TODO: if the banned macros are themselves duplicated
895894
unless(hasDescendant(BannedIntegerLiteral)),
896-
unless(hasAncestor(BannedAncestor)))
895+
unless(IsInUnevaluatedContext))
897896
.bind("nested-duplicates"),
898897
this);
899898

@@ -904,7 +903,7 @@ void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
904903
// Filter noisy false positives.
905904
unless(conditionalOperatorIsInMacro()),
906905
unless(isInTemplateInstantiation()),
907-
unless(hasAncestor(BannedAncestor)))
906+
unless(IsInUnevaluatedContext))
908907
.bind("cond")),
909908
this);
910909

@@ -918,7 +917,7 @@ void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
918917
parametersAreEquivalent(),
919918
// Filter noisy false positives.
920919
unless(isMacro()), unless(isInTemplateInstantiation()),
921-
unless(hasAncestor(BannedAncestor)))
920+
unless(IsInUnevaluatedContext))
922921
.bind("call")),
923922
this);
924923

@@ -929,7 +928,7 @@ void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
929928
nestedParametersAreEquivalent(), argumentCountIs(2),
930929
// Filter noisy false positives.
931930
unless(isMacro()), unless(isInTemplateInstantiation()),
932-
unless(hasAncestor(BannedAncestor)))
931+
unless(IsInUnevaluatedContext))
933932
.bind("nested-duplicates"),
934933
this);
935934

@@ -947,7 +946,7 @@ void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
947946
integerLiteral())),
948947
hasRHS(integerLiteral())))))
949948
.bind("logical-bitwise-confusion")),
950-
unless(hasAncestor(BannedAncestor)))),
949+
unless(IsInUnevaluatedContext))),
951950
this);
952951

953952
// Match expressions like: (X << 8) & 0xFF
@@ -961,7 +960,7 @@ void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
961960
integerLiteral().bind("shift-const"))))),
962961
ignoringParenImpCasts(
963962
integerLiteral().bind("and-const"))),
964-
unless(hasAncestor(BannedAncestor)))
963+
unless(IsInUnevaluatedContext))
965964
.bind("left-right-shift-confusion")),
966965
this);
967966

@@ -980,7 +979,7 @@ void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
980979
Finder->addMatcher(
981980
traverse(TK_AsIs, binaryOperator(isComparisonOperator(),
982981
hasOperands(BinOpCstLeft, CstRight),
983-
unless(hasAncestor(BannedAncestor)))
982+
unless(IsInUnevaluatedContext))
984983
.bind("binop-const-compare-to-const")),
985984
this);
986985

@@ -991,7 +990,7 @@ void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
991990
binaryOperator(isComparisonOperator(),
992991
anyOf(allOf(hasLHS(BinOpCstLeft), hasRHS(SymRight)),
993992
allOf(hasLHS(SymRight), hasRHS(BinOpCstLeft))),
994-
unless(hasAncestor(BannedAncestor)))
993+
unless(IsInUnevaluatedContext))
995994
.bind("binop-const-compare-to-sym")),
996995
this);
997996

@@ -1002,7 +1001,7 @@ void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
10021001
hasRHS(BinOpCstRight),
10031002
// Already reported as redundant.
10041003
unless(operandsAreEquivalent()),
1005-
unless(hasAncestor(BannedAncestor)))
1004+
unless(IsInUnevaluatedContext))
10061005
.bind("binop-const-compare-to-binop-const")),
10071006
this);
10081007

@@ -1019,7 +1018,7 @@ void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
10191018
hasLHS(ComparisonLeft), hasRHS(ComparisonRight),
10201019
// Already reported as redundant.
10211020
unless(operandsAreEquivalent()),
1022-
unless(hasAncestor(BannedAncestor)))
1021+
unless(IsInUnevaluatedContext))
10231022
.bind("comparisons-of-symbol-and-const")),
10241023
this);
10251024
}

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ Changes in existing checks
198198
<clang-tidy/checks/misc/include-cleaner>` check by adding option
199199
`DeduplicateFindings` to output one finding per symbol occurrence.
200200

201+
- Improved :doc:`misc-redundant-expression
202+
<clang-tidy/checks/misc/redundant-expression>` check to ignore
203+
false-positives in unevaluated context (e.g., ``decltype``).
204+
201205
- Improved :doc:`modernize-loop-convert
202206
<clang-tidy/checks/modernize/loop-convert>` to support for-loops with
203207
iterators initialized by free functions like ``begin``, ``end``, or ``size``.

clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,3 +855,11 @@ static_assert(sizeof(X) == sizeof(X));
855855
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: both sides of operator are equivalent
856856

857857
}
858+
859+
namespace PR35857 {
860+
void test() {
861+
int x = 0;
862+
int y = 0;
863+
decltype(x + y - (x + y)) z = 10;
864+
}
865+
}

0 commit comments

Comments
 (0)