Skip to content

Commit a6c3655

Browse files
committed
use enum for prefix instead of &str
1 parent 95e8c0b commit a6c3655

File tree

1 file changed

+27
-34
lines changed

1 file changed

+27
-34
lines changed

clippy_lints/src/endian_bytes.rs

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ enum LintKind {
7676
Big,
7777
}
7878

79+
enum Prefix {
80+
From,
81+
To,
82+
}
83+
7984
impl LintKind {
8085
fn allowed(&self, cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
8186
is_lint_allowed(cx, self.as_lint(), expr.hir_id)
@@ -89,29 +94,13 @@ impl LintKind {
8994
}
9095
}
9196

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+
93100
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],
115104
}
116105
}
117106
}
@@ -127,7 +116,7 @@ impl LateLintPass<'_> for EndianBytes {
127116
if args.is_empty();
128117
let ty = cx.typeck_results().expr_ty(receiver);
129118
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);
131120
then {
132121
return;
133122
}
@@ -141,16 +130,16 @@ impl LateLintPass<'_> for EndianBytes {
141130
let ty = cx.typeck_results().expr_ty(expr);
142131
if ty.is_primitive_ty();
143132
then {
144-
maybe_lint_endian_bytes(cx, expr, "from", *function_name, ty);
133+
maybe_lint_endian_bytes(cx, expr, &Prefix::From, *function_name, ty);
145134
}
146135
}
147136
}
148137
}
149138

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);
154143

155144
let (lint, other_lints) = match name.as_str() {
156145
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,
172161
}
173162

174163
// 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)) {
176165
help = Some(Cow::Borrowed("specify the desired endianness explicitly"));
177166
break 'build_help;
178167
}
179168

180169
// le_bytes where ne_bytes allowed but be_bytes is not, or le_bytes where ne_bytes allowed but
181170
// 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) {
183172
help = Some(Cow::Borrowed("use the native endianness instead"));
184173
break 'build_help;
185174
}
@@ -195,7 +184,7 @@ fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &str,
195184
help_str.push_str("either of ");
196185
}
197186

198-
help_str.push_str(&format!("`{ty}::{}` ", lint.to_name(prefix)));
187+
help_str.push_str(&format!("`{ty}::{}` ", lint.as_name(prefix)));
199188

200189
if i != len && !only_one {
201190
help_str.push_str("or ");
@@ -211,9 +200,13 @@ fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &str,
211200
expr.span,
212201
&format!(
213202
"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 { "" },
217210
),
218211
move |diag| {
219212
if let Some(help) = help {

0 commit comments

Comments
 (0)