@@ -57,12 +57,24 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
57
57
if check_single_piece( & args[ 0 ] ) ;
58
58
if let Some ( format_arg) = get_single_string_arg( cx, & args[ 1 ] ) ;
59
59
if check_unformatted( & args[ 2 ] ) ;
60
+ if let ExprKind :: AddrOf ( _, ref format_arg) = format_arg. node;
60
61
then {
61
- let sugg = format!( "{}.to_string()" , snippet( cx, format_arg, "<arg>" ) . into_owned( ) ) ;
62
+ let ( message, sugg) = if_chain! {
63
+ if let ExprKind :: MethodCall ( ref path, ref span, ref expr) = format_arg. node;
64
+ if path. ident. as_interned_str( ) == "to_string" ;
65
+ then {
66
+ ( "`to_string()` is enough" ,
67
+ snippet( cx, format_arg. span, "<arg>" ) . to_string( ) )
68
+ } else {
69
+ ( "consider using .to_string()" ,
70
+ format!( "{}.to_string()" , snippet( cx, format_arg. span, "<arg>" ) ) )
71
+ }
72
+ } ;
73
+
62
74
span_lint_and_then( cx, USELESS_FORMAT , span, "useless use of `format!`" , |db| {
63
75
db. span_suggestion_with_applicability(
64
76
expr. span,
65
- "consider using .to_string()" ,
77
+ message ,
66
78
sugg,
67
79
Applicability :: MachineApplicable ,
68
80
) ;
@@ -113,9 +125,9 @@ fn check_single_piece(expr: &Expr) -> bool {
113
125
/// ::std::fmt::Display::fmt)],
114
126
/// }
115
127
/// ```
116
- /// and that type of `__arg0` is `&str` or `String`
117
- /// then returns the span of first element of the matched tuple
118
- fn get_single_string_arg ( cx : & LateContext < ' _ , ' _ > , expr : & Expr ) -> Option < Span > {
128
+ /// and that the type of `__arg0` is `&str` or `String`,
129
+ /// then returns the span of first element of the matched tuple.
130
+ fn get_single_string_arg < ' a > ( cx : & LateContext < ' _ , ' _ > , expr : & ' a Expr ) -> Option < & ' a Expr > {
119
131
if_chain ! {
120
132
if let ExprKind :: AddrOf ( _, ref expr) = expr. node;
121
133
if let ExprKind :: Match ( ref match_expr, ref arms, _) = expr. node;
@@ -134,7 +146,7 @@ fn get_single_string_arg(cx: &LateContext<'_, '_>, expr: &Expr) -> Option<Span>
134
146
let ty = walk_ptrs_ty( cx. tables. pat_ty( & pat[ 0 ] ) ) ;
135
147
if ty. sty == ty:: Str || match_type( cx, ty, & paths:: STRING ) {
136
148
if let ExprKind :: Tup ( ref values) = match_expr. node {
137
- return Some ( values[ 0 ] . span ) ;
149
+ return Some ( & values[ 0 ] ) ;
138
150
}
139
151
}
140
152
}
0 commit comments