[PATCH] D157374: [clang-tidy] Ignore decltype in misc-redundant-expression

2023-08-14 Thread Piotr Zegar via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG0c7d28f72e19: [clang-tidy] Ignore decltype in 
misc-redundant-expression (authored by PiotrZSL).

Changed prior to commit:
  https://reviews.llvm.org/D157374?vs=548104&id=550201#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157374/new/

https://reviews.llvm.org/D157374

Files:
  clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression.cpp
@@ -855,3 +855,11 @@
 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: both sides of operator are equivalent
 
 }
+
+namespace PR35857 {
+  void test() {
+int x = 0;
+int y = 0;
+decltype(x + y - (x + y)) z = 10;
+  }
+}
Index: clang-tools-extra/docs/ReleaseNotes.rst
===
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -198,6 +198,10 @@
   ` check by adding option
   `DeduplicateFindings` to output one finding per symbol occurrence.
 
+- Improved :doc:`misc-redundant-expression
+  ` check to ignore
+  false-positives in unevaluated context (e.g., ``decltype``).
+
 - Improved :doc:`modernize-loop-convert
   ` to support for-loops with
   iterators initialized by free functions like ``begin``, ``end``, or ``size``.
Index: clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
===
--- clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
+++ clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
@@ -441,8 +441,6 @@
   return Node.isIntegerConstantExpr(Finder->getASTContext());
 }
 
-AST_MATCHER(Expr, isRequiresExpr) { return isa(Node); }
-
 AST_MATCHER(BinaryOperator, operandsAreEquivalent) {
   return areEquivalentExpr(Node.getLHS(), Node.getRHS());
 }
@@ -862,7 +860,8 @@
 
   const auto BannedIntegerLiteral =
   integerLiteral(expandedByMacro(KnownBannedMacroNames));
-  const auto BannedAncestor = expr(isRequiresExpr());
+  const auto IsInUnevaluatedContext = expr(anyOf(
+  hasAncestor(expr(hasUnevaluatedContext())), hasAncestor(typeLoc(;
 
   // Binary with equivalent operands, like (X != 2 && X != 2).
   Finder->addMatcher(
@@ -879,7 +878,7 @@
unless(hasEitherOperand(hasType(realFloatingPointType(,
unless(hasLHS(AnyLiteralExpr)),
unless(hasDescendant(BannedIntegerLiteral)),
-   unless(hasAncestor(BannedAncestor)))
+   unless(IsInUnevaluatedContext))
.bind("binary")),
   this);
 
@@ -893,7 +892,7 @@
  unless(binaryOperatorIsInMacro()),
  // TODO: if the banned macros are themselves duplicated
  unless(hasDescendant(BannedIntegerLiteral)),
- unless(hasAncestor(BannedAncestor)))
+ unless(IsInUnevaluatedContext))
   .bind("nested-duplicates"),
   this);
 
@@ -904,7 +903,7 @@
// Filter noisy false positives.
unless(conditionalOperatorIsInMacro()),
unless(isInTemplateInstantiation()),
-   unless(hasAncestor(BannedAncestor)))
+   unless(IsInUnevaluatedContext))
.bind("cond")),
   this);
 
@@ -918,7 +917,7 @@
parametersAreEquivalent(),
// Filter noisy false positives.
unless(isMacro()), unless(isInTemplateInstantiation()),
-   unless(hasAncestor(BannedAncestor)))
+   unless(IsInUnevaluatedContext))
.bind("call")),
   this);
 
@@ -929,7 +928,7 @@
   nestedParametersAreEquivalent(), argumentCountIs(2),
   // Filter noisy false positives.
   unless(isMacro()), unless(isInTemplateInstantiation()),
-  unless(hasAncestor(BannedAncestor)))
+  unless(IsInUnevaluatedContext))
   .bind("nested-duplicates"),
   this);
 
@@ -947,7 +946,7 @@
integerLiteral())),
hasRHS(integerLiteral())
.bind("logical-bitwise-confusion")),
-   unless(hasAncestor(BannedAncestor,
+   unless(IsInUnevaluatedContext))),
   this);
 
   // Match expressions like: (X << 8) & 0xFF
@@ -961,7 +960,7 @@
 

[PATCH] D157374: [clang-tidy] Ignore decltype in misc-redundant-expression

2023-08-14 Thread Carlos Galvez via Phabricator via cfe-commits
carlosgalvezp accepted this revision.
carlosgalvezp added a comment.
This revision is now accepted and ready to land.

LGTM


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157374/new/

https://reviews.llvm.org/D157374

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157374: [clang-tidy] Ignore decltype in misc-redundant-expression

2023-08-08 Thread Piotr Zegar via Phabricator via cfe-commits
PiotrZSL created this revision.
PiotrZSL added reviewers: njames93, carlosgalvezp.
Herald added a subscriber: xazax.hun.
Herald added a project: All.
PiotrZSL requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Modify check to ignore any parent typeLoc and
other unevaluated context.

Fixes: #35857


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157374

Files:
  clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression.cpp
===
--- clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression.cpp
@@ -855,3 +855,11 @@
 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: both sides of operator are equivalent
 
 }
+
+namespace PR35857 {
+  void test() {
+int x = 0;
+int y = 0;
+decltype(x + y - (x + y)) z = 10;
+  }
+}
Index: clang-tools-extra/docs/ReleaseNotes.rst
===
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -180,6 +180,10 @@
   ` check to provide fixes for
   ``inline`` namespaces in the same format as :program:`clang-format`.
 
+- Improved :doc:`misc-redundant-expression
+  ` check to ignore
+  false-positives in unevaluated context (e.g., ``decltype``).
+
 - Improved :doc:`modernize-loop-convert
   ` to support for-loops with
   iterators initialized by free functions like ``begin``, ``end``, or ``size``.
Index: clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
===
--- clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
+++ clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
@@ -441,8 +441,6 @@
   return Node.isIntegerConstantExpr(Finder->getASTContext());
 }
 
-AST_MATCHER(Expr, isRequiresExpr) { return isa(Node); }
-
 AST_MATCHER(BinaryOperator, operandsAreEquivalent) {
   return areEquivalentExpr(Node.getLHS(), Node.getRHS());
 }
@@ -862,7 +860,8 @@
 
   const auto BannedIntegerLiteral =
   integerLiteral(expandedByMacro(KnownBannedMacroNames));
-  const auto BannedAncestor = expr(isRequiresExpr());
+  const auto IsInUnevaluatedContext = expr(anyOf(
+  hasAncestor(expr(hasUnevaluatedContext())), hasAncestor(typeLoc(;
 
   // Binary with equivalent operands, like (X != 2 && X != 2).
   Finder->addMatcher(
@@ -879,7 +878,7 @@
unless(hasEitherOperand(hasType(realFloatingPointType(,
unless(hasLHS(AnyLiteralExpr)),
unless(hasDescendant(BannedIntegerLiteral)),
-   unless(hasAncestor(BannedAncestor)))
+   unless(IsInUnevaluatedContext))
.bind("binary")),
   this);
 
@@ -893,7 +892,7 @@
  unless(binaryOperatorIsInMacro()),
  // TODO: if the banned macros are themselves duplicated
  unless(hasDescendant(BannedIntegerLiteral)),
- unless(hasAncestor(BannedAncestor)))
+ unless(IsInUnevaluatedContext))
   .bind("nested-duplicates"),
   this);
 
@@ -904,7 +903,7 @@
// Filter noisy false positives.
unless(conditionalOperatorIsInMacro()),
unless(isInTemplateInstantiation()),
-   unless(hasAncestor(BannedAncestor)))
+   unless(IsInUnevaluatedContext))
.bind("cond")),
   this);
 
@@ -918,7 +917,7 @@
parametersAreEquivalent(),
// Filter noisy false positives.
unless(isMacro()), unless(isInTemplateInstantiation()),
-   unless(hasAncestor(BannedAncestor)))
+   unless(IsInUnevaluatedContext))
.bind("call")),
   this);
 
@@ -929,7 +928,7 @@
   nestedParametersAreEquivalent(), argumentCountIs(2),
   // Filter noisy false positives.
   unless(isMacro()), unless(isInTemplateInstantiation()),
-  unless(hasAncestor(BannedAncestor)))
+  unless(IsInUnevaluatedContext))
   .bind("nested-duplicates"),
   this);
 
@@ -947,7 +946,7 @@
integerLiteral())),
hasRHS(integerLiteral())
.bind("logical-bitwise-confusion")),
-   unless(hasAncestor(BannedAncestor,
+   unless(IsInUnevaluatedContext))),
   this);
 
   // Match expressions like: (X << 8) & 0xFF
@@ -961,7 +960,7 @