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

Malcolm Parsons via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 12 07:50:44 PST 2017


malcolm.parsons added inline comments.


================
Comment at: test/SemaCXX/warn-unused-lambda-capture.cpp:26
+  auto explicit_initialized_value_used = [j = 1] { return j + 1; };
+  auto explicit_initialized_value_unused = [j = 1] {}; // expected-warning{{lambda capture 'j' is not used}}
+
----------------
aaron.ballman wrote:
> malcolm.parsons wrote:
> > Quuxplusone wrote:
> > > Would this still produce a diagnostic if `decltype(j)` were something complicated like `lock_guard` or `string`? Presumably it should do the same thing, more or less, as the other -Wunused diagnostics, which I think is "don't warn if the constructor/destructor might actually do important work".
> > I was planning to check for that; thanks for the reminder.
> One more complex case:
> ```
> #include <typeinfo>
> 
> struct B {
>   virtual ~B() = default;
> };
> 
> struct D : B {};
> 
> struct C {
>   B& get_d() const;
>   C get_c() const;
> };
> 
> void f() {
>   C c1, c2;
>   [c1, c2] {
>     (void)typeid(c1.get_d());
>     (void)typeid(c2.get_c());
>   }();
> }
> ```
> Hopefully this does not diagnose `c1` as being unused but still diagnoses `c2` as unused.
These tests are run with `-nostdsysteminc`, so I ran it manually:
```
aaron.cpp:17:21: warning: expression with side effects will be evaluated despite being used as an operand to 'typeid'
      [-Wpotentially-evaluated-expression]
    (void)typeid(c1.get_d());
                    ^
aaron.cpp:16:8: warning: lambda capture 'c2' is not required to be captured for use in an unevaluated context [-Wunused-lambda-capture]
  [c1, c2] {
       ^
2 warnings generated.
```


https://reviews.llvm.org/D28467





More information about the cfe-commits mailing list