Add suggestion to useless_format

Resolves #2505

Suggests that you use `"foo".to_string()` instead of `format!("foo")`.
This commit is contained in:
Baelyk 2018-03-17 00:58:56 -05:00
parent 6f3f25878f
commit 48027105dc
2 changed files with 10 additions and 4 deletions

View File

@ -3,7 +3,7 @@ use rustc::lint::*;
use rustc::ty;
use syntax::ast::LitKind;
use utils::paths;
use utils::{is_expn_of, match_def_path, match_type, opt_def_id, resolve_node, span_lint, walk_ptrs_ty};
use utils::{is_expn_of, match_def_path, match_type, opt_def_id, resolve_node, snippet, span_lint_and_then, walk_ptrs_ty};
/// **What it does:** Checks for the use of `format!("string literal with no
/// argument")` and `format!("{}", foo)` where `foo` is a string.
@ -53,14 +53,20 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
// and that the argument is a string
if check_arg_is_display(cx, &args[1]);
then {
span_lint(cx, USELESS_FORMAT, span, "useless use of `format!`");
let sugg = format!("{}.to_string()", snippet(cx, expr.span, "<expr>").into_owned());
span_lint_and_then(cx, USELESS_FORMAT, span, "useless use of `format!`", |db| {
db.span_suggestion(expr.span, "consider using .to_string()", sugg);
});
}
}
},
// `format!("foo")` expansion contains `match () { () => [], }`
ExprMatch(ref matchee, _, _) => if let ExprTup(ref tup) = matchee.node {
if tup.is_empty() {
span_lint(cx, USELESS_FORMAT, span, "useless use of `format!`");
let sugg = format!("{}.to_string()", snippet(cx, expr.span, "<expr>").into_owned());
span_lint_and_then(cx, USELESS_FORMAT, span, "useless use of `format!`", |db| {
db.span_suggestion(span, "consider using .to_string()", sugg);
});
}
},
_ => (),

View File

@ -2,7 +2,7 @@ error: useless use of `format!`
--> $DIR/format.rs:6:5
|
6 | format!("foo");
| ^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string()`
|
= note: `-D useless-format` implied by `-D warnings`