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

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

Closed
cor3ntin opened this issue Mar 21, 2022 · 2 comments
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer

Comments

@cor3ntin
Copy link
Contributor

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

@cor3ntin cor3ntin added the clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer label Mar 21, 2022
@dwblaikie
Copy link
Collaborator

My personal hope/take on this is that the warning should be aggressive like this (because it lets us warn on things like unused std::string), but we should have an attribute for types with scoped side effects (and then code like RAIIWrapper(), func(); is valid - since func() happens between the wrapper's ctor and dtor, but RAIIWrapper(); is still "unused" - but there might be a third type with arbitrary (not even scoped) side effects, where type_name(); would be valid/not-warned)

@dwblaikie
Copy link
Collaborator

Oh, yeah, I guess this is just about reference lifetime extended variables - this doesn't lead us to being able to warn on std::string currently.

Yeah, we shouldn't warn on the reference extended cases anymore than we should on the direct variable cases.

mem-frob pushed a commit to draperlaboratory/hope-llvm-project that referenced this issue Oct 7, 2022
…ts...

const auto & var = ObjectWithSideEffects();

Fixes llvm/llvm-project#54489

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D122661
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer
Projects
None yet
Development

No branches or pull requests

2 participants