Skip to content

Commit 2a76c57

Browse files
committed
add suggestion
1 parent 626efab commit 2a76c57

7 files changed

+127
-73
lines changed

compiler/rustc_lint/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ lint_non_upper_case_global = {$sort} `{$name}` should have an upper case name
412412
.label = should have an UPPER_CASE name
413413
414414
lint_noop_method_call = call to `.{$method}()` on a reference in this situation does nothing
415-
.label = unnecessary method call
415+
.suggestion = remove this redundant call
416416
.note = the type `{$orig_ty}` does not implement `{$trait_}`, so calling `{$method}` on `&{$orig_ty}` copies the reference, which does not do anything and can be removed
417417
418418
lint_only_cast_u8_to_char = only `u8` can be cast into `char`

compiler/rustc_lint/src/lints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ pub struct NoopMethodCallDiag<'a> {
12331233
pub method: Symbol,
12341234
pub orig_ty: Ty<'a>,
12351235
pub trait_: Symbol,
1236-
#[label]
1236+
#[suggestion(code = "", applicability = "machine-applicable")]
12371237
pub label: Span,
12381238
}
12391239

tests/ui/lint/noop-method-call.fixed

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// check-pass
2+
// run-rustfix
3+
4+
#![allow(unused)]
5+
6+
use std::borrow::Borrow;
7+
use std::ops::Deref;
8+
9+
struct PlainType<T>(T);
10+
11+
#[derive(Clone)]
12+
struct CloneType<T>(T);
13+
14+
fn check(mut encoded: &[u8]) {
15+
let _ = &mut encoded;
16+
//~^ WARN call to `.clone()` on a reference in this situation does nothing
17+
let _ = &encoded;
18+
//~^ WARN call to `.clone()` on a reference in this situation does nothing
19+
}
20+
21+
fn main() {
22+
let non_clone_type_ref = &PlainType(1u32);
23+
let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref;
24+
//~^ WARN call to `.clone()` on a reference in this situation does nothing
25+
26+
let clone_type_ref = &CloneType(1u32);
27+
let clone_type_ref_clone: CloneType<u32> = clone_type_ref.clone();
28+
29+
30+
let non_deref_type = &PlainType(1u32);
31+
let non_deref_type_deref: &PlainType<u32> = non_deref_type;
32+
//~^ WARN call to `.deref()` on a reference in this situation does nothing
33+
34+
let non_borrow_type = &PlainType(1u32);
35+
let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type;
36+
//~^ WARN call to `.borrow()` on a reference in this situation does nothing
37+
38+
// Borrowing a &&T does not warn since it has collapsed the double reference
39+
let non_borrow_type = &&PlainType(1u32);
40+
let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();
41+
}
42+
43+
fn generic<T>(non_clone_type: &PlainType<T>) {
44+
non_clone_type;
45+
//~^ WARN call to `.clone()` on a reference in this situation does nothing
46+
}
47+
48+
fn non_generic(non_clone_type: &PlainType<u32>) {
49+
non_clone_type;
50+
//~^ WARN call to `.clone()` on a reference in this situation does nothing
51+
}

tests/ui/lint/noop-method-call.rs

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// check-pass
2+
// run-rustfix
23

34
#![allow(unused)]
45

@@ -10,45 +11,41 @@ struct PlainType<T>(T);
1011
#[derive(Clone)]
1112
struct CloneType<T>(T);
1213

14+
fn check(mut encoded: &[u8]) {
15+
let _ = &mut encoded.clone();
16+
//~^ WARN call to `.clone()` on a reference in this situation does nothing
17+
let _ = &encoded.clone();
18+
//~^ WARN call to `.clone()` on a reference in this situation does nothing
19+
}
20+
1321
fn main() {
1422
let non_clone_type_ref = &PlainType(1u32);
1523
let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref.clone();
16-
//~^ WARNING call to `.clone()` on a reference in this situation does nothing
24+
//~^ WARN call to `.clone()` on a reference in this situation does nothing
1725

1826
let clone_type_ref = &CloneType(1u32);
1927
let clone_type_ref_clone: CloneType<u32> = clone_type_ref.clone();
2028

21-
let clone_type_ref = &&CloneType(1u32);
22-
let clone_type_ref_clone: &CloneType<u32> = clone_type_ref.clone();
23-
//~^ WARNING using `.clone()` on a double reference, which returns `&CloneType<u32>`
2429

2530
let non_deref_type = &PlainType(1u32);
2631
let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
27-
//~^ WARNING call to `.deref()` on a reference in this situation does nothing
28-
29-
let non_deref_type = &&PlainType(1u32);
30-
let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
31-
//~^ WARNING using `.deref()` on a double reference, which returns `&PlainType<u32>`
32+
//~^ WARN call to `.deref()` on a reference in this situation does nothing
3233

3334
let non_borrow_type = &PlainType(1u32);
3435
let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();
35-
//~^ WARNING call to `.borrow()` on a reference in this situation does nothing
36+
//~^ WARN call to `.borrow()` on a reference in this situation does nothing
3637

3738
// Borrowing a &&T does not warn since it has collapsed the double reference
3839
let non_borrow_type = &&PlainType(1u32);
3940
let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();
40-
41-
let xs = ["a", "b", "c"];
42-
let _v: Vec<&str> = xs.iter().map(|x| x.clone()).collect(); // could use `*x` instead
43-
//~^ WARNING using `.clone()` on a double reference, which returns `&str`
4441
}
4542

4643
fn generic<T>(non_clone_type: &PlainType<T>) {
4744
non_clone_type.clone();
48-
//~^ WARNING call to `.clone()` on a reference in this situation does nothing
45+
//~^ WARN call to `.clone()` on a reference in this situation does nothing
4946
}
5047

5148
fn non_generic(non_clone_type: &PlainType<u32>) {
5249
non_clone_type.clone();
53-
//~^ WARNING call to `.clone()` on a reference in this situation does nothing
50+
//~^ WARN call to `.clone()` on a reference in this situation does nothing
5451
}

tests/ui/lint/noop-method-call.stderr

+26-30
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,59 @@
11
warning: call to `.clone()` on a reference in this situation does nothing
2-
--> $DIR/noop-method-call.rs:15:71
2+
--> $DIR/noop-method-call.rs:15:25
33
|
4-
LL | let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref.clone();
5-
| ^^^^^^^^ unnecessary method call
4+
LL | let _ = &mut encoded.clone();
5+
| ^^^^^^^^ help: remove this redundant call
66
|
7-
= note: the type `PlainType<u32>` does not implement `Clone`, so calling `clone` on `&PlainType<u32>` copies the reference, which does not do anything and can be removed
7+
= note: the type `[u8]` does not implement `Clone`, so calling `clone` on `&[u8]` copies the reference, which does not do anything and can be removed
88
= note: `#[warn(noop_method_call)]` on by default
99

10-
warning: using `.clone()` on a double reference, which returns `&CloneType<u32>` instead of cloning the inner type
11-
--> $DIR/noop-method-call.rs:22:63
10+
warning: call to `.clone()` on a reference in this situation does nothing
11+
--> $DIR/noop-method-call.rs:17:21
1212
|
13-
LL | let clone_type_ref_clone: &CloneType<u32> = clone_type_ref.clone();
14-
| ^^^^^^^^
13+
LL | let _ = &encoded.clone();
14+
| ^^^^^^^^ help: remove this redundant call
1515
|
16-
= note: `#[warn(suspicious_double_ref_op)]` on by default
16+
= note: the type `[u8]` does not implement `Clone`, so calling `clone` on `&[u8]` copies the reference, which does not do anything and can be removed
1717

18-
warning: call to `.deref()` on a reference in this situation does nothing
19-
--> $DIR/noop-method-call.rs:26:63
18+
warning: call to `.clone()` on a reference in this situation does nothing
19+
--> $DIR/noop-method-call.rs:23:71
2020
|
21-
LL | let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
22-
| ^^^^^^^^ unnecessary method call
21+
LL | let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref.clone();
22+
| ^^^^^^^^ help: remove this redundant call
2323
|
24-
= note: the type `PlainType<u32>` does not implement `Deref`, so calling `deref` on `&PlainType<u32>` copies the reference, which does not do anything and can be removed
24+
= note: the type `PlainType<u32>` does not implement `Clone`, so calling `clone` on `&PlainType<u32>` copies the reference, which does not do anything and can be removed
2525

26-
warning: using `.deref()` on a double reference, which returns `&PlainType<u32>` instead of dereferencing the inner type
27-
--> $DIR/noop-method-call.rs:30:63
26+
warning: call to `.deref()` on a reference in this situation does nothing
27+
--> $DIR/noop-method-call.rs:31:63
2828
|
2929
LL | let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
30-
| ^^^^^^^^
30+
| ^^^^^^^^ help: remove this redundant call
31+
|
32+
= note: the type `PlainType<u32>` does not implement `Deref`, so calling `deref` on `&PlainType<u32>` copies the reference, which does not do anything and can be removed
3133

3234
warning: call to `.borrow()` on a reference in this situation does nothing
33-
--> $DIR/noop-method-call.rs:34:66
35+
--> $DIR/noop-method-call.rs:35:66
3436
|
3537
LL | let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();
36-
| ^^^^^^^^^ unnecessary method call
38+
| ^^^^^^^^^ help: remove this redundant call
3739
|
3840
= note: the type `PlainType<u32>` does not implement `Borrow`, so calling `borrow` on `&PlainType<u32>` copies the reference, which does not do anything and can be removed
3941

40-
warning: using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type
41-
--> $DIR/noop-method-call.rs:42:44
42-
|
43-
LL | let _v: Vec<&str> = xs.iter().map(|x| x.clone()).collect(); // could use `*x` instead
44-
| ^^^^^^^^
45-
4642
warning: call to `.clone()` on a reference in this situation does nothing
47-
--> $DIR/noop-method-call.rs:47:19
43+
--> $DIR/noop-method-call.rs:44:19
4844
|
4945
LL | non_clone_type.clone();
50-
| ^^^^^^^^ unnecessary method call
46+
| ^^^^^^^^ help: remove this redundant call
5147
|
5248
= note: the type `PlainType<T>` does not implement `Clone`, so calling `clone` on `&PlainType<T>` copies the reference, which does not do anything and can be removed
5349

5450
warning: call to `.clone()` on a reference in this situation does nothing
55-
--> $DIR/noop-method-call.rs:52:19
51+
--> $DIR/noop-method-call.rs:49:19
5652
|
5753
LL | non_clone_type.clone();
58-
| ^^^^^^^^ unnecessary method call
54+
| ^^^^^^^^ help: remove this redundant call
5955
|
6056
= note: the type `PlainType<u32>` does not implement `Clone`, so calling `clone` on `&PlainType<u32>` copies the reference, which does not do anything and can be removed
6157

62-
warning: 8 warnings emitted
58+
warning: 7 warnings emitted
6359

tests/ui/lint/suspicious-double-ref-op.rs

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#![feature(lazy_cell)]
22
#![deny(suspicious_double_ref_op, noop_method_call)]
33

4+
use std::borrow::Borrow;
5+
use std::ops::Deref;
6+
7+
struct PlainType<T>(T);
8+
9+
#[derive(Clone)]
10+
struct CloneType<T>(T);
11+
412
pub fn clone_on_double_ref() {
513
let x = vec![1];
614
let y = &&x;
@@ -20,11 +28,16 @@ fn rust_clippy_issue_9272() {
2028
println!("{str}")
2129
}
2230

23-
fn check(mut encoded: &[u8]) {
24-
let _ = &mut encoded.clone();
25-
//~^ ERROR call to `.clone()` on a reference in this situation does nothing
26-
let _ = &encoded.clone();
27-
//~^ ERROR call to `.clone()` on a reference in this situation does nothing
28-
}
31+
fn main() {
32+
let clone_type_ref = &&CloneType(1u32);
33+
let clone_type_ref_clone: &CloneType<u32> = clone_type_ref.clone();
34+
//~^ ERROR using `.clone()` on a double reference, which returns `&CloneType<u32>`
35+
36+
let non_deref_type = &&PlainType(1u32);
37+
let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
38+
//~^ ERROR using `.deref()` on a double reference, which returns `&PlainType<u32>`
2939

30-
fn main() {}
40+
let xs = ["a", "b", "c"];
41+
let _v: Vec<&str> = xs.iter().map(|x| x.clone()).collect(); // could use `*x` instead
42+
//~^ ERROR using `.clone()` on a double reference, which returns `&str`
43+
}
+15-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: using `.clone()` on a double reference, which returns `&Vec<i32>` instead of cloning the inner type
2-
--> $DIR/suspicious-double-ref-op.rs:7:23
2+
--> $DIR/suspicious-double-ref-op.rs:15:23
33
|
44
LL | let z: &Vec<_> = y.clone();
55
| ^^^^^^^^
@@ -10,26 +10,23 @@ note: the lint level is defined here
1010
LL | #![deny(suspicious_double_ref_op, noop_method_call)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
error: call to `.clone()` on a reference in this situation does nothing
14-
--> $DIR/suspicious-double-ref-op.rs:24:25
13+
error: using `.clone()` on a double reference, which returns `&CloneType<u32>` instead of cloning the inner type
14+
--> $DIR/suspicious-double-ref-op.rs:33:63
1515
|
16-
LL | let _ = &mut encoded.clone();
17-
| ^^^^^^^^ unnecessary method call
18-
|
19-
= note: the type `[u8]` does not implement `Clone`, so calling `clone` on `&[u8]` copies the reference, which does not do anything and can be removed
20-
note: the lint level is defined here
21-
--> $DIR/suspicious-double-ref-op.rs:2:35
22-
|
23-
LL | #![deny(suspicious_double_ref_op, noop_method_call)]
24-
| ^^^^^^^^^^^^^^^^
16+
LL | let clone_type_ref_clone: &CloneType<u32> = clone_type_ref.clone();
17+
| ^^^^^^^^
2518

26-
error: call to `.clone()` on a reference in this situation does nothing
27-
--> $DIR/suspicious-double-ref-op.rs:26:21
19+
error: using `.deref()` on a double reference, which returns `&PlainType<u32>` instead of dereferencing the inner type
20+
--> $DIR/suspicious-double-ref-op.rs:37:63
2821
|
29-
LL | let _ = &encoded.clone();
30-
| ^^^^^^^^ unnecessary method call
22+
LL | let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
23+
| ^^^^^^^^
24+
25+
error: using `.clone()` on a double reference, which returns `&str` instead of cloning the inner type
26+
--> $DIR/suspicious-double-ref-op.rs:41:44
3127
|
32-
= note: the type `[u8]` does not implement `Clone`, so calling `clone` on `&[u8]` copies the reference, which does not do anything and can be removed
28+
LL | let _v: Vec<&str> = xs.iter().map(|x| x.clone()).collect(); // could use `*x` instead
29+
| ^^^^^^^^
3330

34-
error: aborting due to 3 previous errors
31+
error: aborting due to 4 previous errors
3532

0 commit comments

Comments
 (0)