[clang-tools-extra] FIX : bugprone-too-small-loop-variable - false-negative when const variable is used as loop bound (PR #81183)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 8 23:58:40 PST 2024


================
@@ -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())))))
----------------
PiotrZSL wrote:

i were thinking, instead of:
```
unless(hasType(enumType()))
```
simply write:
```
unless(declRefExpr(to(enumConstantDecl()))),
unless(allOf(hasType(isConstQualified()),
                    declRefExpr(to(varDecl(anyOf(hasInitializer(ignoringParenImpCasts(declRefExpr(to(enumConstantDecl())))), isConstexpr(), isConstinit())))))
```
Simply to exclude only those that are enum constant and those that use const variable that is initialized with enum constant, you could merge this with exist changes, and reduce duplications.

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


More information about the cfe-commits mailing list