Skip to content

Correctly handle unescape warnings #9406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion clippy_dev/src/update_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,11 @@ fn remove_line_splices(s: &str) -> String {
.and_then(|s| s.strip_suffix('"'))
.unwrap_or_else(|| panic!("expected quoted string, found `{}`", s));
let mut res = String::with_capacity(s.len());
unescape::unescape_literal(s, unescape::Mode::Str, &mut |range, _| res.push_str(&s[range]));
unescape::unescape_literal(s, unescape::Mode::Str, &mut |range, ch| {
if ch.is_ok() {
res.push_str(&s[range]);
}
});
res
}

Expand Down
6 changes: 5 additions & 1 deletion clippy_lints/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,11 @@ fn check_newlines(fmtstr: &StrLit) -> bool {
let contents = fmtstr.symbol.as_str();

let mut cb = |r: Range<usize>, c: Result<char, EscapeError>| {
let c = c.unwrap();
let c = match c {
Ok(c) => c,
Err(e) if !e.is_fatal() => return,
Err(e) => panic!("{:?}", e),
};

if r.end == contents.len() && c == '\n' && !last_was_cr && !has_internal_newline {
should_lint = true;
Expand Down
6 changes: 4 additions & 2 deletions clippy_utils/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,10 @@ impl FormatString {
};

let mut unescaped = String::with_capacity(inner.len());
unescape_literal(inner, mode, &mut |_, ch| {
unescaped.push(ch.unwrap());
unescape_literal(inner, mode, &mut |_, ch| match ch {
Ok(ch) => unescaped.push(ch),
Err(e) if !e.is_fatal() => (),
Err(e) => panic!("{:?}", e),
});

let mut parts = Vec::new();
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/crashes/ice-9405.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![warn(clippy::useless_format)]
#![allow(clippy::print_literal)]

fn main() {
println!(
"\

{}",
"multiple skipped lines"
);
}
11 changes: 11 additions & 0 deletions tests/ui/crashes/ice-9405.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
warning: multiple lines skipped by escaped newline
--> $DIR/ice-9405.rs:6:10
|
LL | "/
| __________^
LL | |
LL | | {}",
| |____________^ skipping everything up to and including this point

warning: 1 warning emitted