Skip to content

Commit

Permalink
Ignore fake read access
Browse files Browse the repository at this point in the history
  • Loading branch information
schubart committed Apr 11, 2023
1 parent 63030ac commit 008ba73
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
9 changes: 9 additions & 0 deletions clippy_lints/src/collection_is_never_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ fn has_no_read_access<'tcx>(cx: &LateContext<'tcx>, id: HirId, block: &'tcx Bloc
if let Some(Node::Stmt(..)) = get_parent_node(cx.tcx, parent.hir_id) {
return ControlFlow::Continue(());
}

// The method call is not a statement, so its return value is used somehow but its type is the
// unit type, so this is not a real read access. Examples:
//
// let y = x.clear();
// println!("{:?}", x.clear());
if cx.typeck_results().expr_ty(parent).is_unit() {
return ControlFlow::Continue(());
}
}

// Any other access to `id` is a read access. Stop searching.
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/collection_is_never_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ fn shadowing_2() {

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

Expand Down
20 changes: 13 additions & 7 deletions tests/ui/collection_is_never_read.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,46 @@ LL | let mut x = HashMap::new(); // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: collection is never read
--> $DIR/collection_is_never_read.rs:95:5
--> $DIR/collection_is_never_read.rs:88:5
|
LL | let mut x = vec![1, 2, 3]; // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: collection is never read
--> $DIR/collection_is_never_read.rs:102:5
--> $DIR/collection_is_never_read.rs:94:5
|
LL | let mut x = vec![1, 2, 3]; // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: collection is never read
--> $DIR/collection_is_never_read.rs:119:5
--> $DIR/collection_is_never_read.rs:101:5
|
LL | let mut x = vec![1, 2, 3]; // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: collection is never read
--> $DIR/collection_is_never_read.rs:118:5
|
LL | let mut x = HashSet::new(); // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: collection is never read
--> $DIR/collection_is_never_read.rs:133:5
--> $DIR/collection_is_never_read.rs:132:5
|
LL | let x = vec![1, 2, 3]; // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^

error: collection is never read
--> $DIR/collection_is_never_read.rs:169:5
--> $DIR/collection_is_never_read.rs:168:5
|
LL | let mut s = String::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: collection is never read
--> $DIR/collection_is_never_read.rs:182:5
--> $DIR/collection_is_never_read.rs:181:5
|
LL | let mut s = String::from("Hello, World!");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 10 previous errors
error: aborting due to 11 previous errors

0 comments on commit 008ba73

Please sign in to comment.