about summary refs log tree commit diff
path: root/compiler/rustc_lint/src
diff options
context:
space:
mode:
authorMara Bos <m-ou.se@m-ou.se>2020-10-20 22:25:42 +0200
committerMara Bos <m-ou.se@m-ou.se>2020-10-20 22:25:42 +0200
commit6b44662669cf1680fe097e593eae20ca5dbed2ee (patch)
tree7adf9caad63fe6c221ee6e97ff9adf9b3ce3a929 /compiler/rustc_lint/src
parent0f193d1a62c128ae94e1f21d7c1212d7c9e95b7d (diff)
Parse the format string for the panic_fmt lint for better warnings.
Diffstat (limited to 'compiler/rustc_lint/src')
-rw-r--r--compiler/rustc_lint/src/panic_fmt.rs42
1 files changed, 33 insertions, 9 deletions
diff --git a/compiler/rustc_lint/src/panic_fmt.rs b/compiler/rustc_lint/src/panic_fmt.rs
index 288e1d61bbf..cff50ff9912 100644
--- a/compiler/rustc_lint/src/panic_fmt.rs
+++ b/compiler/rustc_lint/src/panic_fmt.rs
@@ -3,6 +3,7 @@ use rustc_ast as ast;
 use rustc_errors::Applicability;
 use rustc_hir as hir;
 use rustc_middle::ty;
+use rustc_parse_format::{ParseMode, Parser, Piece};
 use rustc_span::sym;
 
 declare_lint! {
@@ -52,13 +53,28 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
                 if cx.tcx.is_diagnostic_item(sym::std_panic_macro, id)
                     || cx.tcx.is_diagnostic_item(sym::core_panic_macro, id)
                 {
-                    let s = sym.as_str();
-                    if !s.contains(&['{', '}'][..]) {
+                    let fmt = sym.as_str();
+                    if !fmt.contains(&['{', '}'][..]) {
                         return;
                     }
-                    let s = s.replace("{{", "").replace("}}", "");
-                    let looks_like_placeholder =
-                        s.find('{').map_or(false, |i| s[i + 1..].contains('}'));
+
+                    let fmt_span = arg.span.source_callsite();
+
+                    let (snippet, style) =
+                        match cx.sess().parse_sess.source_map().span_to_snippet(fmt_span) {
+                            Ok(snippet) => {
+                                // Count the number of `#`s between the `r` and `"`.
+                                let style = snippet.strip_prefix('r').and_then(|s| s.find('"'));
+                                (Some(snippet), style)
+                            }
+                            Err(_) => (None, None),
+                        };
+
+                    let mut fmt_parser =
+                        Parser::new(fmt.as_ref(), style, snippet, false, ParseMode::Format);
+                    let n_arguments =
+                        (&mut fmt_parser).filter(|a| matches!(a, Piece::NextArgument(_))).count();
+
                     // Unwrap another level of macro expansion if this panic!()
                     // was expanded from assert!() or debug_assert!().
                     for &assert in &[sym::assert_macro, sym::debug_assert_macro] {
@@ -70,15 +86,23 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
                             expn = parent;
                         }
                     }
-                    if looks_like_placeholder {
-                        cx.struct_span_lint(PANIC_FMT, arg.span.source_callsite(), |lint| {
-                            let mut l = lint.build("panic message contains an unused formatting placeholder");
+
+                    if n_arguments > 0 && fmt_parser.errors.is_empty() {
+                        let arg_spans: Vec<_> = match &fmt_parser.arg_places[..] {
+                            [] => vec![fmt_span],
+                            v => v.iter().map(|span| fmt_span.from_inner(*span)).collect(),
+                        };
+                        cx.struct_span_lint(PANIC_FMT, arg_spans, |lint| {
+                            let mut l = lint.build(match n_arguments {
+                                1 => "panic message contains an unused formatting placeholder",
+                                _ => "panic message contains unused formatting placeholders",
+                            });
                             l.note("this message is not used as a format string when given without arguments, but will be in a future Rust version");
                             if expn.call_site.contains(arg.span) {
                                 l.span_suggestion(
                                     arg.span.shrink_to_hi(),
                                     "add the missing argument(s)",
-                                    ", argument".into(),
+                                    ", ...".into(),
                                     Applicability::HasPlaceholders,
                                 );
                                 l.span_suggestion(