Skip to content

Commit f4c6ade

Browse files
committed
Do not panic when analyzing the malformed origin of a format string
1 parent b528cc9 commit f4c6ade

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

clippy_utils/src/macros.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,18 @@ impl FormatString {
391391
};
392392

393393
let mut unescaped = String::with_capacity(inner.len());
394+
// Sometimes the original string comes from a macro which accepts a malformed string, such as in a
395+
// #[display(""somestring)] attribute (accepted by the `displaythis` crate). Reconstructing the
396+
// string from the span will not be possible, so we will just return None here.
397+
let mut unparsable = false;
394398
unescape_literal(inner, mode, &mut |_, ch| match ch {
395399
Ok(ch) => unescaped.push(ch),
396400
Err(e) if !e.is_fatal() => (),
397-
Err(e) => panic!("{e:?}"),
401+
Err(_) => unparsable = true,
398402
});
403+
if unparsable {
404+
return None;
405+
}
399406

400407
let mut parts = Vec::new();
401408
let _: Option<!> = for_each_expr(pieces, |expr| {

0 commit comments

Comments
 (0)