]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/functions/not_unsafe_ptr_arg_deref.rs
Auto merge of #8794 - smoelius:fix-8759, r=llogiq
[rust.git] / clippy_lints / src / functions / not_unsafe_ptr_arg_deref.rs
1 use rustc_hir::{self as hir, intravisit, HirIdSet};
2 use rustc_lint::LateContext;
3 use rustc_middle::ty;
4 use rustc_span::def_id::LocalDefId;
5
6 use clippy_utils::diagnostics::span_lint;
7 use clippy_utils::ty::type_is_unsafe_function;
8 use clippy_utils::{iter_input_pats, path_to_local};
9
10 use super::NOT_UNSAFE_PTR_ARG_DEREF;
11
12 pub(super) fn check_fn<'tcx>(
13     cx: &LateContext<'tcx>,
14     kind: intravisit::FnKind<'tcx>,
15     decl: &'tcx hir::FnDecl<'tcx>,
16     body: &'tcx hir::Body<'tcx>,
17     hir_id: hir::HirId,
18 ) {
19     let unsafety = match kind {
20         intravisit::FnKind::ItemFn(_, _, hir::FnHeader { unsafety, .. }) => unsafety,
21         intravisit::FnKind::Method(_, sig) => sig.header.unsafety,
22         intravisit::FnKind::Closure => return,
23     };
24
25     check_raw_ptr(cx, unsafety, decl, body, cx.tcx.hir().local_def_id(hir_id));
26 }
27
28 pub(super) fn check_trait_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'_>) {
29     if let hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(eid)) = item.kind {
30         let body = cx.tcx.hir().body(eid);
31         check_raw_ptr(cx, sig.header.unsafety, sig.decl, body, item.def_id);
32     }
33 }
34
35 fn check_raw_ptr<'tcx>(
36     cx: &LateContext<'tcx>,
37     unsafety: hir::Unsafety,
38     decl: &'tcx hir::FnDecl<'tcx>,
39     body: &'tcx hir::Body<'tcx>,
40     def_id: LocalDefId,
41 ) {
42     let expr = &body.value;
43     if unsafety == hir::Unsafety::Normal && cx.access_levels.is_exported(def_id) {
44         let raw_ptrs = iter_input_pats(decl, body)
45             .filter_map(|arg| raw_ptr_arg(cx, arg))
46             .collect::<HirIdSet>();
47
48         if !raw_ptrs.is_empty() {
49             let typeck_results = cx.tcx.typeck_body(body.id());
50             let mut v = DerefVisitor {
51                 cx,
52                 ptrs: raw_ptrs,
53                 typeck_results,
54             };
55
56             intravisit::walk_expr(&mut v, expr);
57         }
58     }
59 }
60
61 fn raw_ptr_arg(cx: &LateContext<'_>, arg: &hir::Param<'_>) -> Option<hir::HirId> {
62     if let (&hir::PatKind::Binding(_, id, _, _), Some(&ty::RawPtr(_))) = (
63         &arg.pat.kind,
64         cx.maybe_typeck_results()
65             .map(|typeck_results| typeck_results.pat_ty(arg.pat).kind()),
66     ) {
67         Some(id)
68     } else {
69         None
70     }
71 }
72
73 struct DerefVisitor<'a, 'tcx> {
74     cx: &'a LateContext<'tcx>,
75     ptrs: HirIdSet,
76     typeck_results: &'a ty::TypeckResults<'tcx>,
77 }
78
79 impl<'a, 'tcx> intravisit::Visitor<'tcx> for DerefVisitor<'a, 'tcx> {
80     fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
81         match expr.kind {
82             hir::ExprKind::Call(f, args) => {
83                 let ty = self.typeck_results.expr_ty(f);
84
85                 if type_is_unsafe_function(self.cx, ty) {
86                     for arg in args {
87                         self.check_arg(arg);
88                     }
89                 }
90             },
91             hir::ExprKind::MethodCall(_, args, _) => {
92                 let def_id = self.typeck_results.type_dependent_def_id(expr.hir_id).unwrap();
93                 let base_type = self.cx.tcx.type_of(def_id);
94
95                 if type_is_unsafe_function(self.cx, base_type) {
96                     for arg in args {
97                         self.check_arg(arg);
98                     }
99                 }
100             },
101             hir::ExprKind::Unary(hir::UnOp::Deref, ptr) => self.check_arg(ptr),
102             _ => (),
103         }
104
105         intravisit::walk_expr(self, expr);
106     }
107 }
108
109 impl<'a, 'tcx> DerefVisitor<'a, 'tcx> {
110     fn check_arg(&self, ptr: &hir::Expr<'_>) {
111         if let Some(id) = path_to_local(ptr) {
112             if self.ptrs.contains(&id) {
113                 span_lint(
114                     self.cx,
115                     NOT_UNSAFE_PTR_ARG_DEREF,
116                     ptr.span,
117                     "this public function might dereference a raw pointer but is not marked `unsafe`",
118                 );
119             }
120         }
121     }
122 }