#![warn(clippy::impl_hash_borrow_with_str_and_bytes)] use std::borrow::Borrow; use std::hash::{Hash, Hasher}; struct ExampleType { data: String, } impl Hash for ExampleType { //~^ ERROR: can't fn hash(&self, state: &mut H) { self.data.hash(state); } } impl Borrow for ExampleType { fn borrow(&self) -> &str { &self.data } } impl Borrow<[u8]> for ExampleType { fn borrow(&self) -> &[u8] { self.data.as_bytes() } } struct ShouldNotRaiseForHash {} impl Hash for ShouldNotRaiseForHash { fn hash(&self, state: &mut H) { todo!(); } } struct ShouldNotRaiseForBorrow {} impl Borrow for ShouldNotRaiseForBorrow { fn borrow(&self) -> &str { todo!(); } } impl Borrow<[u8]> for ShouldNotRaiseForBorrow { fn borrow(&self) -> &[u8] { todo!(); } } struct ShouldNotRaiseForHashBorrowStr {} impl Hash for ShouldNotRaiseForHashBorrowStr { fn hash(&self, state: &mut H) { todo!(); } } impl Borrow for ShouldNotRaiseForHashBorrowStr { fn borrow(&self) -> &str { todo!(); } } struct ShouldNotRaiseForHashBorrowSlice {} impl Hash for ShouldNotRaiseForHashBorrowSlice { fn hash(&self, state: &mut H) { todo!(); } } impl Borrow<[u8]> for ShouldNotRaiseForHashBorrowSlice { fn borrow(&self) -> &[u8] { todo!(); } } #[derive(Hash)] //~^ ERROR: can't struct Derived { data: String, } impl Borrow for Derived { fn borrow(&self) -> &str { self.data.as_str() } } impl Borrow<[u8]> for Derived { fn borrow(&self) -> &[u8] { self.data.as_bytes() } } struct GenericExampleType { data: T, } impl Hash for GenericExampleType { fn hash(&self, state: &mut H) { self.data.hash(state); } } impl Borrow for GenericExampleType { fn borrow(&self) -> &str { &self.data } } impl Borrow<[u8]> for GenericExampleType<&'static [u8]> { fn borrow(&self) -> &[u8] { self.data } } struct GenericExampleType2 { data: T, } impl Hash for GenericExampleType2 { //~^ ERROR: can't // this is correctly throwing an error for generic with concrete impl // for all 3 types fn hash(&self, state: &mut H) { self.data.hash(state); } } impl Borrow for GenericExampleType2 { fn borrow(&self) -> &str { &self.data } } impl Borrow<[u8]> for GenericExampleType2 { fn borrow(&self) -> &[u8] { self.data.as_bytes() } }