]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/mem_discriminant.rs
Auto merge of #83776 - jyn514:update-stdarch-docs, r=Amanieu
[rust.git] / src / tools / clippy / clippy_lints / src / mem_discriminant.rs
1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::source::snippet;
3 use clippy_utils::ty::walk_ptrs_ty_depth;
4 use clippy_utils::{match_def_path, paths};
5 use if_chain::if_chain;
6 use rustc_errors::Applicability;
7 use rustc_hir::{BorrowKind, Expr, ExprKind};
8 use rustc_lint::{LateContext, LateLintPass};
9 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 use std::iter;
11
12 declare_clippy_lint! {
13     /// **What it does:** Checks for calls of `mem::discriminant()` on a non-enum type.
14     ///
15     /// **Why is this bad?** The value of `mem::discriminant()` on non-enum types
16     /// is unspecified.
17     ///
18     /// **Known problems:** None.
19     ///
20     /// **Example:**
21     /// ```rust
22     /// use std::mem;
23     ///
24     /// mem::discriminant(&"hello");
25     /// mem::discriminant(&&Some(2));
26     /// ```
27     pub MEM_DISCRIMINANT_NON_ENUM,
28     correctness,
29     "calling `mem::descriminant` on non-enum type"
30 }
31
32 declare_lint_pass!(MemDiscriminant => [MEM_DISCRIMINANT_NON_ENUM]);
33
34 impl<'tcx> LateLintPass<'tcx> for MemDiscriminant {
35     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
36         if_chain! {
37             if let ExprKind::Call(func, func_args) = expr.kind;
38             // is `mem::discriminant`
39             if let ExprKind::Path(ref func_qpath) = func.kind;
40             if let Some(def_id) = cx.qpath_res(func_qpath, func.hir_id).opt_def_id();
41             if match_def_path(cx, def_id, &paths::MEM_DISCRIMINANT);
42             // type is non-enum
43             let ty_param = cx.typeck_results().node_substs(func.hir_id).type_at(0);
44             if !ty_param.is_enum();
45
46             then {
47                 span_lint_and_then(
48                     cx,
49                     MEM_DISCRIMINANT_NON_ENUM,
50                     expr.span,
51                     &format!("calling `mem::discriminant` on non-enum type `{}`", ty_param),
52                     |diag| {
53                         // if this is a reference to an enum, suggest dereferencing
54                         let (base_ty, ptr_depth) = walk_ptrs_ty_depth(ty_param);
55                         if ptr_depth >= 1 && base_ty.is_enum() {
56                             let param = &func_args[0];
57
58                             // cancel out '&'s first
59                             let mut derefs_needed = ptr_depth;
60                             let mut cur_expr = param;
61                             while derefs_needed > 0  {
62                                 if let ExprKind::AddrOf(BorrowKind::Ref, _, inner_expr) = cur_expr.kind {
63                                     derefs_needed -= 1;
64                                     cur_expr = inner_expr;
65                                 } else {
66                                     break;
67                                 }
68                             }
69
70                             let derefs: String = iter::repeat('*').take(derefs_needed).collect();
71                             diag.span_suggestion(
72                                 param.span,
73                                 "try dereferencing",
74                                 format!("{}{}", derefs, snippet(cx, cur_expr.span, "<param>")),
75                                 Applicability::MachineApplicable,
76                             );
77                         }
78                     },
79                 )
80             }
81         }
82     }
83 }