Skip to content

Commit 0b85f4d

Browse files
committed
Account for changes from #14396
1 parent be99564 commit 0b85f4d

File tree

4 files changed

+23
-26
lines changed

4 files changed

+23
-26
lines changed

clippy_lints/src/strings.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -496,21 +496,9 @@ impl<'tcx> LateLintPass<'tcx> for StringToString {
496496
if path.ident.name == sym::to_string
497497
&& let ty = cx.typeck_results().expr_ty(self_arg)
498498
&& is_type_lang_item(cx, ty.peel_refs(), LangItem::String)
499+
&& let Some(parent_span) = is_called_from_map_like(cx, expr)
499500
{
500-
if let Some(parent_span) = is_called_from_map_like(cx, expr) {
501-
suggest_cloned_string_to_string(cx, parent_span);
502-
} else {
503-
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
504-
span_lint_and_then(
505-
cx,
506-
STRING_TO_STRING,
507-
expr.span,
508-
"`to_string()` called on a `String`",
509-
|diag| {
510-
diag.help("consider using `.clone()`");
511-
},
512-
);
513-
}
501+
suggest_cloned_string_to_string(cx, parent_span);
514502
}
515503
},
516504
ExprKind::Path(QPath::TypeRelative(ty, segment)) => {

tests/ui/string_to_string.fixed

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

44
fn main() {
55
let mut message = String::from("Hello");
66
let mut v = message.clone();
77
//~^ ERROR: implicitly cloning a `String` by calling `to_string` on its dereferenced type
8+
9+
let variable1 = String::new();
10+
let v = &variable1;
11+
let variable2 = Some(v);
12+
let _ = variable2.map(|x| {
13+
println!();
14+
x.clone()
15+
//~^ ERROR: implicitly cloning a `String` by calling `to_string` on its dereferenced type
16+
});
17+
18+
let x = Some(String::new());
19+
let _ = x.unwrap_or_else(|| v.clone());
20+
//~^ ERROR: implicitly cloning a `String` by calling `to_string` on its dereferenced type
821
}

tests/ui/string_to_string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ fn main() {
1212
let _ = variable2.map(|x| {
1313
println!();
1414
x.to_string()
15+
//~^ ERROR: implicitly cloning a `String` by calling `to_string` on its dereferenced type
1516
});
16-
//~^^ string_to_string
1717

1818
let x = Some(String::new());
1919
let _ = x.unwrap_or_else(|| v.to_string());
20-
//~^ string_to_string
20+
//~^ ERROR: implicitly cloning a `String` by calling `to_string` on its dereferenced type
2121
}

tests/ui/string_to_string.stderr

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,17 @@ LL | let mut v = message.to_string();
77
= note: `-D clippy::implicit-clone` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::implicit_clone)]`
99

10-
error: `to_string()` called on a `String`
10+
error: implicitly cloning a `String` by calling `to_string` on its dereferenced type
1111
--> tests/ui/string_to_string.rs:14:9
1212
|
1313
LL | x.to_string()
14-
| ^^^^^^^^^^^^^
15-
|
16-
= help: consider using `.clone()`
14+
| ^^^^^^^^^^^^^ help: consider using: `x.clone()`
1715

18-
error: `to_string()` called on a `String`
16+
error: implicitly cloning a `String` by calling `to_string` on its dereferenced type
1917
--> tests/ui/string_to_string.rs:19:33
2018
|
2119
LL | let _ = x.unwrap_or_else(|| v.to_string());
22-
| ^^^^^^^^^^^^^
23-
|
24-
= help: consider using `.clone()`
20+
| ^^^^^^^^^^^^^ help: consider using: `v.clone()`
2521

2622
error: aborting due to 3 previous errors
2723

0 commit comments

Comments
 (0)