<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Spurious -Wunused-lambda-capture for RAII object (e.g. reference-counted pointer)"
   href="https://bugs.llvm.org/show_bug.cgi?id=31977">31977</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Spurious -Wunused-lambda-capture for RAII object (e.g. reference-counted pointer)
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++14
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>dholbert@mozilla.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>I've just tried building Firefox with tip-of-trunk clang today (rev 295219),
but I'm getting a bunch of -Wunused-lambda-capture build warnings for a
perfectly-reasonable code pattern.

In particular:
 - Firefox's source code uses a RAII "RefPtr" struct to manage
reference-counted pointers
 - We have a common pattern where we lambda-capture a RefPtr to a resource, for
the duration of a lambda which e.g. operates on some member variables of that
resource.
 - The lambda-captured RefPtr may not be used, but we depend on it being
captured to keep the resource (and its mebmer-vars, data, whatever) alive so
that the lambda can safely operate on them.

Would it be possible to soften -Wunused-lambda-capture so that it doesn't warn
for this case?  This seems like a handy warning, but we'll probably have to
disable it for the Firefox build process if it stays in its current state.

Here's some sample code that triggers this issue (using printf instead of
reference counting in the contstuctor/destructor):
////////////////////
#include <stdio.h>

struct MyRaiiThing {
  MyRaiiThing() {
    printf("Constructing MyRaiiThing\n");
  }
  MyRaiiThing(const MyRaiiThing& other) {
    printf("Copy-constructing MyRaiiThing\n");
  }
  ~MyRaiiThing() {
    printf("Destructing MyRaiiThing\n");
  }
};

int main() {
  MyRaiiThing thing;
  auto lambda = []() {
    printf("Inside lambda\n");
  };
  lambda();
}</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>