Skip to content

Extend -Wdangling to handle assignments, not just initializations #63310

@higher-performance

Description

@higher-performance

Currently, -Wdangling/-Wdangling-gsl detect the following:

std::string foo() { return ""; }
int main() {
    std::string_view v = foo();  // error: object backing the pointer will be destroyed at the end of the full-expression [-Werror,-Wdangling-gsl]
}

However, they fail to detect this case:

std::string foo() { return ""; }
int main() {
    std::string_view v;
    v = foo();  // no error
}

It seems the compiler currently only handles initializations, not regular assignments, despite them posing the same issue. So it would be great to handle the second case as well.

More generally, if a lifetimebound object is assigned to a non-temporary expression, then I believe it would be correct to issue the diagnostic.

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