Fix literal_string_with_formatting_args lint emitted when it should not

This commit is contained in:
Guillaume Gomez 2025-01-06 19:03:55 +01:00
parent a9c0e22dfa
commit 17f9344a96
3 changed files with 38 additions and 13 deletions

View file

@ -81,7 +81,7 @@ fn emit_lint(cx: &LateContext<'_>, expr: &Expr<'_>, spans: &[(Span, Option<Strin
impl LateLintPass<'_> for LiteralStringWithFormattingArg {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
if expr.span.from_expansion() {
if expr.span.from_expansion() || expr.span.is_dummy() {
return;
}
if let ExprKind::Lit(lit) = expr.kind {

View file

@ -1,6 +1,24 @@
#![warn(clippy::literal_string_with_formatting_args)]
#![allow(clippy::unnecessary_literal_unwrap)]
// Regression test for <https://github.com/rust-lang/rust-clippy/issues/13885>.
// It's not supposed to emit the lint in this case (in `assert!` expansion).
fn compiler_macro() {
fn parse(_: &str) -> Result<(), i32> {
unimplemented!()
}
assert!(
parse(
#[allow(clippy::literal_string_with_formatting_args)]
"foo {:}"
)
.is_err()
);
let value = 0;
assert!(format!("{value}").is_ascii());
}
fn main() {
let x: Option<usize> = None;
let y = "hello";
@ -13,6 +31,7 @@ fn main() {
x.expect(r"{y:?} {y:?} "); //~ literal_string_with_formatting_args
x.expect(r"{y:?} y:?}"); //~ literal_string_with_formatting_args
x.expect(r##" {y:?} {y:?} "##); //~ literal_string_with_formatting_args
assert!("{y}".is_ascii()); //~ literal_string_with_formatting_args
// Ensure that it doesn't try to go in the middle of a unicode character.
x.expect("———{:?}"); //~ literal_string_with_formatting_args

View file

@ -1,5 +1,5 @@
error: this looks like a formatting argument but it is not part of a formatting macro
--> tests/ui/literal_string_with_formatting_arg.rs:7:15
--> tests/ui/literal_string_with_formatting_arg.rs:25:15
|
LL | x.expect("{y} {}");
| ^^^
@ -8,64 +8,70 @@ LL | x.expect("{y} {}");
= help: to override `-D warnings` add `#[allow(clippy::literal_string_with_formatting_args)]`
error: this looks like a formatting argument but it is not part of a formatting macro
--> tests/ui/literal_string_with_formatting_arg.rs:8:16
--> tests/ui/literal_string_with_formatting_arg.rs:26:16
|
LL | x.expect(" {y} bla");
| ^^^
error: this looks like a formatting argument but it is not part of a formatting macro
--> tests/ui/literal_string_with_formatting_arg.rs:9:15
--> tests/ui/literal_string_with_formatting_arg.rs:27:15
|
LL | x.expect("{:?}");
| ^^^^
error: this looks like a formatting argument but it is not part of a formatting macro
--> tests/ui/literal_string_with_formatting_arg.rs:10:15
--> tests/ui/literal_string_with_formatting_arg.rs:28:15
|
LL | x.expect("{y:?}");
| ^^^^^
error: these look like formatting arguments but are not part of a formatting macro
--> tests/ui/literal_string_with_formatting_arg.rs:11:16
--> tests/ui/literal_string_with_formatting_arg.rs:29:16
|
LL | x.expect(" {y:?} {y:?} ");
| ^^^^^ ^^^^^
error: this looks like a formatting argument but it is not part of a formatting macro
--> tests/ui/literal_string_with_formatting_arg.rs:12:23
--> tests/ui/literal_string_with_formatting_arg.rs:30:23
|
LL | x.expect(" {y:..} {y:?} ");
| ^^^^^
error: these look like formatting arguments but are not part of a formatting macro
--> tests/ui/literal_string_with_formatting_arg.rs:13:16
--> tests/ui/literal_string_with_formatting_arg.rs:31:16
|
LL | x.expect(r"{y:?} {y:?} ");
| ^^^^^ ^^^^^
error: this looks like a formatting argument but it is not part of a formatting macro
--> tests/ui/literal_string_with_formatting_arg.rs:14:16
--> tests/ui/literal_string_with_formatting_arg.rs:32:16
|
LL | x.expect(r"{y:?} y:?}");
| ^^^^^
error: these look like formatting arguments but are not part of a formatting macro
--> tests/ui/literal_string_with_formatting_arg.rs:15:19
--> tests/ui/literal_string_with_formatting_arg.rs:33:19
|
LL | x.expect(r##" {y:?} {y:?} "##);
| ^^^^^ ^^^^^
error: this looks like a formatting argument but it is not part of a formatting macro
--> tests/ui/literal_string_with_formatting_arg.rs:17:18
--> tests/ui/literal_string_with_formatting_arg.rs:34:14
|
LL | assert!("{y}".is_ascii());
| ^^^
error: this looks like a formatting argument but it is not part of a formatting macro
--> tests/ui/literal_string_with_formatting_arg.rs:36:18
|
LL | x.expect("———{:?}");
| ^^^^
error: this looks like a formatting argument but it is not part of a formatting macro
--> tests/ui/literal_string_with_formatting_arg.rs:27:19
--> tests/ui/literal_string_with_formatting_arg.rs:46:19
|
LL | x.expect(r##" {x:?} "##); // `x` doesn't exist so we shoud not lint
| ^^^^^
error: aborting due to 11 previous errors
error: aborting due to 12 previous errors