Skip to content

-Wunused-variable triggers for temporaries with side effects when bound to a reference #54489

@cor3ntin

Description

@cor3ntin

The following code warns on guard2 and guard3 (but not guard) despite all 3 variable having side effects on destruction.
This feels inconsistent and surprising.
GCC emits no warning in any of these cases.

struct RAIIWrapper {
    RAIIWrapper();
    ~RAIIWrapper();
};

void foo() {
    auto const  guard = RAIIWrapper();
    auto const& guard2 = RAIIWrapper(); //unused variable 'guard2'
    auto && guard3 = RAIIWrapper(); //unused variable 'guard3'
}

https://godbolt.org/z/zr7dsevoc

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