]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mem_discriminant.rs
rustup https://github.com/rust-lang/rust/pull/57726
[rust.git] / clippy_lints / src / mem_discriminant.rs
1 use crate::utils::{match_def_path, opt_def_id, paths, snippet, span_lint_and_then, walk_ptrs_ty_depth};
2 use if_chain::if_chain;
3 use rustc::hir::{Expr, ExprKind};
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::{declare_tool_lint, lint_array};
6 use rustc_errors::Applicability;
7
8 use std::iter;
9
10 /// **What it does:** Checks for calls of `mem::discriminant()` on a non-enum type.
11 ///
12 /// **Why is this bad?** The value of `mem::discriminant()` on non-enum types
13 /// is unspecified.
14 ///
15 /// **Known problems:** None.
16 ///
17 /// **Example:**
18 /// ```rust
19 /// mem::discriminant(&"hello");
20 /// mem::discriminant(&&Some(2));
21 /// ```
22 declare_clippy_lint! {
23     pub MEM_DISCRIMINANT_NON_ENUM,
24     correctness,
25     "calling mem::descriminant on non-enum type"
26 }
27
28 pub struct MemDiscriminant;
29
30 impl LintPass for MemDiscriminant {
31     fn get_lints(&self) -> LintArray {
32         lint_array![MEM_DISCRIMINANT_NON_ENUM]
33     }
34
35     fn name(&self) -> &'static str {
36         "MemDiscriminant"
37     }
38 }
39
40 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MemDiscriminant {
41     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
42         if_chain! {
43             if let ExprKind::Call(ref func, ref func_args) = expr.node;
44             // is `mem::discriminant`
45             if let ExprKind::Path(ref func_qpath) = func.node;
46             if let Some(def_id) = opt_def_id(cx.tables.qpath_def(func_qpath, func.hir_id));
47             if match_def_path(cx.tcx, def_id, &paths::MEM_DISCRIMINANT);
48             // type is non-enum
49             let ty_param = cx.tables.node_substs(func.hir_id).type_at(0);
50             if !ty_param.is_enum();
51
52             then {
53                 span_lint_and_then(
54                     cx,
55                     MEM_DISCRIMINANT_NON_ENUM,
56                     expr.span,
57                     &format!("calling `mem::discriminant` on non-enum type `{}`", ty_param),
58                     |db| {
59                         // if this is a reference to an enum, suggest dereferencing
60                         let (base_ty, ptr_depth) = walk_ptrs_ty_depth(ty_param);
61                         if ptr_depth >= 1 && base_ty.is_enum() {
62                             let param = &func_args[0];
63
64                             // cancel out '&'s first
65                             let mut derefs_needed = ptr_depth;
66                             let mut cur_expr = param;
67                             while derefs_needed > 0  {
68                                 if let ExprKind::AddrOf(_, ref inner_expr) = cur_expr.node {
69                                     derefs_needed -= 1;
70                                     cur_expr = inner_expr;
71                                 } else {
72                                     break;
73                                 }
74                             }
75
76                             let derefs: String = iter::repeat('*').take(derefs_needed).collect();
77                             db.span_suggestion_with_applicability(
78                                 param.span,
79                                 "try dereferencing",
80                                 format!("{}{}", derefs, snippet(cx, cur_expr.span, "<param>")),
81                                 Applicability::MachineApplicable,
82                             );
83                         }
84                     },
85                 )
86             }
87         }
88     }
89 }