Warn when a lambda explicitly captures something that is not used in its body.
The warning is part of -Wunused and can be enabled with -Wunused-lambda-capture.
Differential D28467
[Sema] Add warning for unused lambda captures malcolm.parsons on Jan 9 2017, 5:33 AM. Authored by
Details Warn when a lambda explicitly captures something that is not used in its body. The warning is part of -Wunused and can be enabled with -Wunused-lambda-capture.
Diff Detail
Event Timeline
Comment Actions I think that the patch would be neater if you add "-Wno-unused-lambda-capture" to the options for all of the tests that are modified by this patch in the CXX/ directory. This would avoid redundant (void) uses and ensure that the (void) uses won't interfere with the original intent where things might be only used in the capture list.
Comment Actions Change warning message.
Comment Actions Improve warning message for use in unevaluated context.
Comment Actions LGTM; this is an awesome new diagnostic, thank you for working on it!
Comment Actions /home/buildbots/ppc64be-sanitizer/sanitizer-ppc64be/build/llvm/lib/Analysis/ValueTracking.cpp:1116:17: error: lambda capture 'BitWidth' is not used [-Werror,-Wunused-lambda-capture] auto KOF = [BitWidth](const APInt &KnownOne, unsigned ShiftAmt) { ^ /home/buildbots/ppc64be-sanitizer/sanitizer-ppc64be/build/llvm/lib/Analysis/ValueTracking.cpp:1127:17: error: lambda capture 'BitWidth' is not used [-Werror,-Wunused-lambda-capture] auto KZF = [BitWidth](const APInt &KnownZero, unsigned ShiftAmt) { ^ /home/buildbots/ppc64be-sanitizer/sanitizer-ppc64be/build/llvm/lib/Analysis/ValueTracking.cpp:1131:17: error: lambda capture 'BitWidth' is not used [-Werror,-Wunused-lambda-capture] auto KOF = [BitWidth](const APInt &KnownOne, unsigned ShiftAmt) { ^ 3 errors generated. The warnings are correct. Should I remove the unused lambda captures or revert this changeset? Comment Actions IMO, you can go ahead and commit a fix that removes the captures, and the fix can be reviewed post-commit. I don't think you should revert this patch as you'll have to remove the captures anyway before reinstating this patch, so might as well do it now. Comment Actions The warnings look correct to me; I think you should remove the unused lambda captures (no need for a test or review since changes are NFC). Comment Actions This change makes Clang hardly incompatible with MSVC++. Consider the following program: #include <stdio.h> int main(void) { const int kDelta = 10000001; auto g = [kDelta](int i) { printf("%d\n", i % kDelta); }; g(2); } Clang will warn about the unused lambda capture: $ clang++ lala.cc -o lala -std=c++14 -Wall -Werror && ./lala lala.cc:5:13: error: lambda capture 'kDelta' is not required to be captured for use in an unevaluated context [-Werror,-Wunused-lambda-capture] auto g = [kDelta](int i) ^ 1 error generated. It will compile without any warnings if I remove kDelta from the list of captures: #include <stdio.h> int main(void) { const int kDelta = 10000001; auto g = [](int i) { printf("%d\n", i % kDelta); }; g(2); } But then Microsoft C++ compiler will raise the error: error C3493: 'kDelta' cannot be implicitly captured because no default capture mode has been specified At this point, I am unsure how can this be improved, but I feel that pointing out this inconsistency might be useful. Comment Actions MSVC's behaviour is discussed here: https://social.msdn.microsoft.com/Forums/SqlServer/en-US/4abf18bd-4ae4-4c72-ba3e-3b13e7909d5f/error-c2057-or-c3493-trying-to-use-an-integral-constant-expression-inside-a-lambda?forum=vclanguage Comment Actions Is kDelta considered to be used in an unevaluated context here? I thought unevaluated context in c++ means the expression is used as operands of operators such as typeid and decltype. |
We do not use the term "odr-use" in any of our other frontend diagnostics; I don't think this is a term that users will actually understand. I think we should drop the term and just use "used".