Skip to content

False positive lifetime GSL warning for no-capture lambda? #126600

Open
@smeenai

Description

@smeenai

For the code in https://godbolt.org/z/v919qqe6o:

struct [[gsl::Pointer]] function_ref {
  template <typename Callable>
  function_ref(Callable &&callable [[clang::lifetimebound]]) : ref(callable) {}
  void (*ref)();
};

void f();
void g(function_ref &r) {
  r = []() { f(); };
}

Clang trunk produces a warning:

warning: object backing the pointer r will be destroyed at the end of the full-expression [-Wdangling-assignment-gsl]
    9 |   r = []() { f(); };

Clang seems to consider the no-capture lambda as a temporary that will be destroyed at the end of the expression. I'm not a standards expert and I haven't found anything definitive through searching, but in practice no-capture lambdas will turn into ordinary functions, so there's no actual issue here. Should we actually be warning for this?

a3b4d91 is a commit that ran into this and changed the code slightly to avoid it (the code change made there would result in a stack-use-after-free were this an actual issue, but it avoids the warning). #126140 is a high-firing instance of this warning, and we'd appreciate some guidance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang: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