Skip to content

Spurious -Wunused-lambda-capture in presence of "if constexpr" #34798

@llvmbot

Description

@llvmbot
Bugzilla Link 35450
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @AaronBallman,@zmodem,@JVApen,@Meinersbur

Extended Description

The warning -Wunused-lambda-capture may indicate that a capture is unused if the body of the lambda contains an if constexpr construct and the capture is only used in the branch which was not instantiated.

Although Clang is technically correct that the capture is not used, the warning is unhelpful, since the capture cannot simply be removed without breaking cases that instantiate the the other branch.

If this is hard to fix "correctly", then Clang should suppress this warning for lambdas that contain an if-constexpr block.

Example:

template <bool b>
int foo(int a) {
  auto f = [&a]() {
    if constexpr(b) {
      return a;
    } else {
      return 0;
    }
  };
  return f();
}

void bar() {
  foo<false>(123);  // generates unused-capture warning in instantiation
  foo<true>(123);   // no warning
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillac++17clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerfalse-positiveWarning fires when it should not

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions