Auto merge of #12783 - shanretoo:fix-assigning-clones, r=blyxyas
fix: use hir_with_context to produce correct snippets for assigning_clones The `assigning_clones` lint is producing wrong output when the assignment is a macro call. Since Applicability level `Unspecified` will never be changed inside `hir_with_applicability`, so it is safe here to replace `hir_with_applicability` with `hir_with_context` to generate snippets of the macro call instead of the expansion. fixes #12776 changelog: [`assigning_clones`]: use `hir_with_context` to produce correct snippets
This commit is contained in:
commit
baf2a23840
4 changed files with 92 additions and 20 deletions
|
|
@ -62,6 +62,16 @@ fn clone_method_rhs_complex(mut_thing: &mut HasCloneFrom, ref_thing: &HasCloneFr
|
|||
mut_thing.clone_from(ref_thing + ref_thing);
|
||||
}
|
||||
|
||||
fn clone_method_macro() {
|
||||
let mut s = String::from("");
|
||||
s.clone_from(&format!("{} {}", "hello", "world"));
|
||||
}
|
||||
|
||||
fn clone_function_macro() {
|
||||
let mut s = String::from("");
|
||||
Clone::clone_from(&mut s, &format!("{} {}", "hello", "world"));
|
||||
}
|
||||
|
||||
fn assign_to_init_mut_var(b: HasCloneFrom) -> HasCloneFrom {
|
||||
let mut a = HasCloneFrom;
|
||||
for _ in 1..10 {
|
||||
|
|
@ -214,6 +224,16 @@ fn owned_function_val(mut mut_thing: String, ref_str: &str) {
|
|||
ToOwned::clone_into(ref_str, &mut mut_thing);
|
||||
}
|
||||
|
||||
fn owned_method_macro() {
|
||||
let mut s = String::from("");
|
||||
format!("{} {}", "hello", "world").clone_into(&mut s);
|
||||
}
|
||||
|
||||
fn owned_function_macro() {
|
||||
let mut s = String::from("");
|
||||
ToOwned::clone_into(&format!("{} {}", "hello", "world"), &mut s);
|
||||
}
|
||||
|
||||
struct FakeToOwned;
|
||||
impl FakeToOwned {
|
||||
/// This looks just like `ToOwned::to_owned`
|
||||
|
|
|
|||
|
|
@ -62,6 +62,16 @@ fn clone_method_rhs_complex(mut_thing: &mut HasCloneFrom, ref_thing: &HasCloneFr
|
|||
*mut_thing = (ref_thing + ref_thing).clone();
|
||||
}
|
||||
|
||||
fn clone_method_macro() {
|
||||
let mut s = String::from("");
|
||||
s = format!("{} {}", "hello", "world").clone();
|
||||
}
|
||||
|
||||
fn clone_function_macro() {
|
||||
let mut s = String::from("");
|
||||
s = Clone::clone(&format!("{} {}", "hello", "world"));
|
||||
}
|
||||
|
||||
fn assign_to_init_mut_var(b: HasCloneFrom) -> HasCloneFrom {
|
||||
let mut a = HasCloneFrom;
|
||||
for _ in 1..10 {
|
||||
|
|
@ -214,6 +224,16 @@ fn owned_function_val(mut mut_thing: String, ref_str: &str) {
|
|||
mut_thing = ToOwned::to_owned(ref_str);
|
||||
}
|
||||
|
||||
fn owned_method_macro() {
|
||||
let mut s = String::from("");
|
||||
s = format!("{} {}", "hello", "world").to_owned();
|
||||
}
|
||||
|
||||
fn owned_function_macro() {
|
||||
let mut s = String::from("");
|
||||
s = ToOwned::to_owned(&format!("{} {}", "hello", "world"));
|
||||
}
|
||||
|
||||
struct FakeToOwned;
|
||||
impl FakeToOwned {
|
||||
/// This looks just like `ToOwned::to_owned`
|
||||
|
|
|
|||
|
|
@ -62,64 +62,88 @@ LL | *mut_thing = (ref_thing + ref_thing).clone();
|
|||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_from()`: `mut_thing.clone_from(ref_thing + ref_thing)`
|
||||
|
||||
error: assigning the result of `Clone::clone()` may be inefficient
|
||||
--> tests/ui/assigning_clones.rs:68:9
|
||||
--> tests/ui/assigning_clones.rs:67:5
|
||||
|
|
||||
LL | s = format!("{} {}", "hello", "world").clone();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_from()`: `s.clone_from(&format!("{} {}", "hello", "world"))`
|
||||
|
||||
error: assigning the result of `Clone::clone()` may be inefficient
|
||||
--> tests/ui/assigning_clones.rs:72:5
|
||||
|
|
||||
LL | s = Clone::clone(&format!("{} {}", "hello", "world"));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_from()`: `Clone::clone_from(&mut s, &format!("{} {}", "hello", "world"))`
|
||||
|
||||
error: assigning the result of `Clone::clone()` may be inefficient
|
||||
--> tests/ui/assigning_clones.rs:78:9
|
||||
|
|
||||
LL | a = b.clone();
|
||||
| ^^^^^^^^^^^^^ help: use `clone_from()`: `a.clone_from(&b)`
|
||||
|
||||
error: assigning the result of `Clone::clone()` may be inefficient
|
||||
--> tests/ui/assigning_clones.rs:139:5
|
||||
--> tests/ui/assigning_clones.rs:149:5
|
||||
|
|
||||
LL | a = b.clone();
|
||||
| ^^^^^^^^^^^^^ help: use `clone_from()`: `a.clone_from(&b)`
|
||||
|
||||
error: assigning the result of `Clone::clone()` may be inefficient
|
||||
--> tests/ui/assigning_clones.rs:146:5
|
||||
--> tests/ui/assigning_clones.rs:156:5
|
||||
|
|
||||
LL | a = b.clone();
|
||||
| ^^^^^^^^^^^^^ help: use `clone_from()`: `a.clone_from(&b)`
|
||||
|
||||
error: assigning the result of `ToOwned::to_owned()` may be inefficient
|
||||
--> tests/ui/assigning_clones.rs:147:5
|
||||
--> tests/ui/assigning_clones.rs:157:5
|
||||
|
|
||||
LL | a = c.to_owned();
|
||||
| ^^^^^^^^^^^^^^^^ help: use `clone_into()`: `c.clone_into(&mut a)`
|
||||
|
||||
error: assigning the result of `ToOwned::to_owned()` may be inefficient
|
||||
--> tests/ui/assigning_clones.rs:177:5
|
||||
--> tests/ui/assigning_clones.rs:187:5
|
||||
|
|
||||
LL | *mut_string = ref_str.to_owned();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(mut_string)`
|
||||
|
||||
error: assigning the result of `ToOwned::to_owned()` may be inefficient
|
||||
--> tests/ui/assigning_clones.rs:181:5
|
||||
--> tests/ui/assigning_clones.rs:191:5
|
||||
|
|
||||
LL | mut_string = ref_str.to_owned();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(&mut mut_string)`
|
||||
|
||||
error: assigning the result of `ToOwned::to_owned()` may be inefficient
|
||||
--> tests/ui/assigning_clones.rs:202:5
|
||||
--> tests/ui/assigning_clones.rs:212:5
|
||||
|
|
||||
LL | **mut_box_string = ref_str.to_owned();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(&mut (*mut_box_string))`
|
||||
|
||||
error: assigning the result of `ToOwned::to_owned()` may be inefficient
|
||||
--> tests/ui/assigning_clones.rs:206:5
|
||||
--> tests/ui/assigning_clones.rs:216:5
|
||||
|
|
||||
LL | **mut_box_string = ref_str.to_owned();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(&mut (*mut_box_string))`
|
||||
|
||||
error: assigning the result of `ToOwned::to_owned()` may be inefficient
|
||||
--> tests/ui/assigning_clones.rs:210:5
|
||||
--> tests/ui/assigning_clones.rs:220:5
|
||||
|
|
||||
LL | *mut_thing = ToOwned::to_owned(ref_str);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ToOwned::clone_into(ref_str, mut_thing)`
|
||||
|
||||
error: assigning the result of `ToOwned::to_owned()` may be inefficient
|
||||
--> tests/ui/assigning_clones.rs:214:5
|
||||
--> tests/ui/assigning_clones.rs:224:5
|
||||
|
|
||||
LL | mut_thing = ToOwned::to_owned(ref_str);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ToOwned::clone_into(ref_str, &mut mut_thing)`
|
||||
|
||||
error: aborting due to 20 previous errors
|
||||
error: assigning the result of `ToOwned::to_owned()` may be inefficient
|
||||
--> tests/ui/assigning_clones.rs:229:5
|
||||
|
|
||||
LL | s = format!("{} {}", "hello", "world").to_owned();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `format!("{} {}", "hello", "world").clone_into(&mut s)`
|
||||
|
||||
error: assigning the result of `ToOwned::to_owned()` may be inefficient
|
||||
--> tests/ui/assigning_clones.rs:234:5
|
||||
|
|
||||
LL | s = ToOwned::to_owned(&format!("{} {}", "hello", "world"));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ToOwned::clone_into(&format!("{} {}", "hello", "world"), &mut s)`
|
||||
|
||||
error: aborting due to 24 previous errors
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue