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 warns even when drop order is not affected #6841

Open
saethlin opened this issue Mar 4, 2021 · 3 comments
Open

let_underscore_drop warns even when drop order is not affected #6841

saethlin opened this issue Mar 4, 2021 · 3 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have S-needs-discussion Status: Needs further discussion before merging or work can be started

Comments

@saethlin
Copy link
Member

saethlin commented Mar 4, 2021

Lint name: clippy::let_underscore_drop

In this context, the semantics of _ do not alter drop order (which is the reason the lint exists, I think?) but the lint fires anyway:

struct FlushOnDrop<T: std::io::Write>(T);

impl<T: std::io::Write> Drop for FlushOnDrop<T> {
    fn drop(&mut self) {
        let _ = self.0.flush();
    }
}

Meta

  • cargo clippy -V: clippy 0.1.52 (476acbf 2021-03-03)
  • rustc -Vv:
    rustc 1.52.0-nightly (476acbf1e 2021-03-03)
    binary: rustc
    commit-hash: 476acbf1e9965b5e95c90f0d7d658709812b7003
    commit-date: 2021-03-03
    host: x86_64-unknown-linux-gnu
    release: 1.52.0-nightly
    LLVM version: 11.0.1
    
@saethlin saethlin 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 Mar 4, 2021
@camsteffen
Copy link
Contributor

I would prefer to keep the rules of let_underscore_drop simple. You could change to let _result = or use drop.

@camsteffen camsteffen added the S-needs-discussion Status: Needs further discussion before merging or work can be started label Mar 8, 2021
@repi
Copy link

repi commented May 8, 2021

Want to use this lint but this is the main reason we do not use it, there is no change in semantics or flow here so requires a lot of unnecessary changes for it to not warn about these cases.

@camsteffen
Copy link
Contributor

Related: #6842 (maybe we can introduce a workaround that a type annotation let _: Result<_, _> = .. will suppress the lint)

The idea of the lint is that using let _ = .. to drop a value is not explicit enough and drop(..) should be used instead. Even if that does not change semantics, it still affects readability of the code since it is not as plainly obvious that a Drop occurs. If you think that let _ = is readable enough, then I don't think this lint is for you. And that is a perfectly acceptable preference to have, which is why the lint is pedantic.

kraktus added a commit to kraktus/reference that referenced this issue May 4, 2022
Raised by rust-lang/rust-clippy#6841, as it is safer in case of refactoring
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have S-needs-discussion Status: Needs further discussion before merging or work can be started
Projects
None yet
Development

No branches or pull requests

3 participants