Skip to content
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

False positive "for loop analysis" warning #109402

Open
rHermes opened this issue Sep 20, 2024 · 4 comments
Open

False positive "for loop analysis" warning #109402

rHermes opened this issue Sep 20, 2024 · 4 comments
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer false-positive Warning fires when it should not

Comments

@rHermes
Copy link

rHermes commented Sep 20, 2024

This is tested with clang 18.1.0 on godbolt and occurs on trunk.

Here is the godbolt: https://godbolt.org/z/o3jhnoPso

Here is the code:

#include <iostream>
#include <vector>

/**
 * Definition for singly-linked list.
 */
struct ListNode
{
  int val;
  ListNode* next;
  ListNode() : val(0), next(nullptr) {}
  ListNode(int x) : val(x), next(nullptr) {}
  ListNode(int x, ListNode* next) : val(x), next(next) {}
};

class Solution
{
public:
  static std::vector<std::vector<int>> spiralMatrix(const int M, const int N, ListNode const* head)
  {
    std::vector<std::vector<int>> out(M, std::vector<int>(N, -1));

    const auto writeVal = [&](const int y, const int x) {
      if (head) {
        out[y][x] = head->val;
        head = head->next;
      }
    };

    // ok, we will loop in numbers.
    for (int iter = 0; head; iter++) {
      const int W = N - 2 * iter;
      const int H = M - 2 * iter;

      for (int j = 0; j < W; j++) {
        writeVal(iter, iter + j);
      }

      for (int j = 1; j < H; j++) {
        writeVal(iter + j, iter + W - 1);
      }

      for (int j = W - 2; 0 <= j; j--) {
        writeVal(iter + H - 1, iter + j);
      }

      for (int j = H - 2; 0 < j; j--) {
        writeVal(iter + j, iter);
      }
    }

    return out;
  }
};

If you compile this like so you get the following warning:

clang++ -Wall -Wextra -std=c++2a -Werror -o test test.cpp
<source>:31:24: error: variable 'head' used in loop condition not modified in loop body [-Werror,-Wfor-loop-analysis]
   31 |     for (int iter = 0; head; iter++) {
      |                        ^~~~
@i42output
Copy link

i42output commented Sep 20, 2024

Simpler testcase exhibiting the same behaviour:

int main()
{
    bool xyzzy = true;

    auto foo = [&]()
    {
        xyzzy = false;
    };

    for (int i = 0; xyzzy; ++i)
    {
        foo();
    }
}

image

@EugeneZelenko EugeneZelenko added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer false-positive Warning fires when it should not and removed new issue labels Sep 20, 2024
@EugeneZelenko
Copy link
Contributor

Could you please try 19 or main branch? https://godbolt.org should be helpful.

@rHermes
Copy link
Author

rHermes commented Sep 30, 2024

I am getting the same error on trunk and 19 :)

@shafik
Copy link
Collaborator

shafik commented Sep 30, 2024

looks like this goes back all the way to clang-3.8: https://godbolt.org/z/66oxxG9oW

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 false-positive Warning fires when it should not
Projects
None yet
Development

No branches or pull requests

4 participants