]> git.lizzy.rs Git - rust.git/commitdiff
Re-add false positive check
authorLzu Tao <taolzu@gmail.com>
Fri, 23 Aug 2019 08:46:23 +0000 (15:46 +0700)
committerflip1995 <hello@philkrones.com>
Tue, 3 Sep 2019 16:07:28 +0000 (18:07 +0200)
clippy_lints/src/format.rs
tests/ui/format.fixed
tests/ui/format.rs
tests/ui/format.stderr

index f28f98b1caba80ddb9aea1b367cffdc4ca913fe2..a20217991ab96c8891fd6bcc813f1426029569c3 100644 (file)
@@ -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));
             }
index 401a1c533fefc5064a923852b4d2d642335dd013..6e100230a3ad338626ef7d2ab2b35e0a881a520f 100644 (file)
@@ -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");
 }
index 1cbc15cfcecbf91a6675e0b073456d10d91d6aa7..1fae6603ac099debbc7b1509f635da5fa7997f5a 100644 (file)
@@ -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"));
 }
index 433a0e705aca11913f89b16648720246a47829a1..9736f34b03b4a86d6c78c58a05dc9559a902e0b4 100644 (file)
@@ -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