Skip to content

Commit 65fe2d1

Browse files
authored
[clang-tidy] Fix a broken fix-it provided by modernize-use-integer-sign-comparison (#163488)
Fixes #162981. This is an issue with both named and functional casts, but this check doesn't have any existing tests for the latter, so I went and added those.
1 parent dd6a6ba commit 65fe2d1

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ void UseIntegerSignComparisonCheck::check(
152152
if (const auto *RHSCast = llvm::dyn_cast<ExplicitCastExpr>(RHS)) {
153153
SubExprRHS = RHSCast->getSubExpr();
154154
R2.setEnd(SubExprRHS->getBeginLoc().getLocWithOffset(-1));
155+
R3.setBegin(Lexer::getLocForEndOfToken(
156+
SubExprRHS->getEndLoc(), 0, *Result.SourceManager, getLangOpts()));
155157
}
156158
DiagnosticBuilder Diag =
157159
diag(BinaryOp->getBeginLoc(),

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,11 @@ Changes in existing checks
363363
<clang-tidy/checks/modernize/use-designated-initializers>` check to
364364
suggest using designated initializers for aliased aggregate types.
365365

366+
- Improved :doc:`modernize-use-integer-sign-comparison
367+
<clang-tidy/checks/modernize/use-integer-sign-comparison>` by providing
368+
correct fix-its when the right-hand side of a comparison contains a
369+
non-C-style cast.
370+
366371
- Improved :doc:`modernize-use-nullptr
367372
<clang-tidy/checks/modernize/use-nullptr>` check by fixing a crash
368373
on Windows when the check was enabled with a 32-bit :program:`clang-tidy`

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ int AllComparisons() {
9292
if (static_cast<unsigned int>(uArray[2]) < static_cast<int>(sArray[2]))
9393
return 0;
9494
// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
95-
// CHECK-FIXES: if (q20::cmp_less(uArray[2],sArray[2])))
96-
// FIXME: There should only be 2 closing braces. The fix-it inserts an unbalanced one.
95+
// CHECK-FIXES: if (q20::cmp_less(uArray[2],sArray[2]))
9796

9897
if ((unsigned int)uArray[3] < (int)sArray[3])
9998
return 0;
@@ -116,6 +115,11 @@ int AllComparisons() {
116115
// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
117116
// CHECK-FIXES: if (q20::cmp_greater(uArray[6] , VALUE))
118117

118+
if (unsigned(uArray[7]) >= int(sArray[7]))
119+
return 0;
120+
// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
121+
// CHECK-FIXES: if (q20::cmp_greater_equal(uArray[7],sArray[7]))
122+
119123

120124
FuncParameters(uVar);
121125
TemplateFuncParameter(sVar);

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ int AllComparisons() {
9191
if (static_cast<unsigned int>(uArray[2]) < static_cast<int>(sArray[2]))
9292
return 0;
9393
// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
94-
// CHECK-FIXES: if (std::cmp_less(uArray[2],sArray[2])))
95-
// FIXME: There should only be 2 closing braces. The fix-it inserts an unbalanced one.
94+
// CHECK-FIXES: if (std::cmp_less(uArray[2],sArray[2]))
9695

9796
if ((unsigned int)uArray[3] < (int)sArray[3])
9897
return 0;
@@ -115,6 +114,11 @@ int AllComparisons() {
115114
// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
116115
// CHECK-FIXES: if (std::cmp_greater(uArray[6] , VALUE))
117116

117+
if (unsigned(uArray[7]) >= int(sArray[7]))
118+
return 0;
119+
// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
120+
// CHECK-FIXES: if (std::cmp_greater_equal(uArray[7],sArray[7]))
121+
118122

119123
FuncParameters(uVar);
120124
TemplateFuncParameter(sVar);

0 commit comments

Comments
 (0)