Skip to content

Commit ce2d292

Browse files
committed
Add raw string regression test for useless_format lint
1 parent 09d302a commit ce2d292

File tree

4 files changed

+27
-22
lines changed

4 files changed

+27
-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

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ fn main() {
1313
format!("foo");
1414
format!("{{}}");
1515
format!("{{}} abc {{}}");
16+
format!(
17+
r##"foo {{}}
18+
" bar"##
19+
);
1620

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

tests/ui/format.stderr

+18-9
Original file line numberDiff line numberDiff line change
@@ -19,52 +19,61 @@ 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!(
25+
LL | | r##"foo {{}}
26+
LL | | " bar"##
27+
LL | | );
28+
| |______^ help: consider using .to_string(): `"foo {}/n/" bar".to_string();`
29+
30+
error: useless use of `format!`
31+
--> $DIR/format.rs:21:5
2332
|
2433
LL | format!("{}", "foo");
2534
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();`
2635

2736
error: useless use of `format!`
28-
--> $DIR/format.rs:21:5
37+
--> $DIR/format.rs:25:5
2938
|
3039
LL | format!("{:+}", "foo"); // Warn when the format makes no difference.
3140
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();`
3241

3342
error: useless use of `format!`
34-
--> $DIR/format.rs:22:5
43+
--> $DIR/format.rs:26:5
3544
|
3645
LL | format!("{:<}", "foo"); // Warn when the format makes no difference.
3746
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();`
3847

3948
error: useless use of `format!`
40-
--> $DIR/format.rs:27:5
49+
--> $DIR/format.rs:31:5
4150
|
4251
LL | format!("{}", arg);
4352
| ^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();`
4453

4554
error: useless use of `format!`
46-
--> $DIR/format.rs:31:5
55+
--> $DIR/format.rs:35:5
4756
|
4857
LL | format!("{:+}", arg); // Warn when the format makes no difference.
4958
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();`
5059

5160
error: useless use of `format!`
52-
--> $DIR/format.rs:32:5
61+
--> $DIR/format.rs:36:5
5362
|
5463
LL | format!("{:<}", arg); // Warn when the format makes no difference.
5564
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();`
5665

5766
error: useless use of `format!`
58-
--> $DIR/format.rs:59:5
67+
--> $DIR/format.rs:63:5
5968
|
6069
LL | format!("{}", 42.to_string());
6170
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `42.to_string();`
6271

6372
error: useless use of `format!`
64-
--> $DIR/format.rs:61:5
73+
--> $DIR/format.rs:65:5
6574
|
6675
LL | format!("{}", x.display().to_string());
6776
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `x.display().to_string();`
6877

69-
error: aborting due to 11 previous errors
78+
error: aborting due to 12 previous errors
7079

0 commit comments

Comments
 (0)