@@ -2,7 +2,7 @@ use crate::Lint;
2
2
use clippy_utils:: { diagnostics:: span_lint_and_then, is_lint_allowed} ;
3
3
use rustc_hir:: { Expr , ExprKind } ;
4
4
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
5
- use rustc_middle:: lint:: in_external_macro;
5
+ use rustc_middle:: { lint:: in_external_macro, ty :: Ty } ;
6
6
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
7
7
use rustc_span:: Symbol ;
8
8
use std:: borrow:: Cow ;
@@ -102,11 +102,13 @@ impl LateLintPass<'_> for EndianBytes {
102
102
103
103
if_chain ! {
104
104
if let ExprKind :: MethodCall ( method_name, receiver, args, ..) = expr. kind;
105
- if let ExprKind :: Lit ( ..) = receiver. kind;
106
105
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( ) ;
108
108
then {
109
- return ;
109
+ if try_lint_endian_bytes( cx, expr, "to" , method_name. ident. name, ty) {
110
+ return ;
111
+ }
110
112
}
111
113
}
112
114
@@ -115,15 +117,16 @@ impl LateLintPass<'_> for EndianBytes {
115
117
if let ExprKind :: Path ( qpath) = function. kind;
116
118
if let Some ( def_id) = cx. qpath_res( & qpath, function. hir_id) . opt_def_id( ) ;
117
119
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( ) ;
119
122
then {
120
- try_lint_endian_bytes( cx, expr, "from" , * function_name) ;
123
+ try_lint_endian_bytes( cx, expr, "from" , * function_name, ty ) ;
121
124
}
122
125
}
123
126
}
124
127
}
125
128
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 {
127
130
let ne = format ! ( "{prefix}_ne_bytes" ) ;
128
131
let le = format ! ( "{prefix}_le_bytes" ) ;
129
132
let be = format ! ( "{prefix}_be_bytes" ) ;
@@ -171,7 +174,7 @@ fn try_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &str, na
171
174
help_str. push_str ( "either of " ) ;
172
175
}
173
176
174
- help_str. push_str ( & format ! ( "`{}` " , lint. to_name( prefix) ) ) ;
177
+ help_str. push_str ( & format ! ( "`{ty}::{ }` " , lint. to_name( prefix) ) ) ;
175
178
176
179
if i != len && !only_one {
177
180
help_str. push_str ( "or " ) ;
@@ -185,7 +188,12 @@ fn try_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: &str, na
185
188
cx,
186
189
lint. as_lint ( ) ,
187
190
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
+ ) ,
189
197
move |diag| {
190
198
if let Some ( help) = help {
191
199
diag. help ( help) ;
0 commit comments