Apply not_unsafe_ptr_arg_deref to type aliases

This commit is contained in:
SeeSpring 2022-01-13 09:27:08 -05:00
parent 97a5daa659
commit 875b240083
3 changed files with 37 additions and 8 deletions

View file

@ -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::<HirIdSet>();
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<hir::HirId> {
if let (&hir::PatKind::Binding(_, id, _, _), &hir::TyKind::Ptr(_)) = (&arg.pat.kind, &ty.kind) {
fn raw_ptr_arg(cx: &LateContext<'_>, arg: &hir::Param<'_>) -> Option<hir::HirId> {
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

View file

@ -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 });

View file

@ -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:88:35
--> $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:89:33
--> $DIR/functions.rs:86:29
|
LL | unsafe { std::ptr::read(p) };
| ^
error: aborting due to 13 previous errors
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: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:97:33
|
LL | unsafe { std::ptr::read(p) };
| ^
error: aborting due to 16 previous errors