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