Add raw string regression test for useless_format lint

This commit is contained in:
Lzu Tao 2019-08-23 08:01:41 +00:00
parent 09d302a786
commit ce2d2920ef
4 changed files with 27 additions and 22 deletions

View file

@ -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);
}
}
}

View file

@ -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`.

View file

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

View file

@ -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