Skip to content

Commit 1b0230c

Browse files
committed
Refactor endian_bytes:
* Merge code paths. * Check HIR tree before checking for macros.
1 parent 0a25df8 commit 1b0230c

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

clippy_lints/src/endian_bytes.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -109,32 +109,27 @@ impl LintKind {
109109

110110
impl LateLintPass<'_> for EndianBytes {
111111
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
112-
if in_external_macro(cx.sess(), expr.span) {
113-
return;
114-
}
115-
116-
if let ExprKind::MethodCall(method_name, receiver, args, ..) = expr.kind
117-
&& args.is_empty()
118-
&& let ty = cx.typeck_results().expr_ty(receiver)
112+
let (prefix, name, ty_expr) = match expr.kind {
113+
ExprKind::MethodCall(method_name, receiver, [], ..) => (Prefix::To, method_name.ident.name, receiver),
114+
ExprKind::Call(function, ..)
115+
if let ExprKind::Path(qpath) = function.kind
116+
&& let Some(def_id) = cx.qpath_res(&qpath, function.hir_id).opt_def_id()
117+
&& let Some(function_name) = cx.get_def_path(def_id).last() =>
118+
{
119+
(Prefix::From, *function_name, expr)
120+
},
121+
_ => return,
122+
};
123+
if !in_external_macro(cx.sess(), expr.span)
124+
&& let ty = cx.typeck_results().expr_ty(ty_expr)
119125
&& ty.is_primitive_ty()
120-
&& maybe_lint_endian_bytes(cx, expr, Prefix::To, method_name.ident.name, ty)
121126
{
122-
return;
123-
}
124-
125-
if let ExprKind::Call(function, ..) = expr.kind
126-
&& let ExprKind::Path(qpath) = function.kind
127-
&& let Some(def_id) = cx.qpath_res(&qpath, function.hir_id).opt_def_id()
128-
&& let Some(function_name) = cx.get_def_path(def_id).last()
129-
&& let ty = cx.typeck_results().expr_ty(expr)
130-
&& ty.is_primitive_ty()
131-
{
132-
maybe_lint_endian_bytes(cx, expr, Prefix::From, *function_name, ty);
127+
maybe_lint_endian_bytes(cx, expr, prefix, name, ty);
133128
}
134129
}
135130
}
136131

137-
fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: Prefix, name: Symbol, ty: Ty<'_>) -> bool {
132+
fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: Prefix, name: Symbol, ty: Ty<'_>) {
138133
let ne = LintKind::Host.as_name(prefix);
139134
let le = LintKind::Little.as_name(prefix);
140135
let be = LintKind::Big.as_name(prefix);
@@ -143,7 +138,7 @@ fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: Prefix
143138
name if name == ne => ((&LintKind::Host), [(&LintKind::Little), (&LintKind::Big)]),
144139
name if name == le => ((&LintKind::Little), [(&LintKind::Host), (&LintKind::Big)]),
145140
name if name == be => ((&LintKind::Big), [(&LintKind::Host), (&LintKind::Little)]),
146-
_ => return false,
141+
_ => return,
147142
};
148143

149144
let mut help = None;
@@ -208,6 +203,4 @@ fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: Prefix
208203
}
209204
},
210205
);
211-
212-
true
213206
}

0 commit comments

Comments
 (0)