Skip to content

Commit 04b7cae

Browse files
committed
refine output
1 parent 48d36a7 commit 04b7cae

File tree

3 files changed

+217
-209
lines changed

3 files changed

+217
-209
lines changed

clippy_lints/src/endian_bytes.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::Lint;
22
use clippy_utils::{diagnostics::span_lint_and_then, is_lint_allowed};
33
use rustc_hir::{Expr, ExprKind};
44
use rustc_lint::{LateContext, LateLintPass, LintContext};
5-
use rustc_middle::lint::in_external_macro;
5+
use rustc_middle::{lint::in_external_macro, ty::Ty};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
77
use rustc_span::Symbol;
88
use std::borrow::Cow;
@@ -102,11 +102,13 @@ impl LateLintPass<'_> for EndianBytes {
102102

103103
if_chain! {
104104
if let ExprKind::MethodCall(method_name, receiver, args, ..) = expr.kind;
105-
if let ExprKind::Lit(..) = receiver.kind;
106105
if args.is_empty();
107-
if try_lint_endian_bytes(cx, expr, "to", method_name.ident.name);
106+
let ty = cx.typeck_results().expr_ty(receiver);
107+
if ty.is_primitive_ty();
108108
then {
109-
return;
109+
if try_lint_endian_bytes(cx, expr, "to", method_name.ident.name, ty) {
110+
return;
111+
}
110112
}
111113
}
112114

@@ -115,15 +117,16 @@ impl LateLintPass<'_> for EndianBytes {
115117
if let ExprKind::Path(qpath) = function.kind;
116118
if let Some(def_id) = cx.qpath_res(&qpath, function.hir_id).opt_def_id();
117119
if let Some(function_name) = cx.get_def_path(def_id).last();
118-
if cx.typeck_results().expr_ty(expr).is_primitive_ty();
120+
let ty = cx.typeck_results().expr_ty(expr);
121+
if ty.is_primitive_ty();
119122
then {
120-
try_lint_endian_bytes(cx, expr, "from", *function_name);
123+
try_lint_endian_bytes(cx, expr, "from", *function_name, ty);
121124
}
122125
}
123126
}
124127
}
125128

126-
fn try_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &str, name: Symbol) -> bool {
129+
fn try_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &str, name: Symbol, ty: Ty<'_>) -> bool {
127130
let ne = format!("{prefix}_ne_bytes");
128131
let le = format!("{prefix}_le_bytes");
129132
let be = format!("{prefix}_be_bytes");
@@ -171,7 +174,7 @@ fn try_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &str, na
171174
help_str.push_str("either of ");
172175
}
173176

174-
help_str.push_str(&format!("`{}` ", lint.to_name(prefix)));
177+
help_str.push_str(&format!("`{ty}::{}` ", lint.to_name(prefix)));
175178

176179
if i != len && !only_one {
177180
help_str.push_str("or ");
@@ -185,7 +188,12 @@ fn try_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &str, na
185188
cx,
186189
lint.as_lint(),
187190
expr.span,
188-
&format!("usage of the method `{}`", lint.to_name(prefix)),
191+
&format!(
192+
"usage of the {}`{ty}::{}`{}",
193+
if prefix == "from" { "function " } else { "" },
194+
lint.to_name(prefix),
195+
if prefix == "to" { " method" } else { "" },
196+
),
189197
move |diag| {
190198
if let Some(help) = help {
191199
diag.help(help);

tests/ui/endian_bytes.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ macro_rules! fn_body {
6565
}
6666

6767
// bless breaks if I use fn_body too much (oops)
68-
macro_rules! fn_body_small {
68+
macro_rules! fn_body_smol {
6969
() => {
7070
2u8.to_ne_bytes();
7171
u8::from_ne_bytes(todo!());
@@ -93,35 +93,35 @@ fn big() { fn_body!(); }
9393
#[rustfmt::skip]
9494
#[warn(clippy::host_endian_bytes)]
9595
#[warn(clippy::big_endian_bytes)]
96-
fn host_encourage_little() { fn_body_small!(); }
96+
fn host_encourage_little() { fn_body_smol!(); }
9797

9898
#[rustfmt::skip]
9999
#[warn(clippy::host_endian_bytes)]
100100
#[warn(clippy::little_endian_bytes)]
101-
fn host_encourage_big() { fn_body_small!(); }
101+
fn host_encourage_big() { fn_body_smol!(); }
102102

103103
#[rustfmt::skip]
104104
#[warn(clippy::host_endian_bytes)]
105105
#[warn(clippy::little_endian_bytes)]
106106
#[warn(clippy::big_endian_bytes)]
107-
fn no_help() { fn_body_small!(); }
107+
fn no_help() { fn_body_smol!(); }
108108

109109
#[rustfmt::skip]
110110
#[warn(clippy::little_endian_bytes)]
111111
#[warn(clippy::big_endian_bytes)]
112-
fn little_encourage_host() { fn_body_small!(); }
112+
fn little_encourage_host() { fn_body_smol!(); }
113113

114114
#[rustfmt::skip]
115115
#[warn(clippy::host_endian_bytes)]
116116
#[warn(clippy::little_endian_bytes)]
117-
fn little_encourage_big() { fn_body_small!(); }
117+
fn little_encourage_big() { fn_body_smol!(); }
118118

119119
#[rustfmt::skip]
120120
#[warn(clippy::big_endian_bytes)]
121121
#[warn(clippy::little_endian_bytes)]
122-
fn big_encourage_host() { fn_body_small!(); }
122+
fn big_encourage_host() { fn_body_smol!(); }
123123

124124
#[rustfmt::skip]
125125
#[warn(clippy::host_endian_bytes)]
126126
#[warn(clippy::big_endian_bytes)]
127-
fn big_encourage_little() { fn_body_small!(); }
127+
fn big_encourage_little() { fn_body_smol!(); }

0 commit comments

Comments
 (0)