Skip to content

Commit 95e8c0b

Browse files
committed
don't allocate the names
1 parent 80ae1ec commit 95e8c0b

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

clippy_lints/src/endian_bytes.rs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::borrow::Cow;
99

1010
declare_clippy_lint! {
1111
/// ### What it does
12-
/// Checks for the usage of the `to_ne_bytes` method.
12+
/// Checks for the usage of the `to_ne_bytes` method and/or the function `from_ne_bytes`.
1313
///
1414
/// ### Why is this bad?
1515
/// It's not, but some may prefer to specify the target endianness explicitly.
@@ -27,7 +27,7 @@ declare_clippy_lint! {
2727

2828
declare_clippy_lint! {
2929
/// ### What it does
30-
/// Checks for the usage of the `to_le_bytes` method.
30+
/// Checks for the usage of the `to_le_bytes` method and/or the function `from_le_bytes`.
3131
///
3232
/// ### Why is this bad?
3333
/// It's not, but some may wish to lint usage of this method, either to suggest using the host
@@ -46,7 +46,7 @@ declare_clippy_lint! {
4646

4747
declare_clippy_lint! {
4848
/// ### What it does
49-
/// Checks for the usage of the `to_be_bytes` method.
49+
/// Checks for the usage of the `to_be_bytes` method and/or the function `from_be_bytes`.
5050
///
5151
/// ### Why is this bad?
5252
/// It's not, but some may wish to lint usage of this method, either to suggest using the host
@@ -65,6 +65,10 @@ declare_clippy_lint! {
6565

6666
declare_lint_pass!(EndianBytes => [HOST_ENDIAN_BYTES, LITTLE_ENDIAN_BYTES, BIG_ENDIAN_BYTES]);
6767

68+
const HOST_NAMES: [&str; 2] = ["from_ne_bytes", "to_ne_bytes"];
69+
const LITTLE_NAMES: [&str; 2] = ["from_le_bytes", "to_le_bytes"];
70+
const BIG_NAMES: [&str; 2] = ["from_be_bytes", "to_be_bytes"];
71+
6872
#[derive(Clone, Debug)]
6973
enum LintKind {
7074
Host,
@@ -85,11 +89,29 @@ impl LintKind {
8589
}
8690
}
8791

88-
fn to_name(&self, prefix: &str) -> String {
92+
fn to_name(&self, prefix: &str) -> &str {
8993
match self {
90-
LintKind::Host => format!("{prefix}_ne_bytes"),
91-
LintKind::Little => format!("{prefix}_le_bytes"),
92-
LintKind::Big => format!("{prefix}_be_bytes"),
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+
},
93115
}
94116
}
95117
}
@@ -105,7 +127,7 @@ impl LateLintPass<'_> for EndianBytes {
105127
if args.is_empty();
106128
let ty = cx.typeck_results().expr_ty(receiver);
107129
if ty.is_primitive_ty();
108-
if try_lint_endian_bytes(cx, expr, "to", method_name.ident.name, ty);
130+
if maybe_lint_endian_bytes(cx, expr, "to", method_name.ident.name, ty);
109131
then {
110132
return;
111133
}
@@ -119,16 +141,16 @@ impl LateLintPass<'_> for EndianBytes {
119141
let ty = cx.typeck_results().expr_ty(expr);
120142
if ty.is_primitive_ty();
121143
then {
122-
try_lint_endian_bytes(cx, expr, "from", *function_name, ty);
144+
maybe_lint_endian_bytes(cx, expr, "from", *function_name, ty);
123145
}
124146
}
125147
}
126148
}
127149

128-
fn try_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &str, name: Symbol, ty: Ty<'_>) -> bool {
129-
let ne = format!("{prefix}_ne_bytes");
130-
let le = format!("{prefix}_le_bytes");
131-
let be = format!("{prefix}_be_bytes");
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);
132154

133155
let (lint, other_lints) = match name.as_str() {
134156
name if name == ne => ((&LintKind::Host), [(&LintKind::Little), (&LintKind::Big)]),

0 commit comments

Comments
 (0)