Skip to content

Commit

Permalink
Disable checks on {}
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Sep 19, 2024
1 parent 9fd308f commit faea4f9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
11 changes: 8 additions & 3 deletions clippy_lints/src/literal_string_with_formatting_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,15 @@ 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 mut 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 _))
Expand Down
6 changes: 4 additions & 2 deletions tests/ui/literal_string_with_formatting_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ 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
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
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:...}");
Expand Down
34 changes: 17 additions & 17 deletions tests/ui/literal_string_with_formatting_arg.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit faea4f9

Please sign in to comment.