about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLzu Tao <taolzu@gmail.com>2019-08-23 15:46:23 +0700
committerflip1995 <hello@philkrones.com>2019-09-03 18:07:28 +0200
commit652840b4f16ee8f5b99349bcadb0931ed5b064b2 (patch)
tree3142c21f644e9b8c6c566348a13416083d63d789
parent413eb5b946955cd33ffd6fb58d7c59de978f92d6 (diff)
downloadrust-652840b4f16ee8f5b99349bcadb0931ed5b064b2.tar.gz
rust-652840b4f16ee8f5b99349bcadb0931ed5b064b2.zip
Re-add false positive check
-rw-r--r--clippy_lints/src/format.rs6
-rw-r--r--tests/ui/format.fixed4
-rw-r--r--tests/ui/format.rs4
-rw-r--r--tests/ui/format.stderr8
4 files changed, 21 insertions, 1 deletions
diff --git a/clippy_lints/src/format.rs b/clippy_lints/src/format.rs
index f28f98b1cab..a20217991ab 100644
--- a/clippy_lints/src/format.rs
+++ b/clippy_lints/src/format.rs
@@ -90,6 +90,10 @@ fn on_argumentv1_new<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, arm
         if let PatKind::Tuple(ref pats, None) = arms[0].pats[0].node;
         if pats.len() == 1;
         then {
+            let ty = walk_ptrs_ty(cx.tables.pat_ty(&pats[0]));
+            if ty.sty != rustc::ty::Str && !match_type(cx, ty, &paths::STRING) {
+                return None;
+            }
             if let ExprKind::Lit(ref lit) = format_args.node {
                 if let LitKind::Str(ref s, _) = lit.node {
                     return Some(format!("{:?}.to_string()", s.as_str()));
@@ -100,6 +104,8 @@ fn on_argumentv1_new<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, arm
                     if path.ident.name == sym!(to_string) {
                         return Some(format!("{}", snip));
                     }
+                } else if let ExprKind::Binary(..) = format_args.node {
+                    return Some(format!("{}", snip));
                 }
                 return Some(format!("{}.to_string()", snip));
             }
diff --git a/tests/ui/format.fixed b/tests/ui/format.fixed
index 401a1c533fe..6e100230a3a 100644
--- a/tests/ui/format.fixed
+++ b/tests/ui/format.fixed
@@ -60,4 +60,8 @@ fn main() {
     42.to_string();
     let x = std::path::PathBuf::from("/bar/foo/qux");
     x.display().to_string();
+
+    // False positive
+    let a = "foo".to_string();
+    let _ = Some(a + "bar");
 }
diff --git a/tests/ui/format.rs b/tests/ui/format.rs
index 1cbc15cfcec..1fae6603ac0 100644
--- a/tests/ui/format.rs
+++ b/tests/ui/format.rs
@@ -63,4 +63,8 @@ fn main() {
     format!("{}", 42.to_string());
     let x = std::path::PathBuf::from("/bar/foo/qux");
     format!("{}", x.display().to_string());
+
+    // False positive
+    let a = "foo".to_string();
+    let _ = Some(format!("{}", a + "bar"));
 }
diff --git a/tests/ui/format.stderr b/tests/ui/format.stderr
index 433a0e705ac..9736f34b03b 100644
--- a/tests/ui/format.stderr
+++ b/tests/ui/format.stderr
@@ -75,5 +75,11 @@ error: useless use of `format!`
 LL |     format!("{}", x.display().to_string());
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `x.display().to_string();`
 
-error: aborting due to 12 previous errors
+error: useless use of `format!`
+  --> $DIR/format.rs:69:18
+   |
+LL |     let _ = Some(format!("{}", a + "bar"));
+   |                  ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `a + "bar"`
+
+error: aborting due to 13 previous errors