Rollup merge of #110955 - fee1-dead-contrib:sus-operation, r=compiler-errors

uplift `clippy::clone_double_ref` as `suspicious_double_ref_op`

Split from #109842.

r? ``@compiler-errors``
This commit is contained in:
Dylan DPC 2023-05-02 11:44:52 +05:30 committed by GitHub
commit 40c4ed4994
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 237 additions and 200 deletions

View file

@ -19,18 +19,17 @@ fn main() {
let clone_type_ref = &CloneType(1u32);
let clone_type_ref_clone: CloneType<u32> = clone_type_ref.clone();
// Calling clone on a double reference doesn't warn since the method call itself
// peels the outer reference off
let clone_type_ref = &&CloneType(1u32);
let clone_type_ref_clone: &CloneType<u32> = clone_type_ref.clone();
//~^ WARNING using `.clone()` on a double reference, which returns `&CloneType<u32>`
let non_deref_type = &PlainType(1u32);
let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
//~^ WARNING call to `.deref()` on a reference in this situation does nothing
// Dereferencing a &&T does not warn since it has collapsed the double reference
let non_deref_type = &&PlainType(1u32);
let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
//~^ WARNING using `.deref()` on a double reference, which returns `&PlainType<u32>`
let non_borrow_type = &PlainType(1u32);
let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();
@ -41,7 +40,8 @@ fn main() {
let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();
let xs = ["a", "b", "c"];
let _v: Vec<&str> = xs.iter().map(|x| x.clone()).collect(); // ok, but could use `*x` instead
let _v: Vec<&str> = xs.iter().map(|x| x.clone()).collect(); // could use `*x` instead
//~^ WARNING using `.clone()` on a double reference, which returns `&str`
}
fn generic<T>(non_clone_type: &PlainType<T>) {

View file

@ -11,22 +11,42 @@ note: the lint level is defined here
LL | #![warn(noop_method_call)]
| ^^^^^^^^^^^^^^^^
warning: using `.clone()` on a double reference, which returns `&CloneType<u32>` instead of cloning the inner type
--> $DIR/noop-method-call.rs:23:63
|
LL | let clone_type_ref_clone: &CloneType<u32> = clone_type_ref.clone();
| ^^^^^^^^
|
= note: `#[warn(suspicious_double_ref_op)]` on by default
warning: call to `.deref()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:28:63
--> $DIR/noop-method-call.rs:27:63
|
LL | let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
| ^^^^^^^^ unnecessary method call
|
= note: the type `&PlainType<u32>` which `deref` is being called on is the same as the type returned from `deref`, so the method call does not do anything and can be removed
warning: using `.deref()` on a double reference, which returns `&PlainType<u32>` instead of dereferencing the inner type
--> $DIR/noop-method-call.rs:31:63
|
LL | let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
| ^^^^^^^^
warning: call to `.borrow()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:36:66
--> $DIR/noop-method-call.rs:35:66
|
LL | let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();
| ^^^^^^^^^ unnecessary method call
|
= note: the type `&PlainType<u32>` which `borrow` is being called on is the same as the type returned from `borrow`, so the method call does not do anything and can be removed
warning: using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type
--> $DIR/noop-method-call.rs:43:44
|
LL | let _v: Vec<&str> = xs.iter().map(|x| x.clone()).collect(); // could use `*x` instead
| ^^^^^^^^
warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:48:19
|
@ -43,5 +63,5 @@ LL | non_clone_type.clone();
|
= note: the type `&PlainType<u32>` which `clone` is being called on is the same as the type returned from `clone`, so the method call does not do anything and can be removed
warning: 5 warnings emitted
warning: 8 warnings emitted

View file

@ -0,0 +1,30 @@
#![feature(lazy_cell)]
#![deny(suspicious_double_ref_op, noop_method_call)]
pub fn clone_on_double_ref() {
let x = vec![1];
let y = &&x;
let z: &Vec<_> = y.clone();
//~^ ERROR using `.clone()` on a double reference, which returns `&Vec<i32>`
println!("{:p} {:p}", *y, z);
}
use std::sync::LazyLock;
pub static STRS: LazyLock<&str> = LazyLock::new(|| "First");
// https://github.com/rust-lang/rust-clippy/issues/9272
fn rust_clippy_issue_9272() {
let str = STRS.clone();
println!("{str}")
}
fn check(mut encoded: &[u8]) {
let _ = &mut encoded.clone();
//~^ ERROR call to `.clone()` on a reference in this situation does nothing
let _ = &encoded.clone();
//~^ ERROR call to `.clone()` on a reference in this situation does nothing
}
fn main() {}

View file

@ -0,0 +1,35 @@
error: using `.clone()` on a double reference, which returns `&Vec<i32>` instead of cloning the inner type
--> $DIR/suspicious-double-ref-op.rs:7:23
|
LL | let z: &Vec<_> = y.clone();
| ^^^^^^^^
|
note: the lint level is defined here
--> $DIR/suspicious-double-ref-op.rs:2:9
|
LL | #![deny(suspicious_double_ref_op, noop_method_call)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: call to `.clone()` on a reference in this situation does nothing
--> $DIR/suspicious-double-ref-op.rs:24:25
|
LL | let _ = &mut encoded.clone();
| ^^^^^^^^ unnecessary method call
|
= note: the type `&[u8]` which `clone` is being called on is the same as the type returned from `clone`, so the method call does not do anything and can be removed
note: the lint level is defined here
--> $DIR/suspicious-double-ref-op.rs:2:35
|
LL | #![deny(suspicious_double_ref_op, noop_method_call)]
| ^^^^^^^^^^^^^^^^
error: call to `.clone()` on a reference in this situation does nothing
--> $DIR/suspicious-double-ref-op.rs:26:21
|
LL | let _ = &encoded.clone();
| ^^^^^^^^ unnecessary method call
|
= note: the type `&[u8]` which `clone` is being called on is the same as the type returned from `clone`, so the method call does not do anything and can be removed
error: aborting due to 3 previous errors