Add regression test for literal_string_with_formatting_arg

This commit is contained in:
Guillaume Gomez 2024-09-18 00:10:16 +02:00
parent cd7cec9066
commit 922aabe348
2 changed files with 94 additions and 0 deletions

View file

@ -0,0 +1,29 @@
#![warn(clippy::literal_string_with_formatting_arg)]
#![allow(clippy::unnecessary_literal_unwrap)]
fn main() {
let x: Option<usize> = None;
let y = "hello";
x.expect("{y} {}"); //~ literal_string_with_formatting_arg
x.expect("{:?}"); //~ literal_string_with_formatting_arg
x.expect("{y:?}"); //~ literal_string_with_formatting_arg
x.expect(" {y:?} {y:?} "); //~ literal_string_with_formatting_arg
x.expect(" {y:..} {y:?} "); //~ literal_string_with_formatting_arg
x.expect(r"{y:?} {y:?} "); //~ literal_string_with_formatting_arg
x.expect(r"{y:?} y:?}"); //~ literal_string_with_formatting_arg
x.expect(r##" {y:?} {y:?} "##); //~ literal_string_with_formatting_arg
"\\.+*?()|[]{}^$#&-~".chars().any(|x| x == 'a'); //~ literal_string_with_formatting_arg
// Ensure that it doesn't try to go in the middle of a unicode character.
x.expect("———{}"); //~ literal_string_with_formatting_arg
// Should not lint!
format!("{y:?}");
println!("{y:?}");
x.expect("{{y} {x");
x.expect("{{y:?}");
x.expect("{y:...}");
let _ = "fn main {\n\
}";
// Unicode characters escape should not lint either.
"\u{0052}".to_string();
}

View file

@ -0,0 +1,65 @@
error: these look like formatting arguments but are not part of a formatting macro
--> tests/ui/literal_string_with_formatting_arg.rs:7:15
|
LL | x.expect("{y} {}");
| ^^^^^^
|
= note: `-D clippy::literal-string-with-formatting-arg` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::literal_string_with_formatting_arg)]`
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: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:9: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:10: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:11: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:12: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:13: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:14: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:15:17
|
LL | "\\.+*?()|[]{}^$#&-~".chars().any(|x| x == 'a');
| ^^
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
|
LL | x.expect("———{}");
| ^^
error: aborting due to 10 previous errors