Skip to content

Commit 17f9344

Browse files
Fix literal_string_with_formatting_args lint emitted when it should not
1 parent a9c0e22 commit 17f9344

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

clippy_lints/src/literal_string_with_formatting_args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fn emit_lint(cx: &LateContext<'_>, expr: &Expr<'_>, spans: &[(Span, Option<Strin
8181

8282
impl LateLintPass<'_> for LiteralStringWithFormattingArg {
8383
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
84-
if expr.span.from_expansion() {
84+
if expr.span.from_expansion() || expr.span.is_dummy() {
8585
return;
8686
}
8787
if let ExprKind::Lit(lit) = expr.kind {

tests/ui/literal_string_with_formatting_arg.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
#![warn(clippy::literal_string_with_formatting_args)]
22
#![allow(clippy::unnecessary_literal_unwrap)]
33

4+
// Regression test for <https://github.com/rust-lang/rust-clippy/issues/13885>.
5+
// It's not supposed to emit the lint in this case (in `assert!` expansion).
6+
fn compiler_macro() {
7+
fn parse(_: &str) -> Result<(), i32> {
8+
unimplemented!()
9+
}
10+
11+
assert!(
12+
parse(
13+
#[allow(clippy::literal_string_with_formatting_args)]
14+
"foo {:}"
15+
)
16+
.is_err()
17+
);
18+
let value = 0;
19+
assert!(format!("{value}").is_ascii());
20+
}
21+
422
fn main() {
523
let x: Option<usize> = None;
624
let y = "hello";
@@ -13,6 +31,7 @@ fn main() {
1331
x.expect(r"{y:?} {y:?} "); //~ literal_string_with_formatting_args
1432
x.expect(r"{y:?} y:?}"); //~ literal_string_with_formatting_args
1533
x.expect(r##" {y:?} {y:?} "##); //~ literal_string_with_formatting_args
34+
assert!("{y}".is_ascii()); //~ literal_string_with_formatting_args
1635
// Ensure that it doesn't try to go in the middle of a unicode character.
1736
x.expect("———{:?}"); //~ literal_string_with_formatting_args
1837

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this looks like a formatting argument but it is not part of a formatting macro
2-
--> tests/ui/literal_string_with_formatting_arg.rs:7:15
2+
--> tests/ui/literal_string_with_formatting_arg.rs:25:15
33
|
44
LL | x.expect("{y} {}");
55
| ^^^
@@ -8,64 +8,70 @@ LL | x.expect("{y} {}");
88
= help: to override `-D warnings` add `#[allow(clippy::literal_string_with_formatting_args)]`
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:16
11+
--> tests/ui/literal_string_with_formatting_arg.rs:26:16
1212
|
1313
LL | x.expect(" {y} bla");
1414
| ^^^
1515

1616
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
17+
--> tests/ui/literal_string_with_formatting_arg.rs:27:15
1818
|
1919
LL | x.expect("{:?}");
2020
| ^^^^
2121

2222
error: this looks like a formatting argument but it is not part of a formatting macro
23-
--> tests/ui/literal_string_with_formatting_arg.rs:10:15
23+
--> tests/ui/literal_string_with_formatting_arg.rs:28:15
2424
|
2525
LL | x.expect("{y:?}");
2626
| ^^^^^
2727

2828
error: these look like formatting arguments but are not part of a formatting macro
29-
--> tests/ui/literal_string_with_formatting_arg.rs:11:16
29+
--> tests/ui/literal_string_with_formatting_arg.rs:29:16
3030
|
3131
LL | x.expect(" {y:?} {y:?} ");
3232
| ^^^^^ ^^^^^
3333

3434
error: this looks like a formatting argument but it is not part of a formatting macro
35-
--> tests/ui/literal_string_with_formatting_arg.rs:12:23
35+
--> tests/ui/literal_string_with_formatting_arg.rs:30:23
3636
|
3737
LL | x.expect(" {y:..} {y:?} ");
3838
| ^^^^^
3939

4040
error: these look like formatting arguments but are not part of a formatting macro
41-
--> tests/ui/literal_string_with_formatting_arg.rs:13:16
41+
--> tests/ui/literal_string_with_formatting_arg.rs:31:16
4242
|
4343
LL | x.expect(r"{y:?} {y:?} ");
4444
| ^^^^^ ^^^^^
4545

4646
error: this looks like a formatting argument but it is not part of a formatting macro
47-
--> tests/ui/literal_string_with_formatting_arg.rs:14:16
47+
--> tests/ui/literal_string_with_formatting_arg.rs:32:16
4848
|
4949
LL | x.expect(r"{y:?} y:?}");
5050
| ^^^^^
5151

5252
error: these look like formatting arguments but are not part of a formatting macro
53-
--> tests/ui/literal_string_with_formatting_arg.rs:15:19
53+
--> tests/ui/literal_string_with_formatting_arg.rs:33:19
5454
|
5555
LL | x.expect(r##" {y:?} {y:?} "##);
5656
| ^^^^^ ^^^^^
5757

5858
error: this looks like a formatting argument but it is not part of a formatting macro
59-
--> tests/ui/literal_string_with_formatting_arg.rs:17:18
59+
--> tests/ui/literal_string_with_formatting_arg.rs:34:14
60+
|
61+
LL | assert!("{y}".is_ascii());
62+
| ^^^
63+
64+
error: this looks like a formatting argument but it is not part of a formatting macro
65+
--> tests/ui/literal_string_with_formatting_arg.rs:36:18
6066
|
6167
LL | x.expect("———{:?}");
6268
| ^^^^
6369

6470
error: this looks like a formatting argument but it is not part of a formatting macro
65-
--> tests/ui/literal_string_with_formatting_arg.rs:27:19
71+
--> tests/ui/literal_string_with_formatting_arg.rs:46:19
6672
|
6773
LL | x.expect(r##" {x:?} "##); // `x` doesn't exist so we shoud not lint
6874
| ^^^^^
6975

70-
error: aborting due to 11 previous errors
76+
error: aborting due to 12 previous errors
7177

0 commit comments

Comments
 (0)