Skip to content

Commit 2b6310f

Browse files
committed
fix format does not parse escaped braces error
1 parent 37f5c1e commit 2b6310f

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

clippy_lints/src/format.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,13 @@ 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>").into_owned();
95+
let actual_snippet = if actual_snippet == "{{}}" {
96+
String::from("{}")
97+
} else {
98+
actual_snippet
99+
};
100+
let sugg = format!("{}.to_string()", actual_snippet);
95101
span_useless_format(cx, span, "consider using .to_string()", sugg);
96102
}
97103
}

tests/ui/format.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ macro_rules! foo {
1111

1212
fn main() {
1313
"foo".to_string();
14+
"{{}}".to_string();
1415

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

tests/ui/format.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ macro_rules! foo {
1111

1212
fn main() {
1313
format!("foo");
14+
format!("{{}}");
1415

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

tests/ui/format.stderr

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,58 @@ LL | format!("foo");
77
= note: `-D clippy::useless-format` implied by `-D warnings`
88

99
error: useless use of `format!`
10-
--> $DIR/format.rs:15:5
10+
--> $DIR/format.rs:14:5
11+
|
12+
LL | format!("{{}}");
13+
| ^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"{{}}".to_string();`
14+
15+
error: useless use of `format!`
16+
--> $DIR/format.rs:16:5
1117
|
1218
LL | format!("{}", "foo");
1319
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();`
1420

1521
error: useless use of `format!`
16-
--> $DIR/format.rs:19:5
22+
--> $DIR/format.rs:20:5
1723
|
1824
LL | format!("{:+}", "foo"); // Warn when the format makes no difference.
1925
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();`
2026

2127
error: useless use of `format!`
22-
--> $DIR/format.rs:20:5
28+
--> $DIR/format.rs:21:5
2329
|
2430
LL | format!("{:<}", "foo"); // Warn when the format makes no difference.
2531
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string();`
2632

2733
error: useless use of `format!`
28-
--> $DIR/format.rs:25:5
34+
--> $DIR/format.rs:26:5
2935
|
3036
LL | format!("{}", arg);
3137
| ^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();`
3238

3339
error: useless use of `format!`
34-
--> $DIR/format.rs:29:5
40+
--> $DIR/format.rs:30:5
3541
|
3642
LL | format!("{:+}", arg); // Warn when the format makes no difference.
3743
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();`
3844

3945
error: useless use of `format!`
40-
--> $DIR/format.rs:30:5
46+
--> $DIR/format.rs:31:5
4147
|
4248
LL | format!("{:<}", arg); // Warn when the format makes no difference.
4349
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using .to_string(): `arg.to_string();`
4450

4551
error: useless use of `format!`
46-
--> $DIR/format.rs:57:5
52+
--> $DIR/format.rs:58:5
4753
|
4854
LL | format!("{}", 42.to_string());
4955
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: `to_string()` is enough: `42.to_string();`
5056

5157
error: useless use of `format!`
52-
--> $DIR/format.rs:59:5
58+
--> $DIR/format.rs:60:5
5359
|
5460
LL | format!("{}", x.display().to_string());
5561
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: `to_string()` is enough: `x.display().to_string();`
5662

57-
error: aborting due to 9 previous errors
63+
error: aborting due to 10 previous errors
5864

0 commit comments

Comments
 (0)