Skip to content

Commit 8c6eea8

Browse files
committed
Extend implicit_clone to handle to_string calls
1 parent 66697e8 commit 8c6eea8

File tree

9 files changed

+43
-14
lines changed

9 files changed

+43
-14
lines changed

clippy_lints/src/methods/implicit_clone.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,12 @@ pub fn check(cx: &LateContext<'_>, method_name: Symbol, expr: &hir::Expr<'_>, re
4040
}
4141

4242
/// Returns true if the named method can be used to clone the receiver.
43-
/// Note that `to_string` is not flagged by `implicit_clone`. So other lints that call
44-
/// `is_clone_like` and that do flag `to_string` must handle it separately. See, e.g.,
45-
/// `is_to_owned_like` in `unnecessary_to_owned.rs`.
4643
pub fn is_clone_like(cx: &LateContext<'_>, method_name: Symbol, method_def_id: hir::def_id::DefId) -> bool {
4744
match method_name {
4845
sym::to_os_string => is_diag_item_method(cx, method_def_id, sym::OsStr),
4946
sym::to_owned => is_diag_trait_item(cx, method_def_id, sym::ToOwned),
5047
sym::to_path_buf => is_diag_item_method(cx, method_def_id, sym::Path),
48+
sym::to_string => is_diag_trait_item(cx, method_def_id, sym::ToString),
5149
sym::to_vec => cx
5250
.tcx
5351
.impl_of_method(method_def_id)

clippy_lints/src/methods/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5403,7 +5403,7 @@ impl Methods {
54035403
implicit_clone::check(cx, name, expr, recv);
54045404
}
54055405
},
5406-
(sym::to_os_string | sym::to_path_buf | sym::to_vec, []) => {
5406+
(sym::to_os_string | sym::to_path_buf | sym::to_string | sym::to_vec, []) => {
54075407
implicit_clone::check(cx, name, expr, recv);
54085408
},
54095409
(sym::type_id, []) => {

clippy_lints/src/methods/unnecessary_to_owned.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,8 +619,8 @@ fn is_cloned_or_copied(cx: &LateContext<'_>, method_name: Symbol, method_def_id:
619619
/// Returns true if the named method can be used to convert the receiver to its "owned"
620620
/// representation.
621621
fn is_to_owned_like<'a>(cx: &LateContext<'a>, call_expr: &Expr<'a>, method_name: Symbol, method_def_id: DefId) -> bool {
622-
is_clone_like(cx, method_name, method_def_id)
623-
|| is_cow_into_owned(cx, method_name, method_def_id)
622+
is_cow_into_owned(cx, method_name, method_def_id)
623+
|| (method_name != sym::to_string && is_clone_like(cx, method_name, method_def_id))
624624
|| is_to_string_on_string_like(cx, call_expr, method_name, method_def_id)
625625
}
626626

tests/ui/implicit_clone.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,10 @@ fn main() {
135135
}
136136
let no_clone = &NoClone;
137137
let _ = no_clone.to_owned();
138+
139+
let s = String::from("foo");
140+
let _ = s.clone();
141+
//~^ implicit_clone
142+
let _ = s.clone();
143+
//~^ implicit_clone
138144
}

tests/ui/implicit_clone.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,10 @@ fn main() {
135135
}
136136
let no_clone = &NoClone;
137137
let _ = no_clone.to_owned();
138+
139+
let s = String::from("foo");
140+
let _ = s.to_owned();
141+
//~^ implicit_clone
142+
let _ = s.to_string();
143+
//~^ implicit_clone
138144
}

tests/ui/implicit_clone.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,17 @@ error: implicitly cloning a `PathBuf` by calling `to_path_buf` on its dereferenc
6767
LL | let _ = pathbuf_ref.to_path_buf();
6868
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(**pathbuf_ref).clone()`
6969

70-
error: aborting due to 11 previous errors
70+
error: implicitly cloning a `String` by calling `to_owned` on its dereferenced type
71+
--> tests/ui/implicit_clone.rs:140:13
72+
|
73+
LL | let _ = s.to_owned();
74+
| ^^^^^^^^^^^^ help: consider using: `s.clone()`
75+
76+
error: implicitly cloning a `String` by calling `to_string` on its dereferenced type
77+
--> tests/ui/implicit_clone.rs:142:13
78+
|
79+
LL | let _ = s.to_string();
80+
| ^^^^^^^^^^^^^ help: consider using: `s.clone()`
81+
82+
error: aborting due to 13 previous errors
7183

tests/ui/string_to_string.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![warn(clippy::implicit_clone)]
2+
#![allow(clippy::redundant_clone)]
3+
4+
fn main() {
5+
let mut message = String::from("Hello");
6+
let mut v = message.clone();
7+
//~^ ERROR: implicitly cloning a `String` by calling `to_string` on its dereferenced type
8+
}

tests/ui/string_to_string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#![warn(clippy::string_to_string)]
1+
#![warn(clippy::implicit_clone, clippy::string_to_string)]
22
#![allow(clippy::redundant_clone, clippy::unnecessary_literal_unwrap)]
33

44
fn main() {
55
let mut message = String::from("Hello");
66
let mut v = message.to_string();
7-
//~^ string_to_string
7+
//~^ ERROR: implicitly cloning a `String` by calling `to_string` on its dereferenced type
88

99
let variable1 = String::new();
1010
let v = &variable1;

tests/ui/string_to_string.stderr

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
error: `to_string()` called on a `String`
1+
error: implicitly cloning a `String` by calling `to_string` on its dereferenced type
22
--> tests/ui/string_to_string.rs:6:17
33
|
44
LL | let mut v = message.to_string();
5-
| ^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `message.clone()`
66
|
7-
= help: consider using `.clone()`
8-
= note: `-D clippy::string-to-string` implied by `-D warnings`
9-
= help: to override `-D warnings` add `#[allow(clippy::string_to_string)]`
7+
= note: `-D clippy::implicit-clone` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::implicit_clone)]`
109

1110
error: `to_string()` called on a `String`
1211
--> tests/ui/string_to_string.rs:14:9

0 commit comments

Comments
 (0)