From cae9cedeb5acc9b4edc23b376e15fba339254c31 Mon Sep 17 00:00:00 2001 From: mcarton Date: Fri, 29 Sep 2017 18:36:03 +0200 Subject: [PATCH] Fix regression with `format!` --- clippy_lints/src/format.rs | 27 +++++++++++++++------------ tests/ui/format.stderr | 14 +++++++++++++- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/clippy_lints/src/format.rs b/clippy_lints/src/format.rs index f1a450e58df39..26b500d554635 100644 --- a/clippy_lints/src/format.rs +++ b/clippy_lints/src/format.rs @@ -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 diff --git a/tests/ui/format.stderr b/tests/ui/format.stderr index 5f5bdc02a5920..d2c9f39383127 100644 --- a/tests/ui/format.stderr +++ b/tests/ui/format.stderr @@ -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