Skip to content

Commit d516925

Browse files
committed
Auto merge of #3946 - rchaser53:issue-3920, r=flip1995
fix format does not parse escaped braces error related: #3920
2 parents abf7f91 + db6ca9b commit d516925

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

clippy_lints/src/format.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
9191
ExprKind::Match(ref matchee, _, _) => {
9292
if let ExprKind::Tup(ref tup) = matchee.node {
9393
if tup.is_empty() {
94-
let sugg = format!("{}.to_string()", snippet(cx, expr.span, "<expr>").into_owned());
94+
let actual_snippet = snippet(cx, expr.span, "<expr>").to_string();
95+
let actual_snippet = actual_snippet.replace("{{}}", "{}");
96+
let sugg = format!("{}.to_string()", actual_snippet);
9597
span_useless_format(cx, span, "consider using .to_string()", sugg);
9698
}
9799
}

tests/ui/format.fixed

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ macro_rules! foo {
1111

1212
fn main() {
1313
"foo".to_string();
14+
"{}".to_string();
15+
"{} abc {}".to_string();
1416

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

tests/ui/format.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ macro_rules! foo {
1111

1212
fn main() {
1313
format!("foo");
14+
format!("{{}}");
15+
format!("{{}} abc {{}}");
1416

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

tests/ui/format.stderr

+20-8
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,65 @@ LL | format!("foo");
66
|
77
= note: `-D clippy::useless-format` implied by `-D warnings`
88

9+
error: useless use of `format!`
10+
--> $DIR/format.rs:14:5
11+
|
12+
LL | format!("{{}}");
13+
| ^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"{}".to_string();`
14+
915
error: useless use of `format!`
1016
--> $DIR/format.rs:15:5
1117
|
18+
LL | format!("{{}} abc {{}}");
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"{} abc {}".to_string();`
20+
21+
error: useless use of `format!`
22+
--> $DIR/format.rs:17:5
23+
|
1224
LL | format!("{}", "foo");
1325
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();`
1426

1527
error: useless use of `format!`
16-
--> $DIR/format.rs:19:5
28+
--> $DIR/format.rs:21:5
1729
|
1830
LL | format!("{:+}", "foo"); // Warn when the format makes no difference.
1931
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();`
2032

2133
error: useless use of `format!`
22-
--> $DIR/format.rs:20:5
34+
--> $DIR/format.rs:22:5
2335
|
2436
LL | format!("{:<}", "foo"); // Warn when the format makes no difference.
2537
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();`
2638

2739
error: useless use of `format!`
28-
--> $DIR/format.rs:25:5
40+
--> $DIR/format.rs:27:5
2941
|
3042
LL | format!("{}", arg);
3143
| ^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();`
3244

3345
error: useless use of `format!`
34-
--> $DIR/format.rs:29:5
46+
--> $DIR/format.rs:31:5
3547
|
3648
LL | format!("{:+}", arg); // Warn when the format makes no difference.
3749
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();`
3850

3951
error: useless use of `format!`
40-
--> $DIR/format.rs:30:5
52+
--> $DIR/format.rs:32:5
4153
|
4254
LL | format!("{:<}", arg); // Warn when the format makes no difference.
4355
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();`
4456

4557
error: useless use of `format!`
46-
--> $DIR/format.rs:57:5
58+
--> $DIR/format.rs:59:5
4759
|
4860
LL | format!("{}", 42.to_string());
4961
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: `to_string()` is enough: `42.to_string();`
5062

5163
error: useless use of `format!`
52-
--> $DIR/format.rs:59:5
64+
--> $DIR/format.rs:61:5
5365
|
5466
LL | format!("{}", x.display().to_string());
5567
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: `to_string()` is enough: `x.display().to_string();`
5668

57-
error: aborting due to 9 previous errors
69+
error: aborting due to 11 previous errors
5870

0 commit comments

Comments
 (0)