]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/panic_unimplemented.rs
Auto merge of #3511 - phansch:remove_allow_doc_markdown, r=phansch
[rust.git] / clippy_lints / src / panic_unimplemented.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 use crate::rustc::hir::*;
11 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
12 use crate::rustc::{declare_tool_lint, lint_array};
13 use crate::syntax::ast::LitKind;
14 use crate::syntax::ext::quote::rt::Span;
15 use crate::syntax::ptr::P;
16 use crate::utils::{is_direct_expn_of, is_expn_of, match_def_path, opt_def_id, paths, resolve_node, span_lint};
17 use if_chain::if_chain;
18
19 /// **What it does:** Checks for missing parameters in `panic!`.
20 ///
21 /// **Why is this bad?** Contrary to the `format!` family of macros, there are
22 /// two forms of `panic!`: if there are no parameters given, the first argument
23 /// is not a format string and used literally. So while `format!("{}")` will
24 /// fail to compile, `panic!("{}")` will not.
25 ///
26 /// **Known problems:** None.
27 ///
28 /// **Example:**
29 /// ```rust
30 /// panic!("This `panic!` is probably missing a parameter there: {}");
31 /// ```
32 declare_clippy_lint! {
33     pub PANIC_PARAMS,
34     style,
35     "missing parameters in `panic!` calls"
36 }
37
38 /// **What it does:** Checks for usage of `unimplemented!`.
39 ///
40 /// **Why is this bad?** This macro should not be present in production code
41 ///
42 /// **Known problems:** None.
43 ///
44 /// **Example:**
45 /// ```rust
46 /// unimplemented!();
47 /// ```
48 declare_clippy_lint! {
49     pub UNIMPLEMENTED,
50     restriction,
51     "`unimplemented!` should not be present in production code"
52 }
53
54 pub struct Pass;
55
56 impl LintPass for Pass {
57     fn get_lints(&self) -> LintArray {
58         lint_array!(PANIC_PARAMS, UNIMPLEMENTED)
59     }
60 }
61
62 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
63     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
64         if_chain! {
65             if let ExprKind::Block(ref block, _) = expr.node;
66             if let Some(ref ex) = block.expr;
67             if let ExprKind::Call(ref fun, ref params) = ex.node;
68             if let ExprKind::Path(ref qpath) = fun.node;
69             if let Some(fun_def_id) = opt_def_id(resolve_node(cx, qpath, fun.hir_id));
70             if match_def_path(cx.tcx, fun_def_id, &paths::BEGIN_PANIC);
71             if params.len() == 2;
72             then {
73                 if is_expn_of(expr.span, "unimplemented").is_some() {
74                     let span = get_outer_span(expr);
75                     span_lint(cx, UNIMPLEMENTED, span,
76                               "`unimplemented` should not be present in production code");
77                 } else {
78                     match_panic(params, expr, cx);
79                 }
80             }
81         }
82     }
83 }
84
85 fn get_outer_span(expr: &Expr) -> Span {
86     if_chain! {
87         if let Some(first) = expr.span.ctxt().outer().expn_info();
88         if let Some(second) = first.call_site.ctxt().outer().expn_info();
89         then {
90             second.call_site
91         } else {
92             expr.span
93         }
94     }
95 }
96
97 fn match_panic(params: &P<[Expr]>, expr: &Expr, cx: &LateContext<'_, '_>) {
98     if_chain! {
99         if let ExprKind::Lit(ref lit) = params[0].node;
100         if is_direct_expn_of(expr.span, "panic").is_some();
101         if let LitKind::Str(ref string, _) = lit.node;
102         let string = string.as_str().replace("{{", "").replace("}}", "");
103         if let Some(par) = string.find('{');
104         if string[par..].contains('}');
105         if params[0].span.source_callee().is_none();
106         if params[0].span.lo() != params[0].span.hi();
107         then {
108             span_lint(cx, PANIC_PARAMS, params[0].span,
109                       "you probably are missing some parameter in your format string");
110         }
111     }
112 }