Wish: Split Wunused-lambda-capture

In migrating our codebase to Xcode 9.3, I came across the new warning -Wunused-lambda-capture. This seems like a helpful warning to enable; yet unfortunately I cannot due to incompatibility with MSVC.

The problem is that it warns on the following code (https://godbolt.org/g/HDiqR8):

int times2(int num) {
  const int two = 2;
  auto multiply = [two](int i) { return i * two; };
  return multiply(num);
}

Per the standard, it is unnecessary to capture `two` because it is an integral constant. MSVC doesn't know about this, and throws C3493 (variable cannot be implicitly captured) if I remove `two` from the capture clause to fix clang's -Wunused-lambda-capture (https://godbolt.org/g/VNNfqT).

Since I don't expect MSVC to be standards compliant any time soon, it would be great if this warning could be split into two warnings:

-Wunused-lambda-capture would only warn on truly unused captures, but not captures that are merely unnecessary (but still used as in the example above).

-Wunnecessary-lambda-capture would warn on redundant explicit captures (as in the example above).

Thanks for considering my wish and making clang even better!

Tobias

Ableton AG, Schoenhauser Allee 6-7, 10119 Berlin, Germany
Sitz (Registered Office) Berlin, Amtsgericht Berlin-Charlottenburg, HRB 72838
Vorstand (Management Board): Gerhard Behles, Jan Bohl
Vorsitzender des Aufsichtsrats (Chair of the Supervisory Board): Uwe Struck

" Since I don't expect MSVC to be standards compliant any time soon,"

I think you may wish to report this as a bug to the MSVC team. They have been REALLY good about fixing standards compliance issues in the past two years or so.

I did that as well as posting my wish here, let's see...

T

This was discussed when the warning was added. I felt the same way, but Aaron convinced me that this is short (“short”) term pain:
http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20170116/183259.html

https://marc.info/?l=cfe-commits&m=148521057007058&w=2

https://marc.info/?l=cfe-commits&m=148546938216504&w=2

We disabled the warning for now in Chromium.

I just got word back from a PM at Microsoft, apparently fixing this bug is not an easy one (seemingly would require architectural changes), and so this is likely going to be a long lived bug in MSVC.

I tend to agree completely with Aaron in that thread, but I wanted to make sure we have this context in case we revisit.

-Erich

Further clarification, “Long Lived” (PM actually said ‘a while’ to me) means “weeks/months”, so consider that as you will.