@@ -3,9 +3,11 @@ use crate::utils::{
3
3
is_automatically_derived, is_copy, match_path, span_lint_and_help, span_lint_and_note, span_lint_and_then,
4
4
} ;
5
5
use if_chain:: if_chain;
6
- use rustc_hir as hir;
7
6
use rustc_hir:: def_id:: DefId ;
8
7
use rustc_hir:: intravisit:: { walk_expr, walk_fn, walk_item, FnKind , NestedVisitorMap , Visitor } ;
8
+ use rustc_hir:: {
9
+ BlockCheckMode , BodyId , Expr , ExprKind , FnDecl , HirId , Item , ItemKind , TraitRef , UnsafeSource , Unsafety ,
10
+ } ;
9
11
use rustc_lint:: { LateContext , LateLintPass } ;
10
12
use rustc_middle:: hir:: map:: Map ;
11
13
use rustc_middle:: ty:: { self , Ty } ;
@@ -97,15 +99,15 @@ declare_clippy_lint! {
97
99
/// }
98
100
/// ```
99
101
pub UNSAFE_DERIVE_DESERIALIZE ,
100
- correctness ,
102
+ pedantic ,
101
103
"deriving `serde::Deserialize` on a type that has methods using `unsafe`"
102
104
}
103
105
104
106
declare_lint_pass ! ( Derive => [ EXPL_IMPL_CLONE_ON_COPY , DERIVE_HASH_XOR_EQ , UNSAFE_DERIVE_DESERIALIZE ] ) ;
105
107
106
108
impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for Derive {
107
- fn check_item ( & mut self , cx : & LateContext < ' a , ' tcx > , item : & ' tcx hir :: Item < ' _ > ) {
108
- if let hir :: ItemKind :: Impl {
109
+ fn check_item ( & mut self , cx : & LateContext < ' a , ' tcx > , item : & ' tcx Item < ' _ > ) {
110
+ if let ItemKind :: Impl {
109
111
of_trait : Some ( ref trait_ref) ,
110
112
..
111
113
} = item. kind
@@ -128,7 +130,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Derive {
128
130
fn check_hash_peq < ' a , ' tcx > (
129
131
cx : & LateContext < ' a , ' tcx > ,
130
132
span : Span ,
131
- trait_ref : & hir :: TraitRef < ' _ > ,
133
+ trait_ref : & TraitRef < ' _ > ,
132
134
ty : Ty < ' tcx > ,
133
135
hash_is_automatically_derived : bool ,
134
136
) {
@@ -175,12 +177,7 @@ fn check_hash_peq<'a, 'tcx>(
175
177
}
176
178
177
179
/// Implementation of the `EXPL_IMPL_CLONE_ON_COPY` lint.
178
- fn check_copy_clone < ' a , ' tcx > (
179
- cx : & LateContext < ' a , ' tcx > ,
180
- item : & hir:: Item < ' _ > ,
181
- trait_ref : & hir:: TraitRef < ' _ > ,
182
- ty : Ty < ' tcx > ,
183
- ) {
180
+ fn check_copy_clone < ' a , ' tcx > ( cx : & LateContext < ' a , ' tcx > , item : & Item < ' _ > , trait_ref : & TraitRef < ' _ > , ty : Ty < ' tcx > ) {
184
181
if match_path ( & trait_ref. path , & paths:: CLONE_TRAIT ) {
185
182
if !is_copy ( cx, ty) {
186
183
return ;
@@ -223,16 +220,16 @@ fn check_copy_clone<'a, 'tcx>(
223
220
/// Implementation of the `UNSAFE_DERIVE_DESERIALIZE` lint.
224
221
fn check_unsafe_derive_deserialize < ' a , ' tcx > (
225
222
cx : & LateContext < ' a , ' tcx > ,
226
- item : & hir :: Item < ' _ > ,
227
- trait_ref : & hir :: TraitRef < ' _ > ,
223
+ item : & Item < ' _ > ,
224
+ trait_ref : & TraitRef < ' _ > ,
228
225
ty : Ty < ' tcx > ,
229
226
) {
230
- fn item_from_def_id < ' tcx > ( cx : & LateContext < ' _ , ' tcx > , def_id : DefId ) -> & ' tcx hir :: Item < ' tcx > {
227
+ fn item_from_def_id < ' tcx > ( cx : & LateContext < ' _ , ' tcx > , def_id : DefId ) -> & ' tcx Item < ' tcx > {
231
228
let hir_id = cx. tcx . hir ( ) . as_local_hir_id ( def_id) . unwrap ( ) ;
232
229
cx. tcx . hir ( ) . expect_item ( hir_id)
233
230
}
234
231
235
- fn has_unsafe < ' tcx > ( cx : & LateContext < ' _ , ' tcx > , item : & ' tcx hir :: Item < ' _ > ) -> bool {
232
+ fn has_unsafe < ' tcx > ( cx : & LateContext < ' _ , ' tcx > , item : & ' tcx Item < ' _ > ) -> bool {
236
233
let mut visitor = UnsafeVisitor { cx, has_unsafe : false } ;
237
234
walk_item ( & mut visitor, item) ;
238
235
visitor. has_unsafe
@@ -267,21 +264,14 @@ struct UnsafeVisitor<'a, 'tcx> {
267
264
impl < ' tcx > Visitor < ' tcx > for UnsafeVisitor < ' _ , ' tcx > {
268
265
type Map = Map < ' tcx > ;
269
266
270
- fn visit_fn (
271
- & mut self ,
272
- kind : FnKind < ' tcx > ,
273
- decl : & ' tcx hir:: FnDecl < ' _ > ,
274
- body_id : hir:: BodyId ,
275
- span : Span ,
276
- id : hir:: HirId ,
277
- ) {
267
+ fn visit_fn ( & mut self , kind : FnKind < ' tcx > , decl : & ' tcx FnDecl < ' _ > , body_id : BodyId , span : Span , id : HirId ) {
278
268
if self . has_unsafe {
279
269
return ;
280
270
}
281
271
282
272
if_chain ! {
283
273
if let Some ( header) = kind. header( ) ;
284
- if let hir :: Unsafety :: Unsafe = header. unsafety;
274
+ if let Unsafety :: Unsafe = header. unsafety;
285
275
then {
286
276
self . has_unsafe = true ;
287
277
}
@@ -290,14 +280,12 @@ impl<'tcx> Visitor<'tcx> for UnsafeVisitor<'_, 'tcx> {
290
280
walk_fn ( self , kind, decl, body_id, span, id) ;
291
281
}
292
282
293
- fn visit_expr ( & mut self , expr : & ' tcx hir :: Expr < ' _ > ) {
283
+ fn visit_expr ( & mut self , expr : & ' tcx Expr < ' _ > ) {
294
284
if self . has_unsafe {
295
285
return ;
296
286
}
297
287
298
- if let hir:: ExprKind :: Block ( block, _) = expr. kind {
299
- use hir:: { BlockCheckMode , UnsafeSource } ;
300
-
288
+ if let ExprKind :: Block ( block, _) = expr. kind {
301
289
match block. rules {
302
290
BlockCheckMode :: UnsafeBlock ( UnsafeSource :: UserProvided )
303
291
| BlockCheckMode :: PushUnsafeBlock ( UnsafeSource :: UserProvided )
0 commit comments