Skip to content

Commit 291f75e

Browse files
committed
Auto merge of #9406 - Jarcho:unescape_ice, r=Manishearth
Correctly handle unescape warnings fixes #9405 changelog: Fix ICE when format literals raise compiler warnings
2 parents c782767 + d4a0785 commit 291f75e

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

clippy_dev/src/update_lints.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,11 @@ fn remove_line_splices(s: &str) -> String {
977977
.and_then(|s| s.strip_suffix('"'))
978978
.unwrap_or_else(|| panic!("expected quoted string, found `{}`", s));
979979
let mut res = String::with_capacity(s.len());
980-
unescape::unescape_literal(s, unescape::Mode::Str, &mut |range, _| res.push_str(&s[range]));
980+
unescape::unescape_literal(s, unescape::Mode::Str, &mut |range, ch| {
981+
if ch.is_ok() {
982+
res.push_str(&s[range]);
983+
}
984+
});
981985
res
982986
}
983987

clippy_lints/src/write.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,11 @@ fn check_newlines(fmtstr: &StrLit) -> bool {
805805
let contents = fmtstr.symbol.as_str();
806806

807807
let mut cb = |r: Range<usize>, c: Result<char, EscapeError>| {
808-
let c = c.unwrap();
808+
let c = match c {
809+
Ok(c) => c,
810+
Err(e) if !e.is_fatal() => return,
811+
Err(e) => panic!("{:?}", e),
812+
};
809813

810814
if r.end == contents.len() && c == '\n' && !last_was_cr && !has_internal_newline {
811815
should_lint = true;

clippy_utils/src/macros.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,10 @@ impl FormatString {
389389
};
390390

391391
let mut unescaped = String::with_capacity(inner.len());
392-
unescape_literal(inner, mode, &mut |_, ch| {
393-
unescaped.push(ch.unwrap());
392+
unescape_literal(inner, mode, &mut |_, ch| match ch {
393+
Ok(ch) => unescaped.push(ch),
394+
Err(e) if !e.is_fatal() => (),
395+
Err(e) => panic!("{:?}", e),
394396
});
395397

396398
let mut parts = Vec::new();

tests/ui/crashes/ice-9405.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![warn(clippy::useless_format)]
2+
#![allow(clippy::print_literal)]
3+
4+
fn main() {
5+
println!(
6+
"\
7+
8+
{}",
9+
"multiple skipped lines"
10+
);
11+
}

tests/ui/crashes/ice-9405.stderr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
warning: multiple lines skipped by escaped newline
2+
--> $DIR/ice-9405.rs:6:10
3+
|
4+
LL | "/
5+
| __________^
6+
LL | |
7+
LL | | {}",
8+
| |____________^ skipping everything up to and including this point
9+
10+
warning: 1 warning emitted
11+

0 commit comments

Comments
 (0)