Skip to content

Commit 5173ed0

Browse files
committed
Don't suggest to_string().to_string in USELESS_FORMAT
1 parent 7eebd5b commit 5173ed0

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

clippy_lints/src/format.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,24 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
5757
if check_single_piece(&args[0]);
5858
if let Some(format_arg) = get_single_string_arg(cx, &args[1]);
5959
if check_unformatted(&args[2]);
60+
if let ExprKind::AddrOf(_, ref format_arg) = format_arg.node;
6061
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+
6274
span_lint_and_then(cx, USELESS_FORMAT, span, "useless use of `format!`", |db| {
6375
db.span_suggestion_with_applicability(
6476
expr.span,
65-
"consider using .to_string()",
77+
message,
6678
sugg,
6779
Applicability::MachineApplicable,
6880
);
@@ -113,9 +125,9 @@ fn check_single_piece(expr: &Expr) -> bool {
113125
/// ::std::fmt::Display::fmt)],
114126
/// }
115127
/// ```
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> {
119131
if_chain! {
120132
if let ExprKind::AddrOf(_, ref expr) = expr.node;
121133
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>
134146
let ty = walk_ptrs_ty(cx.tables.pat_ty(&pat[0]));
135147
if ty.sty == ty::Str || match_type(cx, ty, &paths::STRING) {
136148
if let ExprKind::Tup(ref values) = match_expr.node {
137-
return Some(values[0].span);
149+
return Some(&values[0]);
138150
}
139151
}
140152
}

tests/ui/format.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,8 @@ fn main() {
5252
format!("{:.10}", "foo"); // could not be "foo"[..10]
5353
format!("{:.prec$}", "foo", prec = 1);
5454
format!("{:.prec$}", "foo", prec = 10);
55+
56+
format!("{}", 42.to_string());
57+
let x = std::path::PathBuf::from("/bar/foo/qux");
58+
format!("{}", x.display().to_string());
5559
}

tests/ui/format.stderr

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,21 @@ error: useless use of `format!`
5454
|
5555
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
5656

57-
error: aborting due to 7 previous errors
57+
error: useless use of `format!`
58+
--> $DIR/format.rs:56:5
59+
|
60+
56 | format!("{}", 42.to_string());
61+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: `to_string()` is enough: `42.to_string()`
62+
|
63+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
64+
65+
error: useless use of `format!`
66+
--> $DIR/format.rs:58:5
67+
|
68+
58 | format!("{}", x.display().to_string());
69+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: `to_string()` is enough: `x.display().to_string()`
70+
|
71+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
72+
73+
error: aborting due to 9 previous errors
5874

0 commit comments

Comments
 (0)