non_fmt_panic: machine app. suggestion for assert with string msg.
This commit is contained in:
parent
04c9901a08
commit
b4f448a7ea
1 changed files with 15 additions and 2 deletions
|
@ -120,13 +120,26 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
let ty = cx.typeck_results().expr_ty(arg);
|
||||||
|
// If this is a &str or String, we can confidently give the `"{}", ` suggestion.
|
||||||
|
let is_str = matches!(
|
||||||
|
ty.kind(),
|
||||||
|
ty::Ref(_, r, _) if *r.kind() == ty::Str
|
||||||
|
) || matches!(
|
||||||
|
(ty.ty_adt_def(), cx.tcx.get_diagnostic_item(sym::string_type)),
|
||||||
|
(Some(ty_def), Some(string_type)) if ty_def.did == string_type
|
||||||
|
);
|
||||||
l.span_suggestion_verbose(
|
l.span_suggestion_verbose(
|
||||||
arg_span.shrink_to_lo(),
|
arg_span.shrink_to_lo(),
|
||||||
"add a \"{}\" format string to Display the message",
|
"add a \"{}\" format string to Display the message",
|
||||||
"\"{}\", ".into(),
|
"\"{}\", ".into(),
|
||||||
Applicability::MaybeIncorrect,
|
if is_str {
|
||||||
|
Applicability::MachineApplicable
|
||||||
|
} else {
|
||||||
|
Applicability::MaybeIncorrect
|
||||||
|
},
|
||||||
);
|
);
|
||||||
if panic == sym::std_panic_macro {
|
if !is_str && panic == sym::std_panic_macro {
|
||||||
if let Some((open, close, del)) = find_delimiters(cx, span) {
|
if let Some((open, close, del)) = find_delimiters(cx, span) {
|
||||||
l.multipart_suggestion(
|
l.multipart_suggestion(
|
||||||
"or use std::panic::panic_any instead",
|
"or use std::panic::panic_any instead",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue