Skip to content

Warn about static lambda capturing local variable by reference #111316

Open
@HighCommander4

Description

@HighCommander4

Capturing a local variable by reference in a static lambda is a recipe for undefined behaviour:

int foo() {
  int result = 0;
  static auto lambda = [&result] {
    ++result;
  };
  lambda();
  return result;
}

int main() {
  foo();
  foo();  // oops, the lambda is modifying the `result` variable from the first call
}

It would be nice if clang warned about this.

(Such a bug was caught recently in the LLVM codebase itself, #109367.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions