[llvm-bugs] [Bug 51674] New: False positive from clang-analyzer-deadcode.DeadStores with reference and thread

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Aug 30 07:50:34 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=51674

            Bug ID: 51674
           Summary: False positive from clang-analyzer-deadcode.DeadStores
                    with reference and thread
           Product: clang
           Version: unspecified
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Static Analyzer
          Assignee: dcoughlin at apple.com
          Reporter: samuelmkearney at gmail.com
                CC: dcoughlin at apple.com, llvm-bugs at lists.llvm.org

To reproduce: Install LLVM 14.0, add this minimal example as main.cpp:

```

#include <chrono>
#include <thread>
#include <iostream>

using namespace std::chrono_literals;

int main()
{
  bool keep_running_thread = true;

  std::thread thrd(
      [](const bool& keep_running) {
        while (keep_running)
        {
          std::cout << "Still running!\n";
          std::this_thread::sleep_for(200ms);
        }
      },
      std::ref(keep_running_thread));

  std::this_thread::sleep_for(1s);
  keep_running_thread = false;
  thrd.join();
  return 0;
}

```

Run clang-tidy --checks=clang-analyzer-* main.cpp. It reports:

main.cpp:22:3: warning: Value stored to 'keep_running_thread' is never read
[clang-analyzer-deadcode.DeadStores]
  keep_running_thread = false;
  ^                     ~~~~~
main.cpp:22:3: note: Value stored to 'keep_running_thread' is never read
  keep_running_thread = false;
  ^                     ~~~~~

I would expect no warning here since a reference to keep_running_thread is
passed to the thread.

Note: You can work around this by using a capturing lambda that captures
keep_running_thread by reference; but I would expect this to work using the
method shown as well.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210830/601763cc/attachment-0001.html>


More information about the llvm-bugs mailing list