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:** This lints about use of `format!("string literal with no argument")` and
10 /// `format!("{}", foo)` where `foo` is a string.
12 /// **Why is this bad?** There is no point of doing that. `format!("too")` can be replaced by
13 /// `"foo".to_owned()` if you really need a `String`. The even worse `&format!("foo")` is often
14 /// encountered in the wild. `format!("{}", foo)` can be replaced by `foo.clone()` if `foo: String`
15 /// or `foo.to_owned()` is `foo: &str`.
17 /// **Known problems:** None.
19 /// **Examples:** `format!("foo")` and `format!("{}", foo)`
23 "useless use of `format!`"
26 #[derive(Copy, Clone, Debug)]
29 impl LintPass for Pass {
30 fn get_lints(&self) -> LintArray {
31 lint_array![USELESS_FORMAT]
35 impl LateLintPass for Pass {
36 fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
37 if let Some(span) = is_expn_of(cx, expr.span, "format") {
39 // `format!("{}", foo)` expansion
40 ExprCall(ref fun, ref args) => {
42 let ExprPath(_, ref path) = fun.node,
44 match_path(path, &paths::FMT_ARGUMENTS_NEWV1),
45 // ensure the format string is `"{..}"` with only one argument and no text
46 check_static_str(cx, &args[0]),
47 // ensure the format argument is `{}` ie. Display with no fancy option
48 check_arg_is_display(cx, &args[1])
50 span_lint(cx, USELESS_FORMAT, span, "useless use of `format!`");
53 // `format!("foo")` expansion contains `match () { () => [], }`
54 ExprMatch(ref matchee, _, _) => {
55 if let ExprTup(ref tup) = matchee.node {
57 span_lint(cx, USELESS_FORMAT, span, "useless use of `format!`");
67 /// Checks if the expressions matches
69 /// { static __STATIC_FMTSTR: &[""] = _; __STATIC_FMTSTR }
71 fn check_static_str(cx: &LateContext, expr: &Expr) -> bool {
73 let ExprBlock(ref block) = expr.node,
74 block.stmts.len() == 1,
75 let StmtDecl(ref decl, _) = block.stmts[0].node,
76 let DeclItem(ref decl) = decl.node,
77 let Some(NodeItem(decl)) = cx.tcx.map.find(decl.id),
78 decl.name.as_str() == "__STATIC_FMTSTR",
79 let ItemStatic(_, _, ref expr) = decl.node,
80 let ExprAddrOf(_, ref expr) = expr.node, // &[""]
81 let ExprVec(ref expr) = expr.node,
83 let ExprLit(ref lit) = expr[0].node,
84 let LitKind::Str(ref lit, _) = lit.node,
93 /// Checks if the expressions matches
96 /// (__arg0,) => [::std::fmt::ArgumentV1::new(__arg0, ::std::fmt::Display::fmt)],
99 fn check_arg_is_display(cx: &LateContext, expr: &Expr) -> bool {
101 let ExprAddrOf(_, ref expr) = expr.node,
102 let ExprMatch(_, ref arms, _) = expr.node,
104 arms[0].pats.len() == 1,
105 let PatKind::Tuple(ref pat, None) = arms[0].pats[0].node,
107 let ExprVec(ref exprs) = arms[0].body.node,
109 let ExprCall(_, ref args) = exprs[0].node,
111 let ExprPath(None, ref path) = args[1].node,
112 match_path(path, &paths::DISPLAY_FMT_METHOD)
114 let ty = walk_ptrs_ty(cx.tcx.pat_ty(&pat[0]));
116 return ty.sty == TypeVariants::TyStr || match_type(cx, ty, &paths::STRING);