Merge pull request #2539 from Baelyk/master

Add suggestion to useless_format
This commit is contained in:
Oliver Schneider 2018-03-20 08:22:04 +01:00 committed by GitHub
commit 2d01f42dde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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`