[clang] [clang] Reject array comparisons in C++26 for unknown/dependent types in templates (PR #191101)

via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 14 04:57:13 PDT 2026


================
@@ -12488,8 +12488,7 @@ static void diagnoseTautologicalComparison(Sema &S, SourceLocation Loc,
   QualType LHSType = LHS->getType();
   QualType RHSType = RHS->getType();
   if (LHSType->hasFloatingRepresentation() ||
-      (LHSType->isBlockPointerType() && !BinaryOperator::isEqualityOp(Opc)) ||
-      S.inTemplateInstantiation())
+      (LHSType->isBlockPointerType() && !BinaryOperator::isEqualityOp(Opc)))
----------------
Sirraide wrote:

The entire if statement.

`diagnoseTautologicalComparison()` warns e.g. if you write `true == true` since that’s a bit pointless since it’s always true. The reason this check is here is because comparing arrays compares the addresses, not the elements, and thus, if we know what the arrays are at compile time, we can warn that the comparison will always be true/false.

Now that C++26 has made this ill-formed, it’d be easiest to just move it out of `diagnoseTautologicalComparison()`; we should continue to emit the warning in older language modes, of course, but the code doesn’t need to be in this function for that, and while there are several places where this is called from, only the one I pointed out can possibly involve array types I believe.

The reason we check for `inTemplateInstantiation()` here is that we don’t want to emit tautological comparison warnings if the user writes e.g.
```
template <const int (&a)[3], const int (&b)[3]>
bool f() { return a == b; }
```
and `a` and `b` just happen to refer to the same array when instantiated (https://godbolt.org/z/bP6Yc61Mn). That check makes sense if all we’re doing is emitting tautological comparison warnings, but not for deprecated array comparisons

https://github.com/llvm/llvm-project/pull/191101


More information about the cfe-commits mailing list