Skip to content

Commit b390aef

Browse files
committed
Add raw string regression test for useless_format lint
1 parent db604ab commit b390aef

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

clippy_lints/src/format.rs

+4-13
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,8 @@ fn on_argumentv1_new<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, arm
8989
then {
9090
if let ExprKind::Lit(ref lit) = format_args.node {
9191
if let LitKind::Str(ref s, _) = lit.node {
92-
let snip = s.as_str().replace("{{}}", "{}");
93-
let sugg = format!("\"{}\".to_string()", snip);
94-
return Some(sugg);
92+
return Some(format!("{:?}.to_string()", s.as_str()));
9593
}
96-
return None;
9794
} else {
9895
let snip = snippet(cx, format_args.span, "<arg>");
9996
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
129126
then {
130127
// `format!("foo")` expansion contains `match () { () => [], }`
131128
if tup.is_empty() {
132-
let snip = s.as_str().replace("{{}}", "{}");
133-
let sugg = format!("\"{}\".to_string()", snip);
134-
return Some(sugg);
135-
} else {
136-
if s.as_str().is_empty() {
137-
return on_argumentv1_new(cx, &tup[0], arms);
138-
} else {
139-
return None;
140-
}
129+
return Some(format!("{:?}.to_string()", s.as_str()));
130+
} else if s.as_str().is_empty() {
131+
return on_argumentv1_new(cx, &tup[0], arms);
141132
}
142133
}
143134
}

tests/ui/format.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ fn main() {
1313
"foo".to_string();
1414
"{}".to_string();
1515
"{} abc {}".to_string();
16+
"foo {}\n\" bar".to_string();
1617

1718
"foo".to_string();
1819
format!("{:?}", "foo"); // Don't warn about `Debug`.

tests/ui/format.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ fn main() {
1313
format!("foo");
1414
format!("{{}}");
1515
format!("{{}} abc {{}}");
16+
format!(r##"foo {{}}
17+
" bar"##);
1618

1719
format!("{}", "foo");
1820
format!("{:?}", "foo"); // Don't warn about `Debug`.

tests/ui/format.stderr

+16-9
Original file line numberDiff line numberDiff line change
@@ -19,52 +19,59 @@ LL | format!("{{}} abc {{}}");
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"{} abc {}".to_string();`
2020

2121
error: useless use of `format!`
22-
--> $DIR/format.rs:17:5
22+
--> $DIR/format.rs:16:5
23+
|
24+
LL | / format!(r##"foo {{}}
25+
LL | | " bar"##);
26+
| |__________^ help: consider using .to_string(): `"foo {}/n/" bar".to_string();`
27+
28+
error: useless use of `format!`
29+
--> $DIR/format.rs:19:5
2330
|
2431
LL | format!("{}", "foo");
2532
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();`
2633

2734
error: useless use of `format!`
28-
--> $DIR/format.rs:21:5
35+
--> $DIR/format.rs:23:5
2936
|
3037
LL | format!("{:+}", "foo"); // Warn when the format makes no difference.
3138
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();`
3239

3340
error: useless use of `format!`
34-
--> $DIR/format.rs:22:5
41+
--> $DIR/format.rs:24:5
3542
|
3643
LL | format!("{:<}", "foo"); // Warn when the format makes no difference.
3744
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();`
3845

3946
error: useless use of `format!`
40-
--> $DIR/format.rs:27:5
47+
--> $DIR/format.rs:29:5
4148
|
4249
LL | format!("{}", arg);
4350
| ^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();`
4451

4552
error: useless use of `format!`
46-
--> $DIR/format.rs:31:5
53+
--> $DIR/format.rs:33:5
4754
|
4855
LL | format!("{:+}", arg); // Warn when the format makes no difference.
4956
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();`
5057

5158
error: useless use of `format!`
52-
--> $DIR/format.rs:32:5
59+
--> $DIR/format.rs:34:5
5360
|
5461
LL | format!("{:<}", arg); // Warn when the format makes no difference.
5562
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();`
5663

5764
error: useless use of `format!`
58-
--> $DIR/format.rs:59:5
65+
--> $DIR/format.rs:61:5
5966
|
6067
LL | format!("{}", 42.to_string());
6168
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `42.to_string();`
6269

6370
error: useless use of `format!`
64-
--> $DIR/format.rs:61:5
71+
--> $DIR/format.rs:63:5
6572
|
6673
LL | format!("{}", x.display().to_string());
6774
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `x.display().to_string();`
6875

69-
error: aborting due to 11 previous errors
76+
error: aborting due to 12 previous errors
7077

0 commit comments

Comments
 (0)