@@ -76,6 +76,11 @@ enum LintKind {
76
76
Big ,
77
77
}
78
78
79
+ enum Prefix {
80
+ From ,
81
+ To ,
82
+ }
83
+
79
84
impl LintKind {
80
85
fn allowed ( & self , cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> bool {
81
86
is_lint_allowed ( cx, self . as_lint ( ) , expr. hir_id )
@@ -89,29 +94,13 @@ impl LintKind {
89
94
}
90
95
}
91
96
92
- fn to_name ( & self , prefix : & str ) -> & str {
97
+ fn as_name ( & self , prefix : & Prefix ) -> & str {
98
+ let index = if matches ! ( prefix, Prefix :: From ) { 0 } else { 1 } ;
99
+
93
100
match self {
94
- LintKind :: Host => {
95
- if prefix == "from" {
96
- HOST_NAMES [ 0 ]
97
- } else {
98
- HOST_NAMES [ 1 ]
99
- }
100
- } ,
101
- LintKind :: Little => {
102
- if prefix == "from" {
103
- LITTLE_NAMES [ 0 ]
104
- } else {
105
- LITTLE_NAMES [ 1 ]
106
- }
107
- } ,
108
- LintKind :: Big => {
109
- if prefix == "from" {
110
- BIG_NAMES [ 0 ]
111
- } else {
112
- BIG_NAMES [ 1 ]
113
- }
114
- } ,
101
+ LintKind :: Host => HOST_NAMES [ index] ,
102
+ LintKind :: Little => LITTLE_NAMES [ index] ,
103
+ LintKind :: Big => BIG_NAMES [ index] ,
115
104
}
116
105
}
117
106
}
@@ -127,7 +116,7 @@ impl LateLintPass<'_> for EndianBytes {
127
116
if args. is_empty( ) ;
128
117
let ty = cx. typeck_results( ) . expr_ty( receiver) ;
129
118
if ty. is_primitive_ty( ) ;
130
- if maybe_lint_endian_bytes( cx, expr, "to" , method_name. ident. name, ty) ;
119
+ if maybe_lint_endian_bytes( cx, expr, & Prefix :: To , method_name. ident. name, ty) ;
131
120
then {
132
121
return ;
133
122
}
@@ -141,16 +130,16 @@ impl LateLintPass<'_> for EndianBytes {
141
130
let ty = cx. typeck_results( ) . expr_ty( expr) ;
142
131
if ty. is_primitive_ty( ) ;
143
132
then {
144
- maybe_lint_endian_bytes( cx, expr, "from" , * function_name, ty) ;
133
+ maybe_lint_endian_bytes( cx, expr, & Prefix :: From , * function_name, ty) ;
145
134
}
146
135
}
147
136
}
148
137
}
149
138
150
- fn maybe_lint_endian_bytes ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , prefix : & str , name : Symbol , ty : Ty < ' _ > ) -> bool {
151
- let ne = LintKind :: Host . to_name ( prefix) ;
152
- let le = LintKind :: Little . to_name ( prefix) ;
153
- let be = LintKind :: Big . to_name ( prefix) ;
139
+ fn maybe_lint_endian_bytes ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , prefix : & Prefix , name : Symbol , ty : Ty < ' _ > ) -> bool {
140
+ let ne = LintKind :: Host . as_name ( prefix) ;
141
+ let le = LintKind :: Little . as_name ( prefix) ;
142
+ let be = LintKind :: Big . as_name ( prefix) ;
154
143
155
144
let ( lint, other_lints) = match name. as_str ( ) {
156
145
name if name == ne => ( ( & LintKind :: Host ) , [ ( & LintKind :: Little ) , ( & LintKind :: Big ) ] ) ,
@@ -172,14 +161,14 @@ fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &str,
172
161
}
173
162
174
163
// ne_bytes and all other lints allowed
175
- if lint. to_name ( prefix) == ne && other_lints. iter ( ) . all ( |lint| lint. allowed ( cx, expr) ) {
164
+ if lint. as_name ( prefix) == ne && other_lints. iter ( ) . all ( |lint| lint. allowed ( cx, expr) ) {
176
165
help = Some ( Cow :: Borrowed ( "specify the desired endianness explicitly" ) ) ;
177
166
break ' build_help;
178
167
}
179
168
180
169
// le_bytes where ne_bytes allowed but be_bytes is not, or le_bytes where ne_bytes allowed but
181
170
// le_bytes is not
182
- if ( lint. to_name ( prefix) == le || lint. to_name ( prefix) == be) && LintKind :: Host . allowed ( cx, expr) {
171
+ if ( lint. as_name ( prefix) == le || lint. as_name ( prefix) == be) && LintKind :: Host . allowed ( cx, expr) {
183
172
help = Some ( Cow :: Borrowed ( "use the native endianness instead" ) ) ;
184
173
break ' build_help;
185
174
}
@@ -195,7 +184,7 @@ fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &str,
195
184
help_str. push_str ( "either of " ) ;
196
185
}
197
186
198
- help_str. push_str ( & format ! ( "`{ty}::{}` " , lint. to_name ( prefix) ) ) ;
187
+ help_str. push_str ( & format ! ( "`{ty}::{}` " , lint. as_name ( prefix) ) ) ;
199
188
200
189
if i != len && !only_one {
201
190
help_str. push_str ( "or " ) ;
@@ -211,9 +200,13 @@ fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &str,
211
200
expr. span ,
212
201
& format ! (
213
202
"usage of the {}`{ty}::{}`{}" ,
214
- if prefix == "from" { "function " } else { "" } ,
215
- lint. to_name( prefix) ,
216
- if prefix == "to" { " method" } else { "" } ,
203
+ if matches!( prefix, Prefix :: From ) {
204
+ "function "
205
+ } else {
206
+ ""
207
+ } ,
208
+ lint. as_name( prefix) ,
209
+ if matches!( prefix, Prefix :: To ) { " method" } else { "" } ,
217
210
) ,
218
211
move |diag| {
219
212
if let Some ( help) = help {
0 commit comments