@@ -9,7 +9,7 @@ use std::borrow::Cow;
9
9
10
10
declare_clippy_lint ! {
11
11
/// ### What it does
12
- /// Checks for the usage of the `to_ne_bytes` method.
12
+ /// Checks for the usage of the `to_ne_bytes` method and/or the function `from_ne_bytes` .
13
13
///
14
14
/// ### Why is this bad?
15
15
/// It's not, but some may prefer to specify the target endianness explicitly.
@@ -27,7 +27,7 @@ declare_clippy_lint! {
27
27
28
28
declare_clippy_lint ! {
29
29
/// ### What it does
30
- /// Checks for the usage of the `to_le_bytes` method.
30
+ /// Checks for the usage of the `to_le_bytes` method and/or the function `from_le_bytes` .
31
31
///
32
32
/// ### Why is this bad?
33
33
/// 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! {
46
46
47
47
declare_clippy_lint ! {
48
48
/// ### What it does
49
- /// Checks for the usage of the `to_be_bytes` method.
49
+ /// Checks for the usage of the `to_be_bytes` method and/or the function `from_be_bytes` .
50
50
///
51
51
/// ### Why is this bad?
52
52
/// 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! {
65
65
66
66
declare_lint_pass ! ( EndianBytes => [ HOST_ENDIAN_BYTES , LITTLE_ENDIAN_BYTES , BIG_ENDIAN_BYTES ] ) ;
67
67
68
+ const HOST_NAMES : [ & str ; 2 ] = [ "from_ne_bytes" , "to_ne_bytes" ] ;
69
+ const LITTLE_NAMES : [ & str ; 2 ] = [ "from_le_bytes" , "to_le_bytes" ] ;
70
+ const BIG_NAMES : [ & str ; 2 ] = [ "from_be_bytes" , "to_be_bytes" ] ;
71
+
68
72
#[ derive( Clone , Debug ) ]
69
73
enum LintKind {
70
74
Host ,
@@ -85,11 +89,29 @@ impl LintKind {
85
89
}
86
90
}
87
91
88
- fn to_name ( & self , prefix : & str ) -> String {
92
+ fn to_name ( & self , prefix : & str ) -> & str {
89
93
match self {
90
- LintKind :: Host => format ! ( "{prefix}_ne_bytes" ) ,
91
- LintKind :: Little => format ! ( "{prefix}_le_bytes" ) ,
92
- LintKind :: Big => format ! ( "{prefix}_be_bytes" ) ,
94
+ LintKind :: Host => {
95
+ if prefix == "from" {
96
+ HOST_NAMES [ 0 ]
97
+ } else {
98
+ HOST_NAMES [ 1 ]
99
+ }
100
+ } ,
101
+ LintKind :: Little => {
102
+ if prefix == "from" {
103
+ LITTLE_NAMES [ 0 ]
104
+ } else {
105
+ LITTLE_NAMES [ 1 ]
106
+ }
107
+ } ,
108
+ LintKind :: Big => {
109
+ if prefix == "from" {
110
+ BIG_NAMES [ 0 ]
111
+ } else {
112
+ BIG_NAMES [ 1 ]
113
+ }
114
+ } ,
93
115
}
94
116
}
95
117
}
@@ -105,7 +127,7 @@ impl LateLintPass<'_> for EndianBytes {
105
127
if args. is_empty( ) ;
106
128
let ty = cx. typeck_results( ) . expr_ty( receiver) ;
107
129
if ty. is_primitive_ty( ) ;
108
- if try_lint_endian_bytes ( cx, expr, "to" , method_name. ident. name, ty) ;
130
+ if maybe_lint_endian_bytes ( cx, expr, "to" , method_name. ident. name, ty) ;
109
131
then {
110
132
return ;
111
133
}
@@ -119,16 +141,16 @@ impl LateLintPass<'_> for EndianBytes {
119
141
let ty = cx. typeck_results( ) . expr_ty( expr) ;
120
142
if ty. is_primitive_ty( ) ;
121
143
then {
122
- try_lint_endian_bytes ( cx, expr, "from" , * function_name, ty) ;
144
+ maybe_lint_endian_bytes ( cx, expr, "from" , * function_name, ty) ;
123
145
}
124
146
}
125
147
}
126
148
}
127
149
128
- fn try_lint_endian_bytes ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , prefix : & str , name : Symbol , ty : Ty < ' _ > ) -> bool {
129
- let ne = format ! ( "{ prefix}_ne_bytes" ) ;
130
- let le = format ! ( "{ prefix}_le_bytes" ) ;
131
- let be = format ! ( "{ prefix}_be_bytes" ) ;
150
+ fn maybe_lint_endian_bytes ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , prefix : & str , name : Symbol , ty : Ty < ' _ > ) -> bool {
151
+ let ne = LintKind :: Host . to_name ( prefix) ;
152
+ let le = LintKind :: Little . to_name ( prefix) ;
153
+ let be = LintKind :: Big . to_name ( prefix) ;
132
154
133
155
let ( lint, other_lints) = match name. as_str ( ) {
134
156
name if name == ne => ( ( & LintKind :: Host ) , [ ( & LintKind :: Little ) , ( & LintKind :: Big ) ] ) ,
0 commit comments