Skip to content

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

@greg7mdp

Description

@greg7mdp

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);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions