<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - False positive from clang-analyzer-deadcode.DeadStores with reference and thread"
   href="https://bugs.llvm.org/show_bug.cgi?id=51674">51674</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>False positive from clang-analyzer-deadcode.DeadStores with reference and thread
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Static Analyzer
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>dcoughlin@apple.com
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>samuelmkearney@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dcoughlin@apple.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>