diff --git a/clippy_lints/src/functions/not_unsafe_ptr_arg_deref.rs b/clippy_lints/src/functions/not_unsafe_ptr_arg_deref.rs index 6d829a18b2e..834f6d2425e 100644 --- a/clippy_lints/src/functions/not_unsafe_ptr_arg_deref.rs +++ b/clippy_lints/src/functions/not_unsafe_ptr_arg_deref.rs @@ -42,8 +42,7 @@ fn check_raw_ptr<'tcx>( let expr = &body.value; if unsafety == hir::Unsafety::Normal && cx.access_levels.is_exported(def_id) { let raw_ptrs = iter_input_pats(decl, body) - .zip(decl.inputs.iter()) - .filter_map(|(arg, ty)| raw_ptr_arg(arg, ty)) + .filter_map(|arg| raw_ptr_arg(cx, arg)) .collect::(); if !raw_ptrs.is_empty() { @@ -59,8 +58,12 @@ fn check_raw_ptr<'tcx>( } } -fn raw_ptr_arg(arg: &hir::Param<'_>, ty: &hir::Ty<'_>) -> Option { - if let (&hir::PatKind::Binding(_, id, _, _), &hir::TyKind::Ptr(_)) = (&arg.pat.kind, &ty.kind) { +fn raw_ptr_arg(cx: &LateContext<'_>, arg: &hir::Param<'_>) -> Option { + if let (&hir::PatKind::Binding(_, id, _, _), Some(&ty::RawPtr(_))) = ( + &arg.pat.kind, + cx.maybe_typeck_results() + .map(|typeck_results| typeck_results.pat_ty(arg.pat).kind()), + ) { Some(id) } else { None diff --git a/tests/ui/functions.rs b/tests/ui/functions.rs index 271754cb06e..5521870eaec 100644 --- a/tests/ui/functions.rs +++ b/tests/ui/functions.rs @@ -78,6 +78,14 @@ pub fn public(p: *const u8) { unsafe { std::ptr::read(p) }; } +type Alias = *const u8; + +pub fn type_alias(p: Alias) { + println!("{}", unsafe { *p }); + println!("{:?}", unsafe { p.as_ref() }); + unsafe { std::ptr::read(p) }; +} + impl Bar { fn private(self, p: *const u8) { println!("{}", unsafe { *p }); diff --git a/tests/ui/functions.stderr b/tests/ui/functions.stderr index a2b8c2a384b..8ebd4997f4f 100644 --- a/tests/ui/functions.stderr +++ b/tests/ui/functions.stderr @@ -69,22 +69,40 @@ LL | unsafe { std::ptr::read(p) }; | ^ error: this public function might dereference a raw pointer but is not marked `unsafe` - --> $DIR/functions.rs:87:34 + --> $DIR/functions.rs:84:30 + | +LL | println!("{}", unsafe { *p }); + | ^ + +error: this public function might dereference a raw pointer but is not marked `unsafe` + --> $DIR/functions.rs:85:31 + | +LL | println!("{:?}", unsafe { p.as_ref() }); + | ^ + +error: this public function might dereference a raw pointer but is not marked `unsafe` + --> $DIR/functions.rs:86:29 + | +LL | unsafe { std::ptr::read(p) }; + | ^ + +error: this public function might dereference a raw pointer but is not marked `unsafe` + --> $DIR/functions.rs:95:34 | LL | println!("{}", unsafe { *p }); | ^ error: this public function might dereference a raw pointer but is not marked `unsafe` - --> $DIR/functions.rs:88:35 + --> $DIR/functions.rs:96:35 | LL | println!("{:?}", unsafe { p.as_ref() }); | ^ error: this public function might dereference a raw pointer but is not marked `unsafe` - --> $DIR/functions.rs:89:33 + --> $DIR/functions.rs:97:33 | LL | unsafe { std::ptr::read(p) }; | ^ -error: aborting due to 13 previous errors +error: aborting due to 16 previous errors