@@ -13,8 +13,6 @@ use rustc_middle::ty;
13
13
use rustc_session:: declare_lint_pass;
14
14
use rustc_span:: source_map:: Spanned ;
15
15
16
- use std:: ops:: ControlFlow ;
17
-
18
16
declare_clippy_lint ! {
19
17
/// ### What it does
20
18
/// Checks for string appends of the form `x = x + y` (without
@@ -411,113 +409,6 @@ impl<'tcx> LateLintPass<'tcx> for StrToString {
411
409
}
412
410
}
413
411
414
- declare_clippy_lint ! {
415
- /// ### What it does
416
- /// This lint checks for `.to_string()` method calls on values of type `String`.
417
- ///
418
- /// ### Why restrict this?
419
- /// The `to_string` method is also used on other types to convert them to a string.
420
- /// When called on a `String` it only clones the `String`, which can be more specifically
421
- /// expressed with `.clone()`.
422
- ///
423
- /// ### Example
424
- /// ```no_run
425
- /// let msg = String::from("Hello World");
426
- /// let _ = msg.to_string();
427
- /// ```
428
- /// Use instead:
429
- /// ```no_run
430
- /// let msg = String::from("Hello World");
431
- /// let _ = msg.clone();
432
- /// ```
433
- #[ clippy:: version = "pre 1.29.0" ]
434
- pub STRING_TO_STRING ,
435
- restriction,
436
- "using `to_string()` on a `String`, which should be `clone()`"
437
- }
438
-
439
- declare_lint_pass ! ( StringToString => [ STRING_TO_STRING ] ) ;
440
-
441
- fn is_parent_map_like ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> Option < rustc_span:: Span > {
442
- if let Some ( parent_expr) = get_parent_expr ( cx, expr)
443
- && let ExprKind :: MethodCall ( name, _, _, parent_span) = parent_expr. kind
444
- && name. ident . name == sym:: map
445
- && let Some ( caller_def_id) = cx. typeck_results ( ) . type_dependent_def_id ( parent_expr. hir_id )
446
- && ( clippy_utils:: is_diag_item_method ( cx, caller_def_id, sym:: Result )
447
- || clippy_utils:: is_diag_item_method ( cx, caller_def_id, sym:: Option )
448
- || clippy_utils:: is_diag_trait_item ( cx, caller_def_id, sym:: Iterator ) )
449
- {
450
- Some ( parent_span)
451
- } else {
452
- None
453
- }
454
- }
455
-
456
- fn is_called_from_map_like ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> Option < rustc_span:: Span > {
457
- // Look for a closure as parent of `expr`, discarding simple blocks
458
- let parent_closure = cx
459
- . tcx
460
- . hir_parent_iter ( expr. hir_id )
461
- . try_fold ( expr. hir_id , |child_hir_id, ( _, node) | match node {
462
- // Check that the child expression is the only expression in the block
463
- Node :: Block ( block) if block. stmts . is_empty ( ) && block. expr . map ( |e| e. hir_id ) == Some ( child_hir_id) => {
464
- ControlFlow :: Continue ( block. hir_id )
465
- } ,
466
- Node :: Expr ( expr) if matches ! ( expr. kind, ExprKind :: Block ( ..) ) => ControlFlow :: Continue ( expr. hir_id ) ,
467
- Node :: Expr ( expr) if matches ! ( expr. kind, ExprKind :: Closure ( _) ) => ControlFlow :: Break ( Some ( expr) ) ,
468
- _ => ControlFlow :: Break ( None ) ,
469
- } )
470
- . break_value ( ) ?;
471
- is_parent_map_like ( cx, parent_closure?)
472
- }
473
-
474
- fn suggest_cloned_string_to_string ( cx : & LateContext < ' _ > , span : rustc_span:: Span ) {
475
- span_lint_and_sugg (
476
- cx,
477
- STRING_TO_STRING ,
478
- span,
479
- "`to_string()` called on a `String`" ,
480
- "try" ,
481
- "cloned()" . to_string ( ) ,
482
- Applicability :: MachineApplicable ,
483
- ) ;
484
- }
485
-
486
- impl < ' tcx > LateLintPass < ' tcx > for StringToString {
487
- fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & Expr < ' _ > ) {
488
- if expr. span . from_expansion ( ) {
489
- return ;
490
- }
491
-
492
- match & expr. kind {
493
- ExprKind :: MethodCall ( path, self_arg, [ ] , _) => {
494
- if path. ident . name == sym:: to_string
495
- && let ty = cx. typeck_results ( ) . expr_ty ( self_arg)
496
- && is_type_lang_item ( cx, ty. peel_refs ( ) , LangItem :: String )
497
- && let Some ( parent_span) = is_called_from_map_like ( cx, expr)
498
- {
499
- suggest_cloned_string_to_string ( cx, parent_span) ;
500
- }
501
- } ,
502
- ExprKind :: Path ( QPath :: TypeRelative ( ty, segment) ) => {
503
- if segment. ident . name == sym:: to_string
504
- && let rustc_hir:: TyKind :: Path ( QPath :: Resolved ( _, path) ) = ty. peel_refs ( ) . kind
505
- && let rustc_hir:: def:: Res :: Def ( _, def_id) = path. res
506
- && cx
507
- . tcx
508
- . lang_items ( )
509
- . get ( LangItem :: String )
510
- . is_some_and ( |lang_id| lang_id == def_id)
511
- && let Some ( parent_span) = is_parent_map_like ( cx, expr)
512
- {
513
- suggest_cloned_string_to_string ( cx, parent_span) ;
514
- }
515
- } ,
516
- _ => { } ,
517
- }
518
- }
519
- }
520
-
521
412
declare_clippy_lint ! {
522
413
/// ### What it does
523
414
/// Warns about calling `str::trim` (or variants) before `str::split_whitespace`.
0 commit comments