[clang] [clang] Improve -Wsign-compare diagnostic (PR #128614)

via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 24 17:57:06 PST 2025


https://github.com/halbi2 created https://github.com/llvm/llvm-project/pull/128614

The cv-qualifiers are not relevant to the comparison result so one should not print them.

>From 5137e7f1892e4b61d1e496372e82f63b944b8124 Mon Sep 17 00:00:00 2001
From: halbi2 <hehiralbi at gmail.com>
Date: Mon, 24 Feb 2025 19:54:09 -0500
Subject: [PATCH] [clang] Improve -Wsign-compare diagnostic

The cv-qualifiers are not relevant to the comparison result
so one should not print them.
---
 clang/lib/Sema/SemaChecking.cpp |  3 ++-
 clang/test/SemaCXX/compare.cpp  | 16 ++++++++--------
 2 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 74f425d32648f..5c664eebeb0c5 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -10593,7 +10593,8 @@ static void AnalyzeComparison(Sema &S, BinaryOperator *E) {
 
   S.DiagRuntimeBehavior(E->getOperatorLoc(), E,
                         S.PDiag(diag::warn_mixed_sign_comparison)
-                            << LHS->getType() << RHS->getType()
+                            << LHS->getType().getUnqualifiedType()
+                            << RHS->getType().getUnqualifiedType()
                             << LHS->getSourceRange() << RHS->getSourceRange());
 }
 
diff --git a/clang/test/SemaCXX/compare.cpp b/clang/test/SemaCXX/compare.cpp
index cfddf2142f308..d678adc38efd1 100644
--- a/clang/test/SemaCXX/compare.cpp
+++ b/clang/test/SemaCXX/compare.cpp
@@ -233,10 +233,10 @@ void test4(short s) {
   // All negative shorts are cast towards the max unsigned range.  Relation
   // comparisons are possible, but equality comparisons are tautological.
   const unsigned A = 32768;
-  void (s < A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
-  void (s > A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
-  void (s <= A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
-  void (s >= A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
+  void (s < A); // expected-warning{{comparison of integers of different signs: 'short' and 'unsigned int'}}
+  void (s > A); // expected-warning{{comparison of integers of different signs: 'short' and 'unsigned int'}}
+  void (s <= A); // expected-warning{{comparison of integers of different signs: 'short' and 'unsigned int'}}
+  void (s >= A); // expected-warning{{comparison of integers of different signs: 'short' and 'unsigned int'}}
 
   void (s == A); // expected-warning{{comparison of constant 32768 with expression of type 'short' is always false}}
   void (s != A); // expected-warning{{comparison of constant 32768 with expression of type 'short' is always true}}
@@ -245,12 +245,12 @@ void test4(short s) {
   // unsigned.  Likewise, a negative one short can also be converted to max
   // unsigned.
   const unsigned B = -1;
-  void (s < B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
+  void (s < B); // expected-warning{{comparison of integers of different signs: 'short' and 'unsigned int'}}
   void (s > B); // expected-warning{{comparison 'short' > 4294967295 is always false}}
   void (s <= B); // expected-warning{{comparison 'short' <= 4294967295 is always true}}
-  void (s >= B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
-  void (s == B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
-  void (s != B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
+  void (s >= B); // expected-warning{{comparison of integers of different signs: 'short' and 'unsigned int'}}
+  void (s == B); // expected-warning{{comparison of integers of different signs: 'short' and 'unsigned int'}}
+  void (s != B); // expected-warning{{comparison of integers of different signs: 'short' and 'unsigned int'}}
 
 }
 



More information about the cfe-commits mailing list