Ignore fake read access

This commit is contained in:
Michael Schubart 2023-03-12 14:56:14 +00:00
parent 63030acf4f
commit 008ba7326b
3 changed files with 23 additions and 9 deletions

View File

@ -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.

View File

@ -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();
}

View File

@ -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