@@ -78,6 +78,7 @@ mod string_extend_chars;
78
78
mod suspicious_map;
79
79
mod suspicious_splitn;
80
80
mod uninit_assumed_init;
81
+ mod unit_hash;
81
82
mod unnecessary_filter_map;
82
83
mod unnecessary_fold;
83
84
mod unnecessary_iter_cloned;
@@ -2832,6 +2833,45 @@ declare_clippy_lint! {
2832
2833
"use of sort() when sort_unstable() is equivalent"
2833
2834
}
2834
2835
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
+
2835
2875
pub struct Methods {
2836
2876
avoid_breaking_exported_api : bool ,
2837
2877
msrv : Option < RustcVersion > ,
@@ -2949,6 +2989,7 @@ impl_lint_pass!(Methods => [
2949
2989
RANGE_ZIP_WITH_LEN ,
2950
2990
REPEAT_ONCE ,
2951
2991
STABLE_SORT_PRIMITIVE ,
2992
+ UNIT_HASH ,
2952
2993
] ) ;
2953
2994
2954
2995
/// Extracts a method call name, args, and `Span` of the method name.
@@ -3258,6 +3299,9 @@ impl Methods {
3258
3299
get_last_with_len:: check ( cx, expr, recv, arg) ;
3259
3300
} ,
3260
3301
( "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
+ } ,
3261
3305
( "is_file" , [ ] ) => filetype_is_file:: check ( cx, expr, recv) ,
3262
3306
( "is_digit" , [ radix] ) => is_digit_ascii_radix:: check ( cx, expr, recv, radix, self . msrv ) ,
3263
3307
( "is_none" , [ ] ) => check_is_some_is_none ( cx, expr, recv, false ) ,
0 commit comments