From 413eb5b946955cd33ffd6fb58d7c59de978f92d6 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Fri, 23 Aug 2019 08:01:41 +0000 Subject: [PATCH] Add raw string regression test for useless_format lint --- clippy_lints/src/format.rs | 17 ++++------------- tests/ui/format.fixed | 1 + tests/ui/format.rs | 4 ++++ tests/ui/format.stderr | 27 ++++++++++++++++++--------- 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/clippy_lints/src/format.rs b/clippy_lints/src/format.rs index add1056b0..f28f98b1c 100644 --- a/clippy_lints/src/format.rs +++ b/clippy_lints/src/format.rs @@ -92,11 +92,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, ""); if let ExprKind::MethodCall(ref path, _, _) = format_args.node { @@ -132,15 +129,9 @@ fn on_new_v1<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option [], }` 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 e4217411f..401a1c533 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 61ef3e724..1cbc15cfc 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 d81f48b08..433a0e705 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