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

FN collection_is_never_read: grabbing unit value of push() #10488

Closed
matthiaskrgr opened this issue Mar 12, 2023 · 2 comments · Fixed by #10492
Closed

FN collection_is_never_read: grabbing unit value of push() #10488

matthiaskrgr opened this issue Mar 12, 2023 · 2 comments · Fixed by #10492
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Mar 12, 2023

Summary

,

Lint Name

collection_is_never_read

Reproducer

I tried this code:

#![warn(clippy::collection_is_never_read)]

pub fn main() {
    let mut x: Vec<_> = vec![1, 2, 3];
    x.push(4);
    // the println() is read as a use, but the .push() returns () so we still never actually use the resulting vec
    println!("{:?}", x.push(5));
}

I expected to see this happen:
Lint trigggers

Instead, this happened:
Linnt doesn't trigger

Version

rustc 1.70.0-nightly (8a73f50d8 2023-03-11)
binary: rustc
commit-hash: 8a73f50d875840b8077b8ec080fa41881d7ce40d
commit-date: 2023-03-11
host: x86_64-unknown-linux-gnu
release: 1.70.0-nightly
LLVM version: 15.0.7
@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Mar 12, 2023
@schubart
Copy link
Contributor

@rustbot claim

@schubart
Copy link
Contributor

schubart commented Mar 12, 2023

This is known / expected behaviour. There's a test for it:

#[allow(clippy::let_unit_value)]
fn fake_read() {
    let mut x = vec![1, 2, 3]; // Ok
    x.reverse();
    // `collection_is_never_read` gets fooled, but other lints should catch this.
    let _: () = x.clear();
}

Aren't there other lints that catch that println!("{:?}", x.push(5)); is pointless?

Are there valid use cases for such code?

I wonder if instead of special-casing the unit type in collection_is_never_read, there should be a new lint that catches this kind of "usage" of unit type values.

(I'm not opposed to changing collection_is_never_read, but I'd like to explore this first.)

schubart added a commit to schubart/rust-clippy that referenced this issue Mar 12, 2023
schubart added a commit to schubart/rust-clippy that referenced this issue Apr 11, 2023
@bors bors closed this as completed in 3b5b2ed Apr 11, 2023
J-ZhengLi pushed a commit to TogetherGame/rust-clippy that referenced this issue Aug 26, 2023
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-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants