about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLzu Tao <taolzu@gmail.com>2019-08-23 08:01:41 +0000
committerLzu Tao <taolzu@gmail.com>2019-08-23 09:16:50 +0000
commitce2d2920efc7d1ac2f54c852a63732cb0a2792ad (patch)
tree9f4e51236d5425052aa34a0b619c6e4e8fe7ebcc
parent09d302a786b91e7842af4d7e0fb707b2ca69a4c5 (diff)
downloadrust-ce2d2920efc7d1ac2f54c852a63732cb0a2792ad.tar.gz
rust-ce2d2920efc7d1ac2f54c852a63732cb0a2792ad.zip
Add raw string regression test for useless_format lint
-rw-r--r--clippy_lints/src/format.rs17
-rw-r--r--tests/ui/format.fixed1
-rw-r--r--tests/ui/format.rs4
-rw-r--r--tests/ui/format.stderr27
4 files changed, 27 insertions, 22 deletions
diff --git a/clippy_lints/src/format.rs b/clippy_lints/src/format.rs
index d0da224e6e9..ceba8278fb0 100644
--- a/clippy_lints/src/format.rs
+++ b/clippy_lints/src/format.rs
@@ -89,11 +89,8 @@ fn on_argumentv1_new<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, arm
         then {
             if let ExprKind::Lit(ref lit) = format_args.node {
                 if let LitKind::Str(ref s, _) = lit.node {
-                    let snip = s.as_str().replace("{{}}", "{}");
-                    let sugg = format!("\"{}\".to_string()", snip);
-                    return Some(sugg);
+                    return Some(format!("{:?}.to_string()", s.as_str()));
                 }
-                return None;
             } else {
                 let snip = snippet(cx, format_args.span, "<arg>");
                 if let ExprKind::MethodCall(ref path, _, _) = format_args.node {
@@ -129,15 +126,9 @@ fn on_new_v1<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option<S
         then {
             // `format!("foo")` expansion contains `match () { () => [], }`
             if tup.is_empty() {
-                let snip = s.as_str().replace("{{}}", "{}");
-                let sugg = format!("\"{}\".to_string()", snip);
-                return Some(sugg);
-            } else {
-                if s.as_str().is_empty() {
-                    return on_argumentv1_new(cx, &tup[0], arms);
-                } else {
-                    return None;
-                }
+                return Some(format!("{:?}.to_string()", s.as_str()));
+            } else if s.as_str().is_empty() {
+                return on_argumentv1_new(cx, &tup[0], arms);
             }
         }
     }
diff --git a/tests/ui/format.fixed b/tests/ui/format.fixed
index e4217411f05..401a1c533fe 100644
--- a/tests/ui/format.fixed
+++ b/tests/ui/format.fixed
@@ -13,6 +13,7 @@ fn main() {
     "foo".to_string();
     "{}".to_string();
     "{} abc {}".to_string();
+    "foo {}\n\" bar".to_string();
 
     "foo".to_string();
     format!("{:?}", "foo"); // Don't warn about `Debug`.
diff --git a/tests/ui/format.rs b/tests/ui/format.rs
index 61ef3e7243d..1cbc15cfcec 100644
--- a/tests/ui/format.rs
+++ b/tests/ui/format.rs
@@ -13,6 +13,10 @@ fn main() {
     format!("foo");
     format!("{{}}");
     format!("{{}} abc {{}}");
+    format!(
+        r##"foo {{}}
+" bar"##
+    );
 
     format!("{}", "foo");
     format!("{:?}", "foo"); // Don't warn about `Debug`.
diff --git a/tests/ui/format.stderr b/tests/ui/format.stderr
index d81f48b0864..433a0e705ac 100644
--- a/tests/ui/format.stderr
+++ b/tests/ui/format.stderr
@@ -19,52 +19,61 @@ LL |     format!("{{}} abc {{}}");
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"{} abc {}".to_string();`
 
 error: useless use of `format!`
-  --> $DIR/format.rs:17:5
+  --> $DIR/format.rs:16:5
+   |
+LL | /     format!(
+LL | |         r##"foo {{}}
+LL | | " bar"##
+LL | |     );
+   | |______^ help: consider using .to_string(): `"foo {}/n/" bar".to_string();`
+
+error: useless use of `format!`
+  --> $DIR/format.rs:21:5
    |
 LL |     format!("{}", "foo");
    |     ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();`
 
 error: useless use of `format!`
-  --> $DIR/format.rs:21:5
+  --> $DIR/format.rs:25:5
    |
 LL |     format!("{:+}", "foo"); // Warn when the format makes no difference.
    |     ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();`
 
 error: useless use of `format!`
-  --> $DIR/format.rs:22:5
+  --> $DIR/format.rs:26:5
    |
 LL |     format!("{:<}", "foo"); // Warn when the format makes no difference.
    |     ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();`
 
 error: useless use of `format!`
-  --> $DIR/format.rs:27:5
+  --> $DIR/format.rs:31:5
    |
 LL |     format!("{}", arg);
    |     ^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();`
 
 error: useless use of `format!`
-  --> $DIR/format.rs:31:5
+  --> $DIR/format.rs:35:5
    |
 LL |     format!("{:+}", arg); // Warn when the format makes no difference.
    |     ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();`
 
 error: useless use of `format!`
-  --> $DIR/format.rs:32:5
+  --> $DIR/format.rs:36:5
    |
 LL |     format!("{:<}", arg); // Warn when the format makes no difference.
    |     ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();`
 
 error: useless use of `format!`
-  --> $DIR/format.rs:59:5
+  --> $DIR/format.rs:63:5
    |
 LL |     format!("{}", 42.to_string());
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `42.to_string();`
 
 error: useless use of `format!`
-  --> $DIR/format.rs:61:5
+  --> $DIR/format.rs:65:5
    |
 LL |     format!("{}", x.display().to_string());
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `x.display().to_string();`
 
-error: aborting due to 11 previous errors
+error: aborting due to 12 previous errors