<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 on -Wunused-lambda-capture if capturing a reference to a global variable"
   href="https://bugs.llvm.org/show_bug.cgi?id=52099">52099</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>False positive on -Wunused-lambda-capture if capturing a reference to a global variable
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>C++11
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

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

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>When capturing a variable on a lambda that is a reference to a global variable,
or a copy of such, clang warns that the capture is not needed, even though it
technically is, which I'm assuming is due to some optimization pass that it
does.


This happens from Clang 5 to trunk, as shown here:
<a href="https://godbolt.org/z/Yojn83ocP">https://godbolt.org/z/Yojn83ocP</a>

const int globalConfig = 3;

int main()
{
    const auto& localConfig = globalConfig;
    const auto foo = [&localConfig]() { 
        return localConfig == 3;
    };
    return foo();
}

<source>:6:24: warning: lambda capture 'localConfig' is not required to be
captured for this use [-Wunused-lambda-capture]

Note that the warning disappears if we make 'localConfig' technically change
conditionally: <a href="https://godbolt.org/z/W6Gecx7rE">https://godbolt.org/z/W6Gecx7rE</a>

const int globalConfig1 = 3;
const int globalConfig2 = 3;

int main()
{
    bool bla = true;
    const auto& localConfig = bla ? globalConfig1 : globalConfig2;
    const auto foo = [&localConfig]() { 
        return localConfig == 3;
    };
    return foo();
}</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>