Skip to content

Commit c6b381c

Browse files
committed
useless use of format! should return function directly
1 parent 52c8c9c commit c6b381c

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

clippy_lints/src/format.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::paths;
33
use clippy_utils::source::{snippet, snippet_opt};
4+
use clippy_utils::sugg::Sugg;
45
use clippy_utils::ty::is_type_diagnostic_item;
56
use clippy_utils::{is_expn_of, last_path_segment, match_def_path, match_function_call};
67
use if_chain::if_chain;
@@ -100,15 +101,15 @@ fn on_argumentv1_new<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, arms: &
100101
return Some(format!("{:?}.to_string()", s.as_str()));
101102
}
102103
} else {
103-
let snip = snippet(cx, format_args.span, "<arg>");
104+
let sugg = Sugg::hir(cx, format_args, "<arg>");
104105
if let ExprKind::MethodCall(path, _, _, _) = format_args.kind {
105106
if path.ident.name == sym!(to_string) {
106-
return Some(format!("{}", snip));
107+
return Some(format!("{}", sugg));
107108
}
108109
} else if let ExprKind::Binary(..) = format_args.kind {
109-
return Some(format!("{}", snip));
110+
return Some(format!("{}", sugg));
110111
}
111-
return Some(format!("{}.to_string()", snip));
112+
return Some(format!("{}.to_string()", sugg.maybe_par()));
112113
}
113114
}
114115
}
@@ -136,7 +137,7 @@ fn on_new_v1<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<Strin
136137
if let Some(s_src) = snippet_opt(cx, lit.span) {
137138
// Simulate macro expansion, converting {{ and }} to { and }.
138139
let s_expand = s_src.replace("{{", "{").replace("}}", "}");
139-
return Some(format!("{}.to_string()", s_expand))
140+
return Some(format!("{}.to_string()", s_expand));
140141
}
141142
} else if s.as_str().is_empty() {
142143
return on_argumentv1_new(cx, &tup[0], arms);

tests/ui/format.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,8 @@ fn main() {
6565
// False positive
6666
let a = "foo".to_string();
6767
let _ = Some(a + "bar");
68+
69+
// Wrap it with braces
70+
let v: Vec<String> = vec!["foo".to_string(), "bar".to_string()];
71+
let _s: String = (&*v.join("\n")).to_string();
6872
}

tests/ui/format.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,8 @@ fn main() {
6767
// False positive
6868
let a = "foo".to_string();
6969
let _ = Some(format!("{}", a + "bar"));
70+
71+
// Wrap it with braces
72+
let v: Vec<String> = vec!["foo".to_string(), "bar".to_string()];
73+
let _s: String = format!("{}", &*v.join("\n"));
7074
}

tests/ui/format.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,11 @@ error: useless use of `format!`
8787
LL | let _ = Some(format!("{}", a + "bar"));
8888
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `a + "bar"`
8989

90-
error: aborting due to 13 previous errors
90+
error: useless use of `format!`
91+
--> $DIR/format.rs:73:22
92+
|
93+
LL | let _s: String = format!("{}", &*v.join("/n"));
94+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `(&*v.join("/n")).to_string()`
95+
96+
error: aborting due to 14 previous errors
9197

0 commit comments

Comments
 (0)