[useless_asref]: check that the clone receiver is the local

This commit is contained in:
y21 2024-01-12 13:39:42 +01:00
parent e899684254
commit 153b83f61b
4 changed files with 104 additions and 4 deletions

View file

@ -144,6 +144,37 @@ fn foo() {
//~^ ERROR: this call to `as_ref.map(...)` does nothing
}
mod issue12135 {
pub struct Struct {
field: Option<InnerStruct>,
}
#[derive(Clone)]
pub struct Foo;
#[derive(Clone)]
struct InnerStruct {
x: Foo,
}
impl InnerStruct {
fn method(&self) -> &Foo {
&self.x
}
}
pub fn f(x: &Struct) -> Option<Foo> {
x.field.clone();
//~^ ERROR: this call to `as_ref.map(...)` does nothing
x.field.clone();
//~^ ERROR: this call to `as_ref.map(...)` does nothing
x.field.clone();
//~^ ERROR: this call to `as_ref.map(...)` does nothing
x.field.as_ref().map(|v| v.method().clone())
}
}
fn main() {
not_ok();
ok();

View file

@ -144,6 +144,37 @@ fn foo() {
//~^ ERROR: this call to `as_ref.map(...)` does nothing
}
mod issue12135 {
pub struct Struct {
field: Option<InnerStruct>,
}
#[derive(Clone)]
pub struct Foo;
#[derive(Clone)]
struct InnerStruct {
x: Foo,
}
impl InnerStruct {
fn method(&self) -> &Foo {
&self.x
}
}
pub fn f(x: &Struct) -> Option<Foo> {
x.field.as_ref().map(|v| v.clone());
//~^ ERROR: this call to `as_ref.map(...)` does nothing
x.field.as_ref().map(Clone::clone);
//~^ ERROR: this call to `as_ref.map(...)` does nothing
x.field.as_ref().map(|v| Clone::clone(v));
//~^ ERROR: this call to `as_ref.map(...)` does nothing
x.field.as_ref().map(|v| v.method().clone())
}
}
fn main() {
not_ok();
ok();

View file

@ -88,5 +88,23 @@ error: this call to `as_ref.map(...)` does nothing
LL | let z = x.as_ref().map(|z| String::clone(z));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `x.clone()`
error: aborting due to 14 previous errors
error: this call to `as_ref.map(...)` does nothing
--> $DIR/useless_asref.rs:167:9
|
LL | x.field.as_ref().map(|v| v.clone());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `x.field.clone()`
error: this call to `as_ref.map(...)` does nothing
--> $DIR/useless_asref.rs:169:9
|
LL | x.field.as_ref().map(Clone::clone);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `x.field.clone()`
error: this call to `as_ref.map(...)` does nothing
--> $DIR/useless_asref.rs:171:9
|
LL | x.field.as_ref().map(|v| Clone::clone(v));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `x.field.clone()`
error: aborting due to 17 previous errors