[llvm-bugs] [Bug 52099] New: False positive on -Wunused-lambda-capture if capturing a reference to a global variable

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Oct 6 12:04:53 PDT 2021


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

            Bug ID: 52099
           Summary: False positive on -Wunused-lambda-capture if capturing
                    a reference to a global variable
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++11
          Assignee: unassignedclangbugs at nondot.org
          Reporter: tupaschoal at gmail.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

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:
https://godbolt.org/z/Yojn83ocP

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: https://godbolt.org/z/W6Gecx7rE

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

-- 
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/20211006/51c5caaa/attachment.html>


More information about the llvm-bugs mailing list