Skip to content

Commit 9515896

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

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

clippy_lints/src/literal_string_with_formatting_args.rs

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

+18
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";

tests/ui/literal_string_with_formatting_arg.stderr

+11-11
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,61 +8,61 @@ 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:35:18
6060
|
6161
LL | x.expect("———{:?}");
6262
| ^^^^
6363

6464
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
65+
--> tests/ui/literal_string_with_formatting_arg.rs:45:19
6666
|
6767
LL | x.expect(r##" {x:?} "##); // `x` doesn't exist so we shoud not lint
6868
| ^^^^^

0 commit comments

Comments
 (0)