Skip to content

[clang++]: Incorrect warning emited for [-Wfor-loop-analysis] #132038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
greg7mdp opened this issue Mar 19, 2025 · 2 comments · May be fixed by #135573
Open

[clang++]: Incorrect warning emited for [-Wfor-loop-analysis] #132038

greg7mdp opened this issue Mar 19, 2025 · 2 comments · May be fixed by #135573
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer

Comments

@greg7mdp
Copy link

Warning correctly states that the variables are not modified in the loop body, but height is incremented nevertheless (in the incr_height lambda) and the loop is fine.

See example program:

// compile with `clang++-20 -std=c++20 -Wall -c loop.cpp`
//
// ~/tmp ❯ clang++-20 -std=c++20 -Wall -c loop.cpp
// loop.cpp:10:35: warning: variables 'height' and 'end_height' used in loop condition not modified in loop body [-Wfor-loop-analysis]
//    10 |    for (uint32_t end_height = 27; height <= end_height; incr_height())
//       |                                   ^~~~~~    ~~~~~~~~~~
// 1 warning generated.
// -----------------------------------------------------------------------------------------------------------------------------------
#include <cstdint>

extern void add_to_expected_table(uint32_t h);

void test() {
   uint32_t height = 0;

   auto incr_height = [&height]() { ++height; };

   for (uint32_t end_height = 27; height <= end_height; incr_height())
      add_to_expected_table(height);
}
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Mar 19, 2025
@EugeneZelenko EugeneZelenko added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer and removed clang Clang issues not falling into any other category labels Mar 19, 2025
@shafik
Copy link
Collaborator

shafik commented Mar 19, 2025

@shafik
Copy link
Collaborator

shafik commented Mar 19, 2025

It looks like this is handled in CheckForLoopConditionalStatement:

void CheckForLoopConditionalStatement(Sema &S, Expr *Second,

I think maybe the call to DeclMather for the Body here:

DeclMatcher(S, Decls, Body).FoundDeclInUse())

Is the important part, it looks like does not handle CallExpr:

class DeclMatcher : public EvaluatedExprVisitor<DeclMatcher> {

but I could be off

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants