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

let_underscore_drop complains about idiomatic let _ = ignorable_result; #8003

Closed
birkenfeld opened this issue Nov 20, 2021 · 6 comments · Fixed by #9697
Closed

let_underscore_drop complains about idiomatic let _ = ignorable_result; #8003

birkenfeld opened this issue Nov 20, 2021 · 6 comments · Fixed by #9697
Labels
S-needs-discussion Status: Needs further discussion before merging or work can be started

Comments

@birkenfeld
Copy link
Contributor

birkenfeld commented Nov 20, 2021

Lint name: let_underscore_drop

I tried this code:

#![allow(unused)]
#![deny(clippy::let_underscore_drop)]

struct Test;

impl Test {
    fn causes_error(&self) -> Result<(), std::io::Error> {
        todo!()
    }
    
    fn bug(&self) {
        let _ = self.causes_error();
    }
}

I expected to see this happen: no clippy errors

Instead, this happened: clippy wants to keep the Result around until end of scope, while let _ = is the normally suggested way to deal with results where there is no useful Ok and the Err needs to be ignored.

[edit: this originally suggested that the Drop bound is wrongly inferred.]

Meta

Rust version: Nightly on Playground (2021-11-19 a77da2d)

@birkenfeld birkenfeld added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Nov 20, 2021
@smoelius
Copy link
Contributor

Maybe this is correct? Repr::Custom wraps a Box and Box implements Drop.
https://doc.rust-lang.org/src/std/io/error.rs.html#74
https://doc.rust-lang.org/std/boxed/struct.Box.html#impl-Drop

@birkenfeld
Copy link
Contributor Author

You're right, the Drop bound doesn't work properly. std::mem::needs_drop() agrees with you.

However, I will then reformulate the issue: it is idiomatic (at least I thought so) to discard Results that need not be handled with let _ = result; The semantics is also what's desired: no need to keep the Error around until end of scope.

So the lint should maybe just get an exception for the Result case?

@birkenfeld birkenfeld changed the title let_underscore_drop thinks std::io::Result<()> implements Drop let_underscore_drop complains about idiomatic let _ = ignorable_result; Nov 21, 2021
@xFrednet
Copy link
Member

xFrednet commented Nov 21, 2021

I am not convinced that Result should get a pass out of the box, there are probably cases where the error should be keep. On the other hand, this would also be a bit weird. I'm not quite sure what the best course of action would be, we could introduce a configuration value for it. 🤔

@xFrednet xFrednet added S-needs-discussion Status: Needs further discussion before merging or work can be started and removed C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Nov 21, 2021
@birkenfeld
Copy link
Contributor Author

Sure it is only pedantic, but clippy directly contradicts idiomatic use here. And I'd be very interested to see an error with an intended scope-like effect :)

@xFrednet
Copy link
Member

Thinking about it more, I agree that it would make sense to allow this lint for the Result type, at least if only the Error type implements drop. Maybe we could still have a configuration value, but the default configuration could allow Result by default. :)

@doy
Copy link

doy commented Dec 14, 2021

would it be possible to determine whether the only Drop implementation found is for Box, and just ignore that case? that feels like it would cover most issues here, and Box::drop seems irrelevant to the issue that this lint is trying to catch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-needs-discussion Status: Needs further discussion before merging or work can be started
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants