Skip to content

Commit 77c9ddd

Browse files
authored
Correctly handle string indices in literal_string_with_formatting_arg (#13841)
Fixes #13838. r? @klensy changelog: Correctly handle string indices in `literal_string_with_formatting_arg`
2 parents 063c5c1 + ad695da commit 77c9ddd

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

clippy_lints/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![feature(iter_partition_in_place)]
1010
#![feature(let_chains)]
1111
#![feature(never_type)]
12+
#![feature(round_char_boundary)]
1213
#![feature(rustc_private)]
1314
#![feature(stmt_expr_attributes)]
1415
#![feature(unwrap_infallible)]

clippy_lints/src/literal_string_with_formatting_args.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,15 @@ impl LateLintPass<'_> for LiteralStringWithFormattingArg {
108108
if error.span.end >= current.len() {
109109
break;
110110
}
111-
current = &current[error.span.end + 1..];
111+
// We find the closest char to where the error location ends.
112+
let pos = current.floor_char_boundary(error.span.end);
113+
// We get the next character.
114+
current = if let Some((next_char_pos, _)) = current[pos..].char_indices().nth(1) {
115+
// We make the parser start from this new location.
116+
&current[pos + next_char_pos..]
117+
} else {
118+
break;
119+
};
112120
diff_len = fmt_str.len() - current.len();
113121
parser = Parser::new(current, None, None, false, ParseMode::Format);
114122
} else if let Piece::NextArgument(arg) = piece {

tests/ui/literal_string_with_formatting_arg.rs

+4
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ fn main() {
3030
}";
3131
// Unicode characters escape should not lint either.
3232
"\u{0052}".to_string();
33+
34+
// Regression test for <https://github.com/rust-lang/rust-clippy/issues/13838>.
35+
let x: Option<usize> = Some(0);
36+
x.expect("{…}");
3337
}

0 commit comments

Comments
 (0)