Add raw string regression test for useless_format lint

This commit is contained in:
Lzu Tao 2019-08-23 08:01:41 +00:00 committed by flip1995
parent 1564306943
commit 413eb5b946
No known key found for this signature in database
GPG key ID: 693086869D506637
4 changed files with 27 additions and 22 deletions

View file

@ -92,11 +92,8 @@ fn on_argumentv1_new<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, arm
then { then {
if let ExprKind::Lit(ref lit) = format_args.node { if let ExprKind::Lit(ref lit) = format_args.node {
if let LitKind::Str(ref s, _) = lit.node { if let LitKind::Str(ref s, _) = lit.node {
let snip = s.as_str().replace("{{}}", "{}"); return Some(format!("{:?}.to_string()", s.as_str()));
let sugg = format!("\"{}\".to_string()", snip);
return Some(sugg);
} }
return None;
} else { } else {
let snip = snippet(cx, format_args.span, "<arg>"); let snip = snippet(cx, format_args.span, "<arg>");
if let ExprKind::MethodCall(ref path, _, _) = format_args.node { 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<S
then { then {
// `format!("foo")` expansion contains `match () { () => [], }` // `format!("foo")` expansion contains `match () { () => [], }`
if tup.is_empty() { if tup.is_empty() {
let snip = s.as_str().replace("{{}}", "{}"); return Some(format!("{:?}.to_string()", s.as_str()));
let sugg = format!("\"{}\".to_string()", snip); } else if s.as_str().is_empty() {
return Some(sugg);
} else {
if s.as_str().is_empty() {
return on_argumentv1_new(cx, &tup[0], arms); return on_argumentv1_new(cx, &tup[0], arms);
} else {
return None;
}
} }
} }
} }

View file

@ -13,6 +13,7 @@ fn main() {
"foo".to_string(); "foo".to_string();
"{}".to_string(); "{}".to_string();
"{} abc {}".to_string(); "{} abc {}".to_string();
"foo {}\n\" bar".to_string();
"foo".to_string(); "foo".to_string();
format!("{:?}", "foo"); // Don't warn about `Debug`. format!("{:?}", "foo"); // Don't warn about `Debug`.

View file

@ -13,6 +13,10 @@ fn main() {
format!("foo"); format!("foo");
format!("{{}}"); format!("{{}}");
format!("{{}} abc {{}}"); format!("{{}} abc {{}}");
format!(
r##"foo {{}}
" bar"##
);
format!("{}", "foo"); format!("{}", "foo");
format!("{:?}", "foo"); // Don't warn about `Debug`. format!("{:?}", "foo"); // Don't warn about `Debug`.

View file

@ -19,52 +19,61 @@ LL | format!("{{}} abc {{}}");
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"{} abc {}".to_string();` | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"{} abc {}".to_string();`
error: useless use of `format!` 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"); LL | format!("{}", "foo");
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();` | ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();`
error: useless use of `format!` 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. LL | format!("{:+}", "foo"); // Warn when the format makes no difference.
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();` | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();`
error: useless use of `format!` 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. LL | format!("{:<}", "foo"); // Warn when the format makes no difference.
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();` | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();`
error: useless use of `format!` error: useless use of `format!`
--> $DIR/format.rs:27:5 --> $DIR/format.rs:31:5
| |
LL | format!("{}", arg); LL | format!("{}", arg);
| ^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();` | ^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();`
error: useless use of `format!` 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. LL | format!("{:+}", arg); // Warn when the format makes no difference.
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();` | ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();`
error: useless use of `format!` 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. LL | format!("{:<}", arg); // Warn when the format makes no difference.
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();` | ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();`
error: useless use of `format!` error: useless use of `format!`
--> $DIR/format.rs:59:5 --> $DIR/format.rs:63:5
| |
LL | format!("{}", 42.to_string()); LL | format!("{}", 42.to_string());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `42.to_string();` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `42.to_string();`
error: useless use of `format!` error: useless use of `format!`
--> $DIR/format.rs:61:5 --> $DIR/format.rs:65:5
| |
LL | format!("{}", x.display().to_string()); LL | format!("{}", x.display().to_string());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `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