Skip to content

Commit

Permalink
Auto merge of rust-lang#8273 - SeeSpring:apply_not_unsafe_ptr_arg_der…
Browse files Browse the repository at this point in the history
…ef_to_type_aliases, r=llogiq

Apply `not_unsafe_ptr_arg_deref` to type aliases

changelog: Apply [`not_unsafe_ptr_arg_deref`] to type aliases
  • Loading branch information
bors committed Jan 13, 2022
2 parents 97a5daa + 875b240 commit 5cada57
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
11 changes: 7 additions & 4 deletions clippy_lints/src/functions/not_unsafe_ptr_arg_deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
26 changes: 22 additions & 4 deletions tests/ui/functions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5cada57

Please sign in to comment.