Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
llvmbot opened this issue Nov 28, 2017 · 6 comments
Open

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

llvmbot opened this issue Nov 28, 2017 · 6 comments
Labels
bugzilla Issues migrated from bugzilla c++17 clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer false-positive Warning fires when it should not

Comments

@llvmbot
Copy link
Member

llvmbot commented Nov 28, 2017

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
}
@zmodem
Copy link
Collaborator

zmodem commented Feb 25, 2020

Sorry, this does not seem to be getting fixed for clang 10, and I don't think we can block on it.

@JVApen
Copy link

JVApen commented Oct 2, 2020

Is there any update on when this can get fixed?

@Meinersbur
Copy link
Member

Also causes problems when compiling flang: https://reviews.llvm.org/rGd4a1db4f3fd7

@AaronBallman
Copy link
Collaborator

I can confirm that this behavior still appears on trunk.

When instantiating the template for foo(), we only transform the non-discarded branch when the if statement condition has a known value.

if (!ConstexprConditionValue || *ConstexprConditionValue) {

if (!ConstexprConditionValue || !*ConstexprConditionValue) {

Because we only transform one branch per instantiation, we don't ever mark the capture as being used. However, this is done according to the standard: https://eel.is/c++draft/stmt.if#2.sentence-3

Because we can't instantiate the branch-not-taken when it's no longer dependent, I think we have to decide whether we'd rather have false positives or false negatives. Given that a false positive is trivial to workaround (add (void)a; to the lambda body outside of the branch), it seems reasonable to me that we prefer false positives to false negatives (it'd be a bummer for the "show me explicit captures I don't need" diagnostic couldn't tell you about a capture you don't need because of a possibly unrelated if constexpr in the lambda body).

@chrchr-github
Copy link

With trunk:
https://godbolt.org/z/vsPq3s8eG

@EugeneZelenko EugeneZelenko added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer false-positive Warning fires when it should not labels Nov 5, 2023
@kdkavanagh
Copy link

Confirmed still an issue in v18.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c++17 clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer false-positive Warning fires when it should not
Projects
None yet
Development

No branches or pull requests

8 participants