2 use rustc::hir::map::Node::NodeItem;
4 use rustc::ty::TypeVariants;
5 use syntax::ast::LitKind;
7 use utils::{is_expn_of, match_path, match_type, span_lint, walk_ptrs_ty};
9 /// **What it does:** Checks for the use of `format!("string literal with no
10 /// argument")` and `format!("{}", foo)` where `foo` is a string.
12 /// **Why is this bad?** There is no point of doing that. `format!("too")` can
13 /// be replaced by `"foo".to_owned()` if you really need a `String`. The even
14 /// worse `&format!("foo")` is often encountered in the wild. `format!("{}",
15 /// foo)` can be replaced by `foo.clone()` if `foo: String` or `foo.to_owned()`
18 /// **Known problems:** None.
23 /// format!("{}", foo)
28 "useless use of `format!`"
31 #[derive(Copy, Clone, Debug)]
34 impl LintPass for Pass {
35 fn get_lints(&self) -> LintArray {
36 lint_array![USELESS_FORMAT]
40 impl LateLintPass for Pass {
41 fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
42 if let Some(span) = is_expn_of(cx, expr.span, "format") {
44 // `format!("{}", foo)` expansion
45 ExprCall(ref fun, ref args) => {
47 let ExprPath(_, ref path) = fun.node,
49 match_path(path, &paths::FMT_ARGUMENTS_NEWV1),
50 // ensure the format string is `"{..}"` with only one argument and no text
51 check_static_str(cx, &args[0]),
52 // ensure the format argument is `{}` ie. Display with no fancy option
53 check_arg_is_display(cx, &args[1])
55 span_lint(cx, USELESS_FORMAT, span, "useless use of `format!`");
58 // `format!("foo")` expansion contains `match () { () => [], }`
59 ExprMatch(ref matchee, _, _) => {
60 if let ExprTup(ref tup) = matchee.node {
62 span_lint(cx, USELESS_FORMAT, span, "useless use of `format!`");
72 /// Checks if the expressions matches
74 /// { static __STATIC_FMTSTR: &[""] = _; __STATIC_FMTSTR }
76 fn check_static_str(cx: &LateContext, expr: &Expr) -> bool {
78 let ExprBlock(ref block) = expr.node,
79 block.stmts.len() == 1,
80 let StmtDecl(ref decl, _) = block.stmts[0].node,
81 let DeclItem(ref decl) = decl.node,
82 let Some(NodeItem(decl)) = cx.tcx.map.find(decl.id),
83 decl.name.as_str() == "__STATIC_FMTSTR",
84 let ItemStatic(_, _, ref expr) = decl.node,
85 let ExprAddrOf(_, ref expr) = expr.node, // &[""]
86 let ExprVec(ref expr) = expr.node,
88 let ExprLit(ref lit) = expr[0].node,
89 let LitKind::Str(ref lit, _) = lit.node,
98 /// Checks if the expressions matches
101 /// (__arg0,) => [::std::fmt::ArgumentV1::new(__arg0, ::std::fmt::Display::fmt)],
104 fn check_arg_is_display(cx: &LateContext, expr: &Expr) -> bool {
106 let ExprAddrOf(_, ref expr) = expr.node,
107 let ExprMatch(_, ref arms, _) = expr.node,
109 arms[0].pats.len() == 1,
110 let PatKind::Tuple(ref pat, None) = arms[0].pats[0].node,
112 let ExprVec(ref exprs) = arms[0].body.node,
114 let ExprCall(_, ref args) = exprs[0].node,
116 let ExprPath(None, ref path) = args[1].node,
117 match_path(path, &paths::DISPLAY_FMT_METHOD)
119 let ty = walk_ptrs_ty(cx.tcx.pat_ty(&pat[0]));
121 return ty.sty == TypeVariants::TyStr || match_type(cx, ty, &paths::STRING);