]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_lint/src/panic_fmt.rs
Rollup merge of #80159 - jyn514:array, r=m-ou-se
[rust.git] / compiler / rustc_lint / src / panic_fmt.rs
1 use crate::{LateContext, LateLintPass, LintContext};
2 use rustc_ast as ast;
3 use rustc_errors::{pluralize, Applicability};
4 use rustc_hir as hir;
5 use rustc_middle::ty;
6 use rustc_parse_format::{ParseMode, Parser, Piece};
7 use rustc_span::{sym, InnerSpan};
8
9 declare_lint! {
10     /// The `panic_fmt` lint detects `panic!("..")` with `{` or `}` in the string literal.
11     ///
12     /// ### Example
13     ///
14     /// ```rust,no_run
15     /// panic!("{}");
16     /// ```
17     ///
18     /// {{produces}}
19     ///
20     /// ### Explanation
21     ///
22     /// `panic!("{}")` panics with the message `"{}"`, as a `panic!()` invocation
23     /// with a single argument does not use `format_args!()`.
24     /// A future edition of Rust will interpret this string as format string,
25     /// which would break this.
26     PANIC_FMT,
27     Warn,
28     "detect braces in single-argument panic!() invocations",
29     report_in_external_macro
30 }
31
32 declare_lint_pass!(PanicFmt => [PANIC_FMT]);
33
34 impl<'tcx> LateLintPass<'tcx> for PanicFmt {
35     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
36         if let hir::ExprKind::Call(f, [arg]) = &expr.kind {
37             if let &ty::FnDef(def_id, _) = cx.typeck_results().expr_ty(f).kind() {
38                 if Some(def_id) == cx.tcx.lang_items().begin_panic_fn()
39                     || Some(def_id) == cx.tcx.lang_items().panic_fn()
40                 {
41                     check_panic(cx, f, arg);
42                 }
43             }
44         }
45     }
46 }
47
48 fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tcx hir::Expr<'tcx>) {
49     if let hir::ExprKind::Lit(lit) = &arg.kind {
50         if let ast::LitKind::Str(sym, _) = lit.node {
51             let mut expn = f.span.ctxt().outer_expn_data();
52             if let Some(id) = expn.macro_def_id {
53                 if cx.tcx.is_diagnostic_item(sym::std_panic_macro, id)
54                     || cx.tcx.is_diagnostic_item(sym::core_panic_macro, id)
55                 {
56                     let fmt = sym.as_str();
57                     if !fmt.contains(&['{', '}'][..]) {
58                         return;
59                     }
60
61                     let fmt_span = arg.span.source_callsite();
62
63                     let (snippet, style) =
64                         match cx.sess().parse_sess.source_map().span_to_snippet(fmt_span) {
65                             Ok(snippet) => {
66                                 // Count the number of `#`s between the `r` and `"`.
67                                 let style = snippet.strip_prefix('r').and_then(|s| s.find('"'));
68                                 (Some(snippet), style)
69                             }
70                             Err(_) => (None, None),
71                         };
72
73                     let mut fmt_parser =
74                         Parser::new(fmt.as_ref(), style, snippet.clone(), false, ParseMode::Format);
75                     let n_arguments =
76                         (&mut fmt_parser).filter(|a| matches!(a, Piece::NextArgument(_))).count();
77
78                     // Unwrap another level of macro expansion if this panic!()
79                     // was expanded from assert!() or debug_assert!().
80                     for &assert in &[sym::assert_macro, sym::debug_assert_macro] {
81                         let parent = expn.call_site.ctxt().outer_expn_data();
82                         if parent
83                             .macro_def_id
84                             .map_or(false, |id| cx.tcx.is_diagnostic_item(assert, id))
85                         {
86                             expn = parent;
87                         }
88                     }
89
90                     if n_arguments > 0 && fmt_parser.errors.is_empty() {
91                         let arg_spans: Vec<_> = match &fmt_parser.arg_places[..] {
92                             [] => vec![fmt_span],
93                             v => v.iter().map(|span| fmt_span.from_inner(*span)).collect(),
94                         };
95                         cx.struct_span_lint(PANIC_FMT, arg_spans, |lint| {
96                             let mut l = lint.build(match n_arguments {
97                                 1 => "panic message contains an unused formatting placeholder",
98                                 _ => "panic message contains unused formatting placeholders",
99                             });
100                             l.note("this message is not used as a format string when given without arguments, but will be in a future Rust edition");
101                             if expn.call_site.contains(arg.span) {
102                                 l.span_suggestion(
103                                     arg.span.shrink_to_hi(),
104                                     &format!("add the missing argument{}", pluralize!(n_arguments)),
105                                     ", ...".into(),
106                                     Applicability::HasPlaceholders,
107                                 );
108                                 l.span_suggestion(
109                                     arg.span.shrink_to_lo(),
110                                     "or add a \"{}\" format string to use the message literally",
111                                     "\"{}\", ".into(),
112                                     Applicability::MachineApplicable,
113                                 );
114                             }
115                             l.emit();
116                         });
117                     } else {
118                         let brace_spans: Option<Vec<_>> = snippet
119                             .filter(|s| s.starts_with('"') || s.starts_with("r#"))
120                             .map(|s| {
121                                 s.char_indices()
122                                     .filter(|&(_, c)| c == '{' || c == '}')
123                                     .map(|(i, _)| {
124                                         fmt_span.from_inner(InnerSpan { start: i, end: i + 1 })
125                                     })
126                                     .collect()
127                             });
128                         let msg = match &brace_spans {
129                             Some(v) if v.len() == 1 => "panic message contains a brace",
130                             _ => "panic message contains braces",
131                         };
132                         cx.struct_span_lint(PANIC_FMT, brace_spans.unwrap_or(vec![expn.call_site]), |lint| {
133                             let mut l = lint.build(msg);
134                             l.note("this message is not used as a format string, but will be in a future Rust edition");
135                             if expn.call_site.contains(arg.span) {
136                                 l.span_suggestion(
137                                     arg.span.shrink_to_lo(),
138                                     "add a \"{}\" format string to use the message literally",
139                                     "\"{}\", ".into(),
140                                     Applicability::MachineApplicable,
141                                 );
142                             }
143                             l.emit();
144                         });
145                     }
146                 }
147             }
148         }
149     }
150 }