1
+ mod not_unsafe_ptr_arg_deref;
1
2
mod too_many_arguments;
2
3
mod too_many_lines;
3
4
4
- use clippy_utils:: diagnostics:: { span_lint , span_lint_and_help, span_lint_and_then} ;
5
+ use clippy_utils:: diagnostics:: { span_lint_and_help, span_lint_and_then} ;
5
6
use clippy_utils:: source:: snippet_opt;
6
- use clippy_utils:: ty:: { is_must_use_ty, is_type_diagnostic_item, type_is_unsafe_function} ;
7
- use clippy_utils:: {
8
- attr_by_name, attrs:: is_proc_macro, iter_input_pats, match_def_path, must_use_attr, path_to_local, return_ty,
9
- trait_ref_of_method,
10
- } ;
7
+ use clippy_utils:: ty:: { is_must_use_ty, is_type_diagnostic_item} ;
8
+ use clippy_utils:: { attr_by_name, attrs:: is_proc_macro, match_def_path, must_use_attr, return_ty, trait_ref_of_method} ;
11
9
use if_chain:: if_chain;
12
10
use rustc_ast:: ast:: Attribute ;
13
11
use rustc_data_structures:: fx:: FxHashSet ;
@@ -258,14 +256,7 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
258
256
) {
259
257
too_many_arguments:: check_fn ( cx, kind, decl, span, hir_id, self . too_many_arguments_threshold ) ;
260
258
too_many_lines:: check ( cx, span, body, self . too_many_lines_threshold ) ;
261
-
262
- let unsafety = match kind {
263
- intravisit:: FnKind :: ItemFn ( _, _, hir:: FnHeader { unsafety, .. } , _) => unsafety,
264
- intravisit:: FnKind :: Method ( _, sig, _) => sig. header . unsafety ,
265
- intravisit:: FnKind :: Closure => return ,
266
- } ;
267
-
268
- Self :: check_raw_ptr ( cx, unsafety, decl, body, hir_id) ;
259
+ not_unsafe_ptr_arg_deref:: check_fn ( cx, kind, decl, body, hir_id) ;
269
260
}
270
261
271
262
fn check_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx hir:: Item < ' _ > ) {
@@ -323,6 +314,7 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
323
314
324
315
fn check_trait_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx hir:: TraitItem < ' _ > ) {
325
316
too_many_arguments:: check_trait_item ( cx, item, self . too_many_arguments_threshold ) ;
317
+ not_unsafe_ptr_arg_deref:: check_trait_item ( cx, item) ;
326
318
327
319
if let hir:: TraitItemKind :: Fn ( ref sig, ref eid) = item. kind {
328
320
let is_public = cx. access_levels . is_exported ( item. hir_id ( ) ) ;
@@ -338,8 +330,6 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
338
330
}
339
331
if let hir:: TraitFn :: Provided ( eid) = * eid {
340
332
let body = cx. tcx . hir ( ) . body ( eid) ;
341
- Self :: check_raw_ptr ( cx, sig. header . unsafety , & sig. decl , body, item. hir_id ( ) ) ;
342
-
343
333
if attr. is_none ( ) && is_public && !is_proc_macro ( cx. sess ( ) , attrs) {
344
334
check_must_use_candidate (
345
335
cx,
@@ -356,35 +346,6 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
356
346
}
357
347
}
358
348
359
- impl < ' tcx > Functions {
360
- fn check_raw_ptr (
361
- cx : & LateContext < ' tcx > ,
362
- unsafety : hir:: Unsafety ,
363
- decl : & ' tcx hir:: FnDecl < ' _ > ,
364
- body : & ' tcx hir:: Body < ' _ > ,
365
- hir_id : hir:: HirId ,
366
- ) {
367
- let expr = & body. value ;
368
- if unsafety == hir:: Unsafety :: Normal && cx. access_levels . is_exported ( hir_id) {
369
- let raw_ptrs = iter_input_pats ( decl, body)
370
- . zip ( decl. inputs . iter ( ) )
371
- . filter_map ( |( arg, ty) | raw_ptr_arg ( arg, ty) )
372
- . collect :: < FxHashSet < _ > > ( ) ;
373
-
374
- if !raw_ptrs. is_empty ( ) {
375
- let typeck_results = cx. tcx . typeck_body ( body. id ( ) ) ;
376
- let mut v = DerefVisitor {
377
- cx,
378
- ptrs : raw_ptrs,
379
- typeck_results,
380
- } ;
381
-
382
- intravisit:: walk_expr ( & mut v, expr) ;
383
- }
384
- }
385
- }
386
- }
387
-
388
349
fn check_result_unit_err ( cx : & LateContext < ' _ > , decl : & hir:: FnDecl < ' _ > , item_span : Span , fn_header_span : Span ) {
389
350
if_chain ! {
390
351
if !in_external_macro( cx. sess( ) , item_span) ;
@@ -524,71 +485,6 @@ fn is_mutable_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Span, tys: &m
524
485
}
525
486
}
526
487
527
- fn raw_ptr_arg ( arg : & hir:: Param < ' _ > , ty : & hir:: Ty < ' _ > ) -> Option < hir:: HirId > {
528
- if let ( & hir:: PatKind :: Binding ( _, id, _, _) , & hir:: TyKind :: Ptr ( _) ) = ( & arg. pat . kind , & ty. kind ) {
529
- Some ( id)
530
- } else {
531
- None
532
- }
533
- }
534
-
535
- struct DerefVisitor < ' a , ' tcx > {
536
- cx : & ' a LateContext < ' tcx > ,
537
- ptrs : FxHashSet < hir:: HirId > ,
538
- typeck_results : & ' a ty:: TypeckResults < ' tcx > ,
539
- }
540
-
541
- impl < ' a , ' tcx > intravisit:: Visitor < ' tcx > for DerefVisitor < ' a , ' tcx > {
542
- type Map = Map < ' tcx > ;
543
-
544
- fn visit_expr ( & mut self , expr : & ' tcx hir:: Expr < ' _ > ) {
545
- match expr. kind {
546
- hir:: ExprKind :: Call ( ref f, args) => {
547
- let ty = self . typeck_results . expr_ty ( f) ;
548
-
549
- if type_is_unsafe_function ( self . cx , ty) {
550
- for arg in args {
551
- self . check_arg ( arg) ;
552
- }
553
- }
554
- } ,
555
- hir:: ExprKind :: MethodCall ( _, _, args, _) => {
556
- let def_id = self . typeck_results . type_dependent_def_id ( expr. hir_id ) . unwrap ( ) ;
557
- let base_type = self . cx . tcx . type_of ( def_id) ;
558
-
559
- if type_is_unsafe_function ( self . cx , base_type) {
560
- for arg in args {
561
- self . check_arg ( arg) ;
562
- }
563
- }
564
- } ,
565
- hir:: ExprKind :: Unary ( hir:: UnOp :: Deref , ref ptr) => self . check_arg ( ptr) ,
566
- _ => ( ) ,
567
- }
568
-
569
- intravisit:: walk_expr ( self , expr) ;
570
- }
571
-
572
- fn nested_visit_map ( & mut self ) -> intravisit:: NestedVisitorMap < Self :: Map > {
573
- intravisit:: NestedVisitorMap :: None
574
- }
575
- }
576
-
577
- impl < ' a , ' tcx > DerefVisitor < ' a , ' tcx > {
578
- fn check_arg ( & self , ptr : & hir:: Expr < ' _ > ) {
579
- if let Some ( id) = path_to_local ( ptr) {
580
- if self . ptrs . contains ( & id) {
581
- span_lint (
582
- self . cx ,
583
- NOT_UNSAFE_PTR_ARG_DEREF ,
584
- ptr. span ,
585
- "this public function dereferences a raw pointer but is not marked `unsafe`" ,
586
- ) ;
587
- }
588
- }
589
- }
590
- }
591
-
592
488
struct StaticMutVisitor < ' a , ' tcx > {
593
489
cx : & ' a LateContext < ' tcx > ,
594
490
mutates_static : bool ,
0 commit comments