about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJason Newcomb <jsnewcomb@pm.me>2024-06-10 01:22:37 -0400
committerJason Newcomb <jsnewcomb@pm.me>2024-07-07 16:10:19 -0400
commit1b0230cf2525d8c3653cab40d888d6c9b90c059f (patch)
treebed459b6eeef8ccc8ca5520bb88b3623650b8568
parent0a25df87efd5fdf1543d26764c929d8d6e00a48a (diff)
downloadrust-1b0230cf2525d8c3653cab40d888d6c9b90c059f.tar.gz
rust-1b0230cf2525d8c3653cab40d888d6c9b90c059f.zip
Refactor `endian_bytes`:
* Merge code paths.
* Check HIR tree before checking for macros.
-rw-r--r--clippy_lints/src/endian_bytes.rs39
1 files changed, 16 insertions, 23 deletions
diff --git a/clippy_lints/src/endian_bytes.rs b/clippy_lints/src/endian_bytes.rs
index bb766e96338..f11ac5f317c 100644
--- a/clippy_lints/src/endian_bytes.rs
+++ b/clippy_lints/src/endian_bytes.rs
@@ -109,32 +109,27 @@ impl LintKind {
 
 impl LateLintPass<'_> for EndianBytes {
     fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
-        if in_external_macro(cx.sess(), expr.span) {
-            return;
-        }
-
-        if let ExprKind::MethodCall(method_name, receiver, args, ..) = expr.kind
-            && args.is_empty()
-            && let ty = cx.typeck_results().expr_ty(receiver)
+        let (prefix, name, ty_expr) = match expr.kind {
+            ExprKind::MethodCall(method_name, receiver, [], ..) => (Prefix::To, method_name.ident.name, receiver),
+            ExprKind::Call(function, ..)
+                if let ExprKind::Path(qpath) = function.kind
+                    && let Some(def_id) = cx.qpath_res(&qpath, function.hir_id).opt_def_id()
+                    && let Some(function_name) = cx.get_def_path(def_id).last() =>
+            {
+                (Prefix::From, *function_name, expr)
+            },
+            _ => return,
+        };
+        if !in_external_macro(cx.sess(), expr.span)
+            && let ty = cx.typeck_results().expr_ty(ty_expr)
             && ty.is_primitive_ty()
-            && maybe_lint_endian_bytes(cx, expr, Prefix::To, method_name.ident.name, ty)
         {
-            return;
-        }
-
-        if let ExprKind::Call(function, ..) = expr.kind
-            && let ExprKind::Path(qpath) = function.kind
-            && let Some(def_id) = cx.qpath_res(&qpath, function.hir_id).opt_def_id()
-            && let Some(function_name) = cx.get_def_path(def_id).last()
-            && let ty = cx.typeck_results().expr_ty(expr)
-            && ty.is_primitive_ty()
-        {
-            maybe_lint_endian_bytes(cx, expr, Prefix::From, *function_name, ty);
+            maybe_lint_endian_bytes(cx, expr, prefix, 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<'_>) {
     let ne = LintKind::Host.as_name(prefix);
     let le = LintKind::Little.as_name(prefix);
     let be = LintKind::Big.as_name(prefix);
@@ -143,7 +138,7 @@ fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: Prefix
         name if name == ne => ((&LintKind::Host), [(&LintKind::Little), (&LintKind::Big)]),
         name if name == le => ((&LintKind::Little), [(&LintKind::Host), (&LintKind::Big)]),
         name if name == be => ((&LintKind::Big), [(&LintKind::Host), (&LintKind::Little)]),
-        _ => return false,
+        _ => return,
     };
 
     let mut help = None;
@@ -208,6 +203,4 @@ fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: Prefix
             }
         },
     );
-
-    true
 }