File tree Expand file tree Collapse file tree 4 files changed +23
-26
lines changed Expand file tree Collapse file tree 4 files changed +23
-26
lines changed Original file line number Diff line number Diff line change @@ -494,21 +494,9 @@ impl<'tcx> LateLintPass<'tcx> for StringToString {
494494 if path. ident . name == sym:: to_string
495495 && let ty = cx. typeck_results ( ) . expr_ty ( self_arg)
496496 && is_type_lang_item ( cx, ty. peel_refs ( ) , LangItem :: String )
497+ && let Some ( parent_span) = is_called_from_map_like ( cx, expr)
497498 {
498- if let Some ( parent_span) = is_called_from_map_like ( cx, expr) {
499- suggest_cloned_string_to_string ( cx, parent_span) ;
500- } else {
501- #[ expect( clippy:: collapsible_span_lint_calls, reason = "rust-clippy#7797" ) ]
502- span_lint_and_then (
503- cx,
504- STRING_TO_STRING ,
505- expr. span ,
506- "`to_string()` called on a `String`" ,
507- |diag| {
508- diag. help ( "consider using `.clone()`" ) ;
509- } ,
510- ) ;
511- }
499+ suggest_cloned_string_to_string ( cx, parent_span) ;
512500 }
513501 } ,
514502 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 )]
33
44fn 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}
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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 |
1313LL | 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 |
2119LL | let _ = x.unwrap_or_else(|| v.to_string());
22- | ^^^^^^^^^^^^^
23- |
24- = help: consider using `.clone()`
20+ | ^^^^^^^^^^^^^ help: consider using: `v.clone()`
2521
2622error: aborting due to 3 previous errors
2723
You can’t perform that action at this time.
0 commit comments