Skip to content

Commit

Permalink
Correctly handle string indices in `literal_string_with_formatting_ar…
Browse files Browse the repository at this point in the history
…g` (#13841)

Fixes #13838.

r? @klensy

changelog: Correctly handle string indices in
`literal_string_with_formatting_arg`
  • Loading branch information
flip1995 authored Dec 16, 2024
2 parents 063c5c1 + ad695da commit 77c9ddd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#![feature(iter_partition_in_place)]
#![feature(let_chains)]
#![feature(never_type)]
#![feature(round_char_boundary)]
#![feature(rustc_private)]
#![feature(stmt_expr_attributes)]
#![feature(unwrap_infallible)]
Expand Down
10 changes: 9 additions & 1 deletion clippy_lints/src/literal_string_with_formatting_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,15 @@ impl LateLintPass<'_> for LiteralStringWithFormattingArg {
if error.span.end >= current.len() {
break;
}
current = &current[error.span.end + 1..];
// We find the closest char to where the error location ends.
let pos = current.floor_char_boundary(error.span.end);
// We get the next character.
current = if let Some((next_char_pos, _)) = current[pos..].char_indices().nth(1) {
// We make the parser start from this new location.
&current[pos + next_char_pos..]
} else {
break;
};
diff_len = fmt_str.len() - current.len();
parser = Parser::new(current, None, None, false, ParseMode::Format);
} else if let Piece::NextArgument(arg) = piece {
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/literal_string_with_formatting_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ fn main() {
}";
// Unicode characters escape should not lint either.
"\u{0052}".to_string();

// Regression test for <https://github.com/rust-lang/rust-clippy/issues/13838>.
let x: Option<usize> = Some(0);
x.expect("{…}");
}

0 comments on commit 77c9ddd

Please sign in to comment.