Skip to content

Commit 2d7c05d

Browse files
Disable checks on {}
1 parent 9fd308f commit 2d7c05d

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

clippy_lints/src/literal_string_with_formatting_arg.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,15 @@ impl EarlyLintPass for LiteralStringWithFormattingArg {
7070
continue;
7171
}
7272

73-
let mut end = fmt_str[pos.end..].find('}').map_or(pos.end, |found| found + pos.end);
74-
if fmt_str[start..end].contains(':') {
75-
end += 1;
73+
if fmt_str[start + 1..].trim_start().starts_with('}') {
74+
// For now, we ignore `{}`.
75+
continue;
7676
}
77+
78+
let end = fmt_str[start + 1..]
79+
.find('}')
80+
.map_or(pos.end, |found| start + 1 + found)
81+
+ 1;
7782
spans.push(
7883
expr.span
7984
.with_hi(lo + BytePos((start + add) as _))

tests/ui/literal_string_with_formatting_arg.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ fn main() {
55
let x: Option<usize> = None;
66
let y = "hello";
77
x.expect("{y} {}"); //~ literal_string_with_formatting_arg
8+
x.expect(" {y} bla"); //~ literal_string_with_formatting_arg
89
x.expect("{:?}"); //~ literal_string_with_formatting_arg
910
x.expect("{y:?}"); //~ literal_string_with_formatting_arg
1011
x.expect(" {y:?} {y:?} "); //~ literal_string_with_formatting_arg
1112
x.expect(" {y:..} {y:?} "); //~ literal_string_with_formatting_arg
1213
x.expect(r"{y:?} {y:?} "); //~ literal_string_with_formatting_arg
1314
x.expect(r"{y:?} y:?}"); //~ literal_string_with_formatting_arg
1415
x.expect(r##" {y:?} {y:?} "##); //~ literal_string_with_formatting_arg
15-
"\\.+*?()|[]{}^$#&-~".chars().any(|x| x == 'a'); //~ literal_string_with_formatting_arg
1616
// Ensure that it doesn't try to go in the middle of a unicode character.
17-
x.expect("———{}"); //~ literal_string_with_formatting_arg
17+
x.expect("———{:?}"); //~ literal_string_with_formatting_arg
1818

1919
// Should not lint!
2020
format!("{y:?}");
2121
println!("{y:?}");
22+
x.expect(" {} "); // For now we ignore `{}` to limit false positives.
23+
x.expect(" { } "); // For now we ignore `{}` to limit false positives.
2224
x.expect("{{y} {x");
2325
x.expect("{{y:?}");
2426
x.expect("{y:...}");
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,65 @@
1-
error: these look like formatting arguments but are not part of a formatting macro
1+
error: this looks like a formatting argument but it is not part of a formatting macro
22
--> tests/ui/literal_string_with_formatting_arg.rs:7:15
33
|
44
LL | x.expect("{y} {}");
5-
| ^^^^^^
5+
| ^^^
66
|
77
= note: `-D clippy::literal-string-with-formatting-arg` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::literal_string_with_formatting_arg)]`
99

1010
error: this looks like a formatting argument but it is not part of a formatting macro
11-
--> tests/ui/literal_string_with_formatting_arg.rs:8:15
11+
--> tests/ui/literal_string_with_formatting_arg.rs:8:16
12+
|
13+
LL | x.expect(" {y} bla");
14+
| ^^^
15+
16+
error: this looks like a formatting argument but it is not part of a formatting macro
17+
--> tests/ui/literal_string_with_formatting_arg.rs:9:15
1218
|
1319
LL | x.expect("{:?}");
1420
| ^^^^
1521

1622
error: this looks like a formatting argument but it is not part of a formatting macro
17-
--> tests/ui/literal_string_with_formatting_arg.rs:9:15
23+
--> tests/ui/literal_string_with_formatting_arg.rs:10:15
1824
|
1925
LL | x.expect("{y:?}");
2026
| ^^^^^
2127

2228
error: these look like formatting arguments but are not part of a formatting macro
23-
--> tests/ui/literal_string_with_formatting_arg.rs:10:16
29+
--> tests/ui/literal_string_with_formatting_arg.rs:11:16
2430
|
2531
LL | x.expect(" {y:?} {y:?} ");
2632
| ^^^^^ ^^^^^
2733

2834
error: this looks like a formatting argument but it is not part of a formatting macro
29-
--> tests/ui/literal_string_with_formatting_arg.rs:11:23
35+
--> tests/ui/literal_string_with_formatting_arg.rs:12:23
3036
|
3137
LL | x.expect(" {y:..} {y:?} ");
3238
| ^^^^^
3339

3440
error: these look like formatting arguments but are not part of a formatting macro
35-
--> tests/ui/literal_string_with_formatting_arg.rs:12:16
41+
--> tests/ui/literal_string_with_formatting_arg.rs:13:16
3642
|
3743
LL | x.expect(r"{y:?} {y:?} ");
3844
| ^^^^^ ^^^^^
3945

4046
error: this looks like a formatting argument but it is not part of a formatting macro
41-
--> tests/ui/literal_string_with_formatting_arg.rs:13:16
47+
--> tests/ui/literal_string_with_formatting_arg.rs:14:16
4248
|
4349
LL | x.expect(r"{y:?} y:?}");
4450
| ^^^^^
4551

4652
error: these look like formatting arguments but are not part of a formatting macro
47-
--> tests/ui/literal_string_with_formatting_arg.rs:14:19
53+
--> tests/ui/literal_string_with_formatting_arg.rs:15:19
4854
|
4955
LL | x.expect(r##" {y:?} {y:?} "##);
5056
| ^^^^^ ^^^^^
5157

52-
error: this looks like a formatting argument but it is not part of a formatting macro
53-
--> tests/ui/literal_string_with_formatting_arg.rs:15:17
54-
|
55-
LL | "\\.+*?()|[]{}^$#&-~".chars().any(|x| x == 'a');
56-
| ^^
57-
5858
error: this looks like a formatting argument but it is not part of a formatting macro
5959
--> tests/ui/literal_string_with_formatting_arg.rs:17:18
6060
|
61-
LL | x.expect("———{}");
62-
| ^^
61+
LL | x.expect("———{:?}");
62+
| ^^^^
6363

6464
error: aborting due to 10 previous errors
6565

0 commit comments

Comments
 (0)