about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMara Bos <m-ou.se@m-ou.se>2021-08-12 19:14:54 +0200
committerMara Bos <m-ou.se@m-ou.se>2021-08-13 11:50:32 +0200
commitb4f448a7ea47049ad6392afc3684e17372cb4469 (patch)
tree0493a6f7fb8bd633f5c1473772553cd2fe8c9867
parent04c9901a0838d20e6ac0bcda94ea1a8c239bb0d7 (diff)
downloadrust-b4f448a7ea47049ad6392afc3684e17372cb4469.tar.gz
rust-b4f448a7ea47049ad6392afc3684e17372cb4469.zip
non_fmt_panic: machine app. suggestion for assert with string msg.
-rw-r--r--compiler/rustc_lint/src/non_fmt_panic.rs17
1 files changed, 15 insertions, 2 deletions
diff --git a/compiler/rustc_lint/src/non_fmt_panic.rs b/compiler/rustc_lint/src/non_fmt_panic.rs
index ec53625f105..23c4bc7d608 100644
--- a/compiler/rustc_lint/src/non_fmt_panic.rs
+++ b/compiler/rustc_lint/src/non_fmt_panic.rs
@@ -120,13 +120,26 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
                 );
             }
         } 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(
                 arg_span.shrink_to_lo(),
                 "add a \"{}\" format string to Display the message",
                 "\"{}\", ".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) {
                     l.multipart_suggestion(
                         "or use std::panic::panic_any instead",