Skip to content

Commit b4f448a

Browse files
committed
non_fmt_panic: machine app. suggestion for assert with string msg.
1 parent 04c9901 commit b4f448a

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

compiler/rustc_lint/src/non_fmt_panic.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,26 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
120120
);
121121
}
122122
} else {
123+
let ty = cx.typeck_results().expr_ty(arg);
124+
// If this is a &str or String, we can confidently give the `"{}", ` suggestion.
125+
let is_str = matches!(
126+
ty.kind(),
127+
ty::Ref(_, r, _) if *r.kind() == ty::Str
128+
) || matches!(
129+
(ty.ty_adt_def(), cx.tcx.get_diagnostic_item(sym::string_type)),
130+
(Some(ty_def), Some(string_type)) if ty_def.did == string_type
131+
);
123132
l.span_suggestion_verbose(
124133
arg_span.shrink_to_lo(),
125134
"add a \"{}\" format string to Display the message",
126135
"\"{}\", ".into(),
127-
Applicability::MaybeIncorrect,
136+
if is_str {
137+
Applicability::MachineApplicable
138+
} else {
139+
Applicability::MaybeIncorrect
140+
},
128141
);
129-
if panic == sym::std_panic_macro {
142+
if !is_str && panic == sym::std_panic_macro {
130143
if let Some((open, close, del)) = find_delimiters(cx, span) {
131144
l.multipart_suggestion(
132145
"or use std::panic::panic_any instead",

0 commit comments

Comments
 (0)