summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/impl_hash_with_borrow_str_and_bytes.rs
blob: f6ce6153e01d14bee38ffb0a8c8991a8495969a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#![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<H: Hasher>(&self, state: &mut H) {
        self.data.hash(state);
    }
}

impl Borrow<str> 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<H: Hasher>(&self, state: &mut H) {
        todo!();
    }
}

struct ShouldNotRaiseForBorrow {}
impl Borrow<str> 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<H: Hasher>(&self, state: &mut H) {
        todo!();
    }
}
impl Borrow<str> for ShouldNotRaiseForHashBorrowStr {
    fn borrow(&self) -> &str {
        todo!();
    }
}

struct ShouldNotRaiseForHashBorrowSlice {}
impl Hash for ShouldNotRaiseForHashBorrowSlice {
    fn hash<H: Hasher>(&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<str> 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<T> {
    data: T,
}

impl<T: Hash> Hash for GenericExampleType<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.data.hash(state);
    }
}

impl Borrow<str> for GenericExampleType<String> {
    fn borrow(&self) -> &str {
        &self.data
    }
}

impl Borrow<[u8]> for GenericExampleType<&'static [u8]> {
    fn borrow(&self) -> &[u8] {
        self.data
    }
}

struct GenericExampleType2<T> {
    data: T,
}

impl Hash for GenericExampleType2<String> {
    //~^ ERROR: can't
    // this is correctly throwing an error for generic with concrete impl
    // for all 3 types
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.data.hash(state);
    }
}

impl Borrow<str> for GenericExampleType2<String> {
    fn borrow(&self) -> &str {
        &self.data
    }
}

impl Borrow<[u8]> for GenericExampleType2<String> {
    fn borrow(&self) -> &[u8] {
        self.data.as_bytes()
    }
}