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