llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

False "Value stored is never read" due to reference captured in lambda

Open llvmbot opened this issue 11 years ago • 5 comments

Bugzilla Link 22834
Resolution FIXED
Resolved on Aug 04, 2020 14:38
Version 3.5
OS MacOS X
Attachments Runnable example
Reporter LLVM Bugzilla Contributor
CC @aaronpuchert,@devincoughlin,@haoNoQ,@Xazax-hun
Fixed by commit(s) r253630

Extended Description

If a value is captured in a lambda, then modified later in the code, the analyzer thinks the value is never used, and flags it as a dead store, even if the lambda is run locally to use the value. Here's a simple example, where f1() prints "0" rather than "1" as a result of the indicated line.

bool b = true;
auto f1 = [&b] () {
    cout << b << endl;
};
b = false; // warning on this line
f1();

/ws/art_hack/ArtCppHack/ArtCppHack/main.cpp:12:5: warning: Value stored to 'b' is never read b = false; ^ ~~~~~ 1 warning generated.

We're generally able to work around this by adding something like "(void)b;" to make the variable appear to be used, but it would be nice not to have to.

I'm using the analyzer integrated in Xcode 6.1.1.

llvmbot avatar Mar 07 '15 07:03 llvmbot

I hope so. Lambdas are much more popular these days than they were in 2015 and i haven't heard much complaints about those recently. Tried to reproduce it on Godbolt: the false positive is there until 3.7.1, and the supposed fix was first released with 3.8. So it all adds up.

aaronpuchert avatar Aug 04 '20 21:08 aaronpuchert

I hope so. Lambdas are much more popular these days than they were in 2015 and i haven't heard much complaints about those recently.

haoNoQ avatar Aug 04 '20 18:08 haoNoQ

Patch under review. http://reviews.llvm.org/D14736 This has been submitted, so I assume the bug can be closed?

aaronpuchert avatar Aug 04 '20 16:08 aaronpuchert

Patch under review. http://reviews.llvm.org/D14736

devincoughlin avatar Nov 17 '15 02:11 devincoughlin

assigned to @tkremenek

llvmbot avatar Mar 07 '15 07:03 llvmbot