Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useless use of format! should return function directly #7092

Merged
merged 1 commit into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions clippy_lints/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::paths;
use clippy_utils::source::{snippet, snippet_opt};
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{is_expn_of, last_path_segment, match_def_path, match_function_call};
use if_chain::if_chain;
Expand Down Expand Up @@ -100,15 +101,15 @@ fn on_argumentv1_new<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, arms: &
return Some(format!("{:?}.to_string()", s.as_str()));
}
} else {
let snip = snippet(cx, format_args.span, "<arg>");
let sugg = Sugg::hir(cx, format_args, "<arg>");
if let ExprKind::MethodCall(path, _, _, _) = format_args.kind {
if path.ident.name == sym!(to_string) {
return Some(format!("{}", snip));
return Some(format!("{}", sugg));
}
} else if let ExprKind::Binary(..) = format_args.kind {
return Some(format!("{}", snip));
return Some(format!("{}", sugg));
}
return Some(format!("{}.to_string()", snip));
return Some(format!("{}.to_string()", sugg.maybe_par()));
}
}
}
Expand Down Expand Up @@ -136,7 +137,7 @@ fn on_new_v1<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<Strin
if let Some(s_src) = snippet_opt(cx, lit.span) {
// Simulate macro expansion, converting {{ and }} to { and }.
let s_expand = s_src.replace("{{", "{").replace("}}", "}");
return Some(format!("{}.to_string()", s_expand))
return Some(format!("{}.to_string()", s_expand));
}
} else if s.as_str().is_empty() {
return on_argumentv1_new(cx, &tup[0], arms);
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/format.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@ fn main() {
// False positive
let a = "foo".to_string();
let _ = Some(a + "bar");

// Wrap it with braces
let v: Vec<String> = vec!["foo".to_string(), "bar".to_string()];
let _s: String = (&*v.join("\n")).to_string();
}
4 changes: 4 additions & 0 deletions tests/ui/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@ fn main() {
// False positive
let a = "foo".to_string();
let _ = Some(format!("{}", a + "bar"));

// Wrap it with braces
let v: Vec<String> = vec!["foo".to_string(), "bar".to_string()];
let _s: String = format!("{}", &*v.join("\n"));
}
8 changes: 7 additions & 1 deletion tests/ui/format.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,11 @@ error: useless use of `format!`
LL | let _ = Some(format!("{}", a + "bar"));
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `a + "bar"`

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

error: aborting due to 14 previous errors