File tree 4 files changed +23
-26
lines changed
4 files changed +23
-26
lines changed Original file line number Diff line number Diff line change @@ -496,21 +496,9 @@ impl<'tcx> LateLintPass<'tcx> for StringToString {
496
496
if path. ident . name == sym:: to_string
497
497
&& let ty = cx. typeck_results ( ) . expr_ty ( self_arg)
498
498
&& is_type_lang_item ( cx, ty. peel_refs ( ) , LangItem :: String )
499
+ && let Some ( parent_span) = is_called_from_map_like ( cx, expr)
499
500
{
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) ;
514
502
}
515
503
} ,
516
504
ExprKind :: Path ( QPath :: TypeRelative ( ty, segment) ) => {
Original file line number Diff line number Diff line change 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 )]
3
3
4
4
fn main() {
5
5
let mut message = String::from("Hello");
6
6
let mut v = message.clone();
7
7
//~^ 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
8
21
}
Original file line number Diff line number Diff line change @@ -12,10 +12,10 @@ fn main() {
12
12
let _ = variable2. map ( |x| {
13
13
println ! ( ) ;
14
14
x. to_string ( )
15
+ //~^ ERROR: implicitly cloning a `String` by calling `to_string` on its dereferenced type
15
16
} ) ;
16
- //~^^ string_to_string
17
17
18
18
let x = Some ( String :: new ( ) ) ;
19
19
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
21
21
}
Original file line number Diff line number Diff line change @@ -7,21 +7,17 @@ LL | let mut v = message.to_string();
7
7
= note: `-D clippy::implicit-clone` implied by `-D warnings`
8
8
= help: to override `-D warnings` add `#[allow(clippy::implicit_clone)]`
9
9
10
- error: `to_string()` called on a `String`
10
+ error: implicitly cloning a `String` by calling `to_string` on its dereferenced type
11
11
--> tests/ui/string_to_string.rs:14:9
12
12
|
13
13
LL | x.to_string()
14
- | ^^^^^^^^^^^^^
15
- |
16
- = help: consider using `.clone()`
14
+ | ^^^^^^^^^^^^^ help: consider using: `x.clone()`
17
15
18
- error: `to_string()` called on a `String`
16
+ error: implicitly cloning a `String` by calling `to_string` on its dereferenced type
19
17
--> tests/ui/string_to_string.rs:19:33
20
18
|
21
19
LL | let _ = x.unwrap_or_else(|| v.to_string());
22
- | ^^^^^^^^^^^^^
23
- |
24
- = help: consider using `.clone()`
20
+ | ^^^^^^^^^^^^^ help: consider using: `v.clone()`
25
21
26
22
error: aborting due to 3 previous errors
27
23
You can’t perform that action at this time.
0 commit comments