]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/unit_hash.rs
Remove a span from hir::ExprKind::MethodCall
[rust.git] / clippy_lints / src / unit_hash.rs
1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::source::snippet;
3 use rustc_errors::Applicability;
4 use rustc_hir::{Expr, ExprKind};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
7 use rustc_span::sym;
8
9 declare_clippy_lint! {
10     /// ### What it does
11     /// Detects `().hash(_)`.
12     ///
13     /// ### Why is this bad?
14     /// Hashing a unit value doesn't do anything as the implementation of `Hash` for `()` is a no-op.
15     ///
16     /// ### Example
17     /// ```rust
18     /// # use std::hash::Hash;
19     /// # use std::collections::hash_map::DefaultHasher;
20     /// # enum Foo { Empty, WithValue(u8) }
21     /// # use Foo::*;
22     /// # let mut state = DefaultHasher::new();
23     /// # let my_enum = Foo::Empty;
24     /// match my_enum {
25     ///         Empty => ().hash(&mut state),
26     ///         WithValue(x) => x.hash(&mut state),
27     /// }
28     /// ```
29     /// Use instead:
30     /// ```rust
31     /// # use std::hash::Hash;
32     /// # use std::collections::hash_map::DefaultHasher;
33     /// # enum Foo { Empty, WithValue(u8) }
34     /// # use Foo::*;
35     /// # let mut state = DefaultHasher::new();
36     /// # let my_enum = Foo::Empty;
37     /// match my_enum {
38     ///         Empty => 0_u8.hash(&mut state),
39     ///         WithValue(x) => x.hash(&mut state),
40     /// }
41     /// ```
42     #[clippy::version = "1.58.0"]
43     pub UNIT_HASH,
44     correctness,
45     "hashing a unit value, which does nothing"
46 }
47 declare_lint_pass!(UnitHash => [UNIT_HASH]);
48
49 impl<'tcx> LateLintPass<'tcx> for UnitHash {
50     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
51         if_chain! {
52             if let ExprKind::MethodCall(name_ident, args, _) = &expr.kind;
53             if name_ident.ident.name == sym::hash;
54             if let [recv, state_param] = args;
55             if cx.typeck_results().expr_ty(recv).is_unit();
56             then {
57                 span_lint_and_then(
58                     cx,
59                     UNIT_HASH,
60                     expr.span,
61                     "this call to `hash` on the unit type will do nothing",
62                     |diag| {
63                         diag.span_suggestion(
64                             expr.span,
65                             "remove the call to `hash` or consider using",
66                             format!(
67                                 "0_u8.hash({})",
68                                 snippet(cx, state_param.span, ".."),
69                             ),
70                             Applicability::MaybeIncorrect,
71                         );
72                         diag.note("the implementation of `Hash` for `()` is a no-op");
73                     }
74                 );
75             }
76         }
77     }
78 }