derive Copy/PartialEq for Prefix

This commit is contained in:
Centri3 2023-06-03 14:31:40 -05:00
parent b1a21ae347
commit 7fe200ed05

View File

@ -76,6 +76,7 @@ enum LintKind {
Big, Big,
} }
#[derive(Clone, Copy, PartialEq)]
enum Prefix { enum Prefix {
From, From,
To, To,
@ -94,8 +95,8 @@ impl LintKind {
} }
} }
fn as_name(&self, prefix: &Prefix) -> &str { fn as_name(&self, prefix: Prefix) -> &str {
let index = usize::from(matches!(prefix, Prefix::To)); let index = usize::from(prefix == Prefix::To);
match self { match self {
LintKind::Host => HOST_NAMES[index], LintKind::Host => HOST_NAMES[index],
@ -116,7 +117,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, &Prefix::To, method_name.ident.name, ty); if maybe_lint_endian_bytes(cx, expr, Prefix::To, method_name.ident.name, ty);
then { then {
return; return;
} }
@ -130,13 +131,13 @@ 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, &Prefix::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: &Prefix, 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.as_name(prefix); let ne = LintKind::Host.as_name(prefix);
let le = LintKind::Little.as_name(prefix); let le = LintKind::Little.as_name(prefix);
let be = LintKind::Big.as_name(prefix); let be = LintKind::Big.as_name(prefix);
@ -200,13 +201,9 @@ fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &Prefi
expr.span, expr.span,
&format!( &format!(
"usage of the {}`{ty}::{}`{}", "usage of the {}`{ty}::{}`{}",
if matches!(prefix, Prefix::From) { if prefix == Prefix::From { "function " } else { "" },
"function "
} else {
""
},
lint.as_name(prefix), lint.as_name(prefix),
if matches!(prefix, Prefix::To) { " method" } else { "" }, if prefix == Prefix::To { " method" } else { "" },
), ),
move |diag| { move |diag| {
if let Some(help) = help { if let Some(help) = help {