@@ -78,6 +78,7 @@ mod string_extend_chars;
7878mod suspicious_map;
7979mod suspicious_splitn;
8080mod uninit_assumed_init;
81+ mod unit_hash;
8182mod unnecessary_filter_map;
8283mod unnecessary_fold;
8384mod unnecessary_iter_cloned;
@@ -2832,6 +2833,45 @@ declare_clippy_lint! {
28322833 "use of sort() when sort_unstable() is equivalent"
28332834}
28342835
2836+ declare_clippy_lint ! {
2837+ /// ### What it does
2838+ /// Detects `().hash(_)`.
2839+ ///
2840+ /// ### Why is this bad?
2841+ /// Hashing a unit value doesn't do anything as the implementation of `Hash` for `()` is a no-op.
2842+ ///
2843+ /// ### Example
2844+ /// ```rust
2845+ /// # use std::hash::Hash;
2846+ /// # use std::collections::hash_map::DefaultHasher;
2847+ /// # enum Foo { Empty, WithValue(u8) }
2848+ /// # use Foo::*;
2849+ /// # let mut state = DefaultHasher::new();
2850+ /// # let my_enum = Foo::Empty;
2851+ /// match my_enum {
2852+ /// Empty => ().hash(&mut state),
2853+ /// WithValue(x) => x.hash(&mut state),
2854+ /// }
2855+ /// ```
2856+ /// Use instead:
2857+ /// ```rust
2858+ /// # use std::hash::Hash;
2859+ /// # use std::collections::hash_map::DefaultHasher;
2860+ /// # enum Foo { Empty, WithValue(u8) }
2861+ /// # use Foo::*;
2862+ /// # let mut state = DefaultHasher::new();
2863+ /// # let my_enum = Foo::Empty;
2864+ /// match my_enum {
2865+ /// Empty => 0_u8.hash(&mut state),
2866+ /// WithValue(x) => x.hash(&mut state),
2867+ /// }
2868+ /// ```
2869+ #[ clippy:: version = "1.58.0" ]
2870+ pub UNIT_HASH ,
2871+ correctness,
2872+ "hashing a unit value, which does nothing"
2873+ }
2874+
28352875pub struct Methods {
28362876 avoid_breaking_exported_api : bool ,
28372877 msrv : Option < RustcVersion > ,
@@ -2949,6 +2989,7 @@ impl_lint_pass!(Methods => [
29492989 RANGE_ZIP_WITH_LEN ,
29502990 REPEAT_ONCE ,
29512991 STABLE_SORT_PRIMITIVE ,
2992+ UNIT_HASH ,
29522993] ) ;
29532994
29542995/// Extracts a method call name, args, and `Span` of the method name.
@@ -3258,6 +3299,9 @@ impl Methods {
32583299 get_last_with_len:: check ( cx, expr, recv, arg) ;
32593300 } ,
32603301 ( "get_or_insert_with" , [ arg] ) => unnecessary_lazy_eval:: check ( cx, expr, recv, arg, "get_or_insert" ) ,
3302+ ( "hash" , [ arg] ) => {
3303+ unit_hash:: check ( cx, expr, recv, arg) ;
3304+ } ,
32613305 ( "is_file" , [ ] ) => filetype_is_file:: check ( cx, expr, recv) ,
32623306 ( "is_digit" , [ radix] ) => is_digit_ascii_radix:: check ( cx, expr, recv, radix, self . msrv ) ,
32633307 ( "is_none" , [ ] ) => check_is_some_is_none ( cx, expr, recv, false ) ,
0 commit comments