Fix regression with format!

This commit is contained in:
mcarton 2017-09-29 18:36:03 +02:00
parent bc76f397c6
commit cae9cedeb5
2 changed files with 28 additions and 13 deletions

View file

@ -50,8 +50,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
let Some(fun_def_id) = opt_def_id(resolve_node(cx, qpath, fun.hir_id)),
match_def_path(cx.tcx, fun_def_id, &paths::FMT_ARGUMENTS_NEWV1),
// ensure the format string is `"{..}"` with only one argument and no text
check_static_str(cx, &args[0]),
check_static_str(&args[0]),
// ensure the format argument is `{}` ie. Display with no fancy option
// and that the argument is a string
check_arg_is_display(cx, &args[1])
], {
span_lint(cx, USELESS_FORMAT, span, "useless use of `format!`");
@ -96,17 +97,19 @@ pub fn get_argument_fmtstr_parts<'a, 'b>(cx: &LateContext<'a, 'b>, expr: &'a Exp
None
}
/// Checks if the expressions matches
/// ```rust, ignore
/// { static __STATIC_FMTSTR: &'static[&'static str] = &["a", "b", c];
/// __STATIC_FMTSTR }
/// ```
fn check_static_str(cx: &LateContext, expr: &Expr) -> bool {
if let Some(expr) = get_argument_fmtstr_parts(cx, expr) {
expr.len() == 1 && expr[0].is_empty()
} else {
false
}
/// Checks if the expressions matches `&[""]`
fn check_static_str(expr: &Expr) -> bool {
if_let_chain! {[
let ExprAddrOf(_, ref expr) = expr.node, // &[""]
let ExprArray(ref exprs) = expr.node, // [""]
exprs.len() == 1,
let ExprLit(ref lit) = exprs[0].node,
let LitKind::Str(ref lit, _) = lit.node,
], {
return lit.as_str().is_empty();
}}
false
}
/// Checks if the expressions matches

View file

@ -6,5 +6,17 @@ error: useless use of `format!`
|
= note: `-D useless-format` implied by `-D warnings`
error: aborting due to previous error
error: useless use of `format!`
--> $DIR/format.rs:8:5
|
8 | format!("{}", "foo");
| ^^^^^^^^^^^^^^^^^^^^^
error: useless use of `format!`
--> $DIR/format.rs:15:5
|
15 | format!("{}", arg);
| ^^^^^^^^^^^^^^^^^^^
error: aborting due to 3 previous errors