about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCentri3 <114838443+Centri3@users.noreply.github.com>2023-05-31 17:29:18 -0500
committerCentri3 <114838443+Centri3@users.noreply.github.com>2023-05-31 17:30:23 -0500
commit95e8c0b35a0963aa998aa68d9c74cdb2c5a5e6ab (patch)
tree4cd690cc4f8f209689c18eb2adc1b90b497e8592
parent80ae1ec12d6aa7d5cec840387c14a196296b983c (diff)
downloadrust-95e8c0b35a0963aa998aa68d9c74cdb2c5a5e6ab.tar.gz
rust-95e8c0b35a0963aa998aa68d9c74cdb2c5a5e6ab.zip
don't allocate the names
-rw-r--r--clippy_lints/src/endian_bytes.rs48
1 files changed, 35 insertions, 13 deletions
diff --git a/clippy_lints/src/endian_bytes.rs b/clippy_lints/src/endian_bytes.rs
index 05005a791c2..19fb74b9050 100644
--- a/clippy_lints/src/endian_bytes.rs
+++ b/clippy_lints/src/endian_bytes.rs
@@ -9,7 +9,7 @@ use std::borrow::Cow;
 
 declare_clippy_lint! {
     /// ### What it does
-    /// Checks for the usage of the `to_ne_bytes` method.
+    /// Checks for the usage of the `to_ne_bytes` method and/or the function `from_ne_bytes`.
     ///
     /// ### Why is this bad?
     /// It's not, but some may prefer to specify the target endianness explicitly.
@@ -27,7 +27,7 @@ declare_clippy_lint! {
 
 declare_clippy_lint! {
     /// ### What it does
-    /// Checks for the usage of the `to_le_bytes` method.
+    /// Checks for the usage of the `to_le_bytes` method and/or the function `from_le_bytes`.
     ///
     /// ### Why is this bad?
     /// 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! {
 
 declare_clippy_lint! {
     /// ### What it does
-    /// Checks for the usage of the `to_be_bytes` method.
+    /// Checks for the usage of the `to_be_bytes` method and/or the function `from_be_bytes`.
     ///
     /// ### Why is this bad?
     /// 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! {
 
 declare_lint_pass!(EndianBytes => [HOST_ENDIAN_BYTES, LITTLE_ENDIAN_BYTES, BIG_ENDIAN_BYTES]);
 
+const HOST_NAMES: [&str; 2] = ["from_ne_bytes", "to_ne_bytes"];
+const LITTLE_NAMES: [&str; 2] = ["from_le_bytes", "to_le_bytes"];
+const BIG_NAMES: [&str; 2] = ["from_be_bytes", "to_be_bytes"];
+
 #[derive(Clone, Debug)]
 enum LintKind {
     Host,
@@ -85,11 +89,29 @@ impl LintKind {
         }
     }
 
-    fn to_name(&self, prefix: &str) -> String {
+    fn to_name(&self, prefix: &str) -> &str {
         match self {
-            LintKind::Host => format!("{prefix}_ne_bytes"),
-            LintKind::Little => format!("{prefix}_le_bytes"),
-            LintKind::Big => format!("{prefix}_be_bytes"),
+            LintKind::Host => {
+                if prefix == "from" {
+                    HOST_NAMES[0]
+                } 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]
+                }
+            },
         }
     }
 }
@@ -105,7 +127,7 @@ impl LateLintPass<'_> for EndianBytes {
             if args.is_empty();
             let ty = cx.typeck_results().expr_ty(receiver);
             if ty.is_primitive_ty();
-            if try_lint_endian_bytes(cx, expr, "to", method_name.ident.name, ty);
+            if maybe_lint_endian_bytes(cx, expr, "to", method_name.ident.name, ty);
             then {
                 return;
             }
@@ -119,16 +141,16 @@ impl LateLintPass<'_> for EndianBytes {
             let ty = cx.typeck_results().expr_ty(expr);
             if ty.is_primitive_ty();
             then {
-                try_lint_endian_bytes(cx, expr, "from", *function_name, ty);
+                maybe_lint_endian_bytes(cx, expr, "from", *function_name, ty);
             }
         }
     }
 }
 
-fn try_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &str, name: Symbol, ty: Ty<'_>) -> bool {
-    let ne = format!("{prefix}_ne_bytes");
-    let le = format!("{prefix}_le_bytes");
-    let be = format!("{prefix}_be_bytes");
+fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &str, name: Symbol, ty: Ty<'_>) -> bool {
+    let ne = LintKind::Host.to_name(prefix);
+    let le = LintKind::Little.to_name(prefix);
+    let be = LintKind::Big.to_name(prefix);
 
     let (lint, other_lints) = match name.as_str() {
         name if name == ne => ((&LintKind::Host), [(&LintKind::Little), (&LintKind::Big)]),