use enum for prefix instead of &str

This commit is contained in:
Centri3 2023-06-02 14:35:54 -05:00
parent 95e8c0b35a
commit a6c36556c8

View File

@ -76,6 +76,11 @@ enum LintKind {
Big, Big,
} }
enum Prefix {
From,
To,
}
impl LintKind { impl LintKind {
fn allowed(&self, cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { fn allowed(&self, cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
is_lint_allowed(cx, self.as_lint(), expr.hir_id) is_lint_allowed(cx, self.as_lint(), expr.hir_id)
@ -89,29 +94,13 @@ impl LintKind {
} }
} }
fn to_name(&self, prefix: &str) -> &str { fn as_name(&self, prefix: &Prefix) -> &str {
let index = if matches!(prefix, Prefix::From) { 0 } else { 1 };
match self { match self {
LintKind::Host => { LintKind::Host => HOST_NAMES[index],
if prefix == "from" { LintKind::Little => LITTLE_NAMES[index],
HOST_NAMES[0] LintKind::Big => BIG_NAMES[index],
} else {
HOST_NAMES[1]
}
},
LintKind::Little => {
if prefix == "from" {
LITTLE_NAMES[0]
} else {
LITTLE_NAMES[1]
}
},
LintKind::Big => {
if prefix == "from" {
BIG_NAMES[0]
} else {
BIG_NAMES[1]
}
},
} }
} }
} }
@ -127,7 +116,7 @@ impl LateLintPass<'_> for EndianBytes {
if args.is_empty(); if args.is_empty();
let ty = cx.typeck_results().expr_ty(receiver); let ty = cx.typeck_results().expr_ty(receiver);
if ty.is_primitive_ty(); if ty.is_primitive_ty();
if maybe_lint_endian_bytes(cx, expr, "to", method_name.ident.name, ty); if maybe_lint_endian_bytes(cx, expr, &Prefix::To, method_name.ident.name, ty);
then { then {
return; return;
} }
@ -141,16 +130,16 @@ impl LateLintPass<'_> for EndianBytes {
let ty = cx.typeck_results().expr_ty(expr); let ty = cx.typeck_results().expr_ty(expr);
if ty.is_primitive_ty(); if ty.is_primitive_ty();
then { then {
maybe_lint_endian_bytes(cx, expr, "from", *function_name, ty); maybe_lint_endian_bytes(cx, expr, &Prefix::From, *function_name, ty);
} }
} }
} }
} }
fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &str, name: Symbol, ty: Ty<'_>) -> bool { fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &Prefix, name: Symbol, ty: Ty<'_>) -> bool {
let ne = LintKind::Host.to_name(prefix); let ne = LintKind::Host.as_name(prefix);
let le = LintKind::Little.to_name(prefix); let le = LintKind::Little.as_name(prefix);
let be = LintKind::Big.to_name(prefix); let be = LintKind::Big.as_name(prefix);
let (lint, other_lints) = match name.as_str() { let (lint, other_lints) = match name.as_str() {
name if name == ne => ((&LintKind::Host), [(&LintKind::Little), (&LintKind::Big)]), 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,
} }
// ne_bytes and all other lints allowed // ne_bytes and all other lints allowed
if lint.to_name(prefix) == ne && other_lints.iter().all(|lint| lint.allowed(cx, expr)) { if lint.as_name(prefix) == ne && other_lints.iter().all(|lint| lint.allowed(cx, expr)) {
help = Some(Cow::Borrowed("specify the desired endianness explicitly")); help = Some(Cow::Borrowed("specify the desired endianness explicitly"));
break 'build_help; break 'build_help;
} }
// le_bytes where ne_bytes allowed but be_bytes is not, or le_bytes where ne_bytes allowed but // le_bytes where ne_bytes allowed but be_bytes is not, or le_bytes where ne_bytes allowed but
// le_bytes is not // le_bytes is not
if (lint.to_name(prefix) == le || lint.to_name(prefix) == be) && LintKind::Host.allowed(cx, expr) { if (lint.as_name(prefix) == le || lint.as_name(prefix) == be) && LintKind::Host.allowed(cx, expr) {
help = Some(Cow::Borrowed("use the native endianness instead")); help = Some(Cow::Borrowed("use the native endianness instead"));
break 'build_help; break 'build_help;
} }
@ -195,7 +184,7 @@ fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &str,
help_str.push_str("either of "); help_str.push_str("either of ");
} }
help_str.push_str(&format!("`{ty}::{}` ", lint.to_name(prefix))); help_str.push_str(&format!("`{ty}::{}` ", lint.as_name(prefix)));
if i != len && !only_one { if i != len && !only_one {
help_str.push_str("or "); help_str.push_str("or ");
@ -211,9 +200,13 @@ fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &str,
expr.span, expr.span,
&format!( &format!(
"usage of the {}`{ty}::{}`{}", "usage of the {}`{ty}::{}`{}",
if prefix == "from" { "function " } else { "" }, if matches!(prefix, Prefix::From) {
lint.to_name(prefix), "function "
if prefix == "to" { " method" } else { "" }, } else {
""
},
lint.as_name(prefix),
if matches!(prefix, Prefix::To) { " method" } else { "" },
), ),
move |diag| { move |diag| {
if let Some(help) = help { if let Some(help) = help {