[clang-tools-extra] FIX : bugprone-too-small-loop-variable - false-negative when const variable is used as loop bound (PR #81183)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 8 11:56:37 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy
Author: Shourya Goel (Sh0g0-1758)
<details>
<summary>Changes</summary>
Fixes: #<!-- -->79580
**PR SUMMARY**: Changed LibASTMatcher to give an appropriate warning when a const loop bound is initialized with a function declaration.
---
Full diff: https://github.com/llvm/llvm-project/pull/81183.diff
4 Files Affected:
- (modified) clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp (+8-4)
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+3)
- (modified) clang-tools-extra/docs/clang-tidy/checks/bugprone/too-small-loop-variable.rst (+2)
- (modified) clang-tools-extra/test/clang-tidy/checkers/bugprone/too-small-loop-variable.cpp (+12)
``````````diff
diff --git a/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
index 8ba8b893e03a6..a73d46f01d9b2 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
@@ -82,10 +82,14 @@ void TooSmallLoopVariableCheck::registerMatchers(MatchFinder *Finder) {
// We are interested in only those cases when the loop bound is a variable
// value (not const, enum, etc.).
StatementMatcher LoopBoundMatcher =
- expr(ignoringParenImpCasts(allOf(hasType(isInteger()),
- unless(integerLiteral()),
- unless(hasType(isConstQualified())),
- unless(hasType(enumType())))))
+ expr(ignoringParenImpCasts(allOf(
+ hasType(isInteger()), unless(integerLiteral()),
+ unless(allOf(
+ hasType(isConstQualified()),
+ declRefExpr(to(varDecl(anyOf(
+ hasInitializer(ignoringParenImpCasts(integerLiteral())),
+ isConstexpr(), isConstinit())))))),
+ unless(hasType(enumType())))))
.bind(LoopUpperBoundName);
// We use the loop increment expression only to make sure we found the right
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index e50914aed5f07..17b1349d20db8 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -169,6 +169,9 @@ Miscellaneous
option is specified. Now ``clang-apply-replacements`` applies formatting only with
the option.
+- Fixed incorrect implementation of ``too-small-loop-variable`` check when a const loop
+ variable is initialized with a function declaration.
+
Improvements to include-fixer
-----------------------------
diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/too-small-loop-variable.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/too-small-loop-variable.rst
index 0f45cc2fe1146..9ee23be45cfea 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/too-small-loop-variable.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/too-small-loop-variable.rst
@@ -44,3 +44,5 @@ a larger user input.
for (unsigned i = 0; i < size; ++i) {} // no warning with MagnitudeBitsUpperLimit = 31 on a system where unsigned is 32-bit
for (int i = 0; i < size; ++i) {} // warning with MagnitudeBitsUpperLimit = 31 on a system where int is 32-bit
}
+
+``-Wtautological-constant-out-of-range-compare`` compiler warning should also be used.
\ No newline at end of file
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/too-small-loop-variable.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/too-small-loop-variable.cpp
index 3229deb93bada..113150b168650 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/too-small-loop-variable.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/too-small-loop-variable.cpp
@@ -93,6 +93,18 @@ void voidBadForLoopWithMacroBound() {
}
}
+unsigned int getVal() {
+ return 300;
+}
+
+// The iteration's upper bound has a function declaration.
+void voidBadForLoop8() {
+ const unsigned int l = getVal();
+ for (unsigned char i = 0; i < l; ++i) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: loop variable has narrower type 'unsigned char' than iteration's upper bound 'const unsigned int' [bugprone-too-small-loop-variable]
+ }
+}
+
////////////////////////////////////////////////////////////////////////////////
/// Correct loops: we should not warn here.
``````````
</details>
https://github.com/llvm/llvm-project/pull/81183
More information about the cfe-commits
mailing list