[PATCH] D28467: [Sema] Add warning for unused lambda captures

Ivan Krasin via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 18 14:03:28 PST 2017


krasin added a comment.

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.
For real world case, see https://codereview.chromium.org/2646553002/, where I tried to fix unused lambda captures in V8, in particular, this failure: https://build.chromium.org/p/tryserver.v8/builders/v8_win_rel_ng/builds/21172/steps/compile/logs/stdio


Repository:
  rL LLVM

https://reviews.llvm.org/D28467





More information about the cfe-commits mailing list