Skip to content

Commit ac9833c

Browse files
committed
[clang-tidy] Improve integer comparison by matching valid expressions outside implicitCastExpr
1 parent 03a791f commit ac9833c

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

clang-tools-extra/clang-tidy/modernize/UseIntegerSignComparisonCheck.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ intCastExpression(bool IsSigned,
3939
// std::cmp_{} functions trigger a compile-time error if either LHS or RHS
4040
// is a non-integer type, char, enum or bool
4141
// (unsigned char/ signed char are Ok and can be used).
42-
auto IntTypeExpr = expr(hasType(hasCanonicalType(qualType(
42+
const auto HasIntegerType = hasType(hasCanonicalType(qualType(
4343
isInteger(), IsSigned ? isSignedInteger() : isUnsignedInteger(),
44-
unless(isActualChar()), unless(booleanType()), unless(enumType())))));
44+
unless(isActualChar()), unless(booleanType()), unless(enumType()))));
45+
46+
auto IntTypeExpr = expr(HasIntegerType);
4547

4648
const auto ImplicitCastExpr =
4749
CastBindName.empty() ? implicitCastExpr(hasSourceExpression(IntTypeExpr))
@@ -52,8 +54,16 @@ intCastExpression(bool IsSigned,
5254
const auto StaticCastExpr = cxxStaticCastExpr(has(ImplicitCastExpr));
5355
const auto FunctionalCastExpr = cxxFunctionalCastExpr(has(ImplicitCastExpr));
5456

57+
// Match function calls or variable references not directly wrapped by an implicit cast
58+
const auto CallIntExpr = CastBindName.empty()
59+
? callExpr(HasIntegerType)
60+
: callExpr(HasIntegerType).bind(CastBindName);
61+
const auto DeclRefIntExpr =
62+
CastBindName.empty() ? declRefExpr(HasIntegerType)
63+
: declRefExpr(HasIntegerType).bind(CastBindName);
64+
5565
return expr(anyOf(ImplicitCastExpr, CStyleCastExpr, StaticCastExpr,
56-
FunctionalCastExpr));
66+
FunctionalCastExpr, CallIntExpr));
5767
}
5868

5969
static StringRef parseOpCode(BinaryOperator::Opcode Code) {

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ Changes in existing checks
177177
matched scenarios of ``find`` and ``rfind`` methods and fixing false
178178
positives when those methods were called with 3 arguments.
179179

180+
- Improved :doc:`modernize-use-integer-sign-comparison
181+
<clang-tidy/checks/modernize/use-integer-sign-comparison>` check by matching
182+
valid integer expressions not directly wrapped around an Implicit Cast.
183+
180184
- Improved :doc:`modernize-use-std-numbers
181185
<clang-tidy/checks/modernize/use-std-numbers>` check to support math
182186
functions of different precisions.

clang-tools-extra/test/clang-tidy/checkers/modernize/use-integer-sign-comparison.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,29 @@ int AllComparisons() {
120120

121121
return 0;
122122
}
123+
124+
namespace PR127471 {
125+
int getSignedValue();
126+
unsigned int getUnsignedValue();
127+
128+
void callExprTest() {
129+
130+
if (getSignedValue() < getUnsignedValue())
131+
return;
132+
// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
133+
// CHECK-FIXES: if (std::cmp_less(getSignedValue() , getUnsignedValue()))
134+
135+
int sVar = 0;
136+
if (getUnsignedValue() > sVar)
137+
return;
138+
// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
139+
// CHECK-FIXES: if (std::cmp_greater(getUnsignedValue() , sVar))
140+
141+
unsigned int uVar = 0;
142+
if (getSignedValue() > uVar)
143+
return;
144+
// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
145+
// CHECK-FIXES: if (std::cmp_greater(getSignedValue() , uVar))
146+
147+
}
148+
} // namespace PR127471

0 commit comments

Comments
 (0)