Disable checks on {}

This commit is contained in:
Guillaume Gomez 2024-09-19 17:39:28 +02:00
parent 8d5a4e95e3
commit 9ea769a479
3 changed files with 49 additions and 38 deletions

View file

@ -70,31 +70,40 @@ impl EarlyLintPass for LiteralStringWithFormattingArg {
continue;
}
let mut end = fmt_str[pos.end..].find('}').map_or(pos.end, |found| found + pos.end);
if fmt_str[start..end].contains(':') {
end += 1;
if fmt_str[start + 1..].trim_start().starts_with('}') {
// For now, we ignore `{}`.
continue;
}
let end = fmt_str[start + 1..]
.find('}')
.map_or(pos.end, |found| start + 1 + found)
+ 1;
spans.push(
expr.span
.with_hi(lo + BytePos((start + add) as _))
.with_lo(lo + BytePos((end + add) as _)),
.with_hi(lo + BytePos((start + add).try_into().unwrap()))
.with_lo(lo + BytePos((end + add).try_into().unwrap())),
);
}
}
if spans.len() == 1 {
span_lint(
cx,
LITERAL_STRING_WITH_FORMATTING_ARG,
spans,
"this looks like a formatting argument but it is not part of a formatting macro",
);
} else if spans.len() > 1 {
span_lint(
cx,
LITERAL_STRING_WITH_FORMATTING_ARG,
spans,
"these look like formatting arguments but are not part of a formatting macro",
);
match spans.len() {
0 => {},
1 => {
span_lint(
cx,
LITERAL_STRING_WITH_FORMATTING_ARG,
spans,
"this looks like a formatting argument but it is not part of a formatting macro",
);
},
_ => {
span_lint(
cx,
LITERAL_STRING_WITH_FORMATTING_ARG,
spans,
"these look like formatting arguments but are not part of a formatting macro",
);
},
}
}
}

View file

@ -5,6 +5,7 @@ fn main() {
let x: Option<usize> = None;
let y = "hello";
x.expect("{y} {}"); //~ literal_string_with_formatting_arg
x.expect(" {y} bla"); //~ 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
@ -12,13 +13,14 @@ fn main() {
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
x.expect("———{:?}"); //~ literal_string_with_formatting_arg
// Should not lint!
format!("{y:?}");
println!("{y:?}");
x.expect(" {} "); // For now we ignore `{}` to limit false positives.
x.expect(" { } "); // For now we ignore `{}` to limit false positives.
x.expect("{{y} {x");
x.expect("{{y:?}");
x.expect("{y:...}");

View file

@ -1,65 +1,65 @@
error: these look like formatting arguments but are not part of a formatting macro
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
|
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
--> tests/ui/literal_string_with_formatting_arg.rs:8: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
|
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
--> tests/ui/literal_string_with_formatting_arg.rs:10: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
--> tests/ui/literal_string_with_formatting_arg.rs:11: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
--> tests/ui/literal_string_with_formatting_arg.rs:12: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
--> tests/ui/literal_string_with_formatting_arg.rs:13: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
--> tests/ui/literal_string_with_formatting_arg.rs:14: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
--> tests/ui/literal_string_with_formatting_arg.rs:15: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("———{}");
| ^^
LL | x.expect("———{:?}");
| ^^^^
error: aborting due to 10 previous errors