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

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


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (halbi2)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/128614.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaChecking.cpp (+2-1) 
- (modified) clang/test/SemaCXX/compare.cpp (+8-8) 


``````````diff
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'}}
 
 }
 

``````````

</details>


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


More information about the cfe-commits mailing list