Skip to content

Commit 5cada57

Browse files
committed
Auto merge of rust-lang#8273 - SeeSpring:apply_not_unsafe_ptr_arg_deref_to_type_aliases, r=llogiq
Apply `not_unsafe_ptr_arg_deref` to type aliases changelog: Apply [`not_unsafe_ptr_arg_deref`] to type aliases
2 parents 97a5daa + 875b240 commit 5cada57

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

clippy_lints/src/functions/not_unsafe_ptr_arg_deref.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ fn check_raw_ptr<'tcx>(
4242
let expr = &body.value;
4343
if unsafety == hir::Unsafety::Normal && cx.access_levels.is_exported(def_id) {
4444
let raw_ptrs = iter_input_pats(decl, body)
45-
.zip(decl.inputs.iter())
46-
.filter_map(|(arg, ty)| raw_ptr_arg(arg, ty))
45+
.filter_map(|arg| raw_ptr_arg(cx, arg))
4746
.collect::<HirIdSet>();
4847

4948
if !raw_ptrs.is_empty() {
@@ -59,8 +58,12 @@ fn check_raw_ptr<'tcx>(
5958
}
6059
}
6160

62-
fn raw_ptr_arg(arg: &hir::Param<'_>, ty: &hir::Ty<'_>) -> Option<hir::HirId> {
63-
if let (&hir::PatKind::Binding(_, id, _, _), &hir::TyKind::Ptr(_)) = (&arg.pat.kind, &ty.kind) {
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+
) {
6467
Some(id)
6568
} else {
6669
None

tests/ui/functions.rs

+8
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ pub fn public(p: *const u8) {
7878
unsafe { std::ptr::read(p) };
7979
}
8080

81+
type Alias = *const u8;
82+
83+
pub fn type_alias(p: Alias) {
84+
println!("{}", unsafe { *p });
85+
println!("{:?}", unsafe { p.as_ref() });
86+
unsafe { std::ptr::read(p) };
87+
}
88+
8189
impl Bar {
8290
fn private(self, p: *const u8) {
8391
println!("{}", unsafe { *p });

tests/ui/functions.stderr

+22-4
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,40 @@ LL | unsafe { std::ptr::read(p) };
6969
| ^
7070

7171
error: this public function might dereference a raw pointer but is not marked `unsafe`
72-
--> $DIR/functions.rs:87:34
72+
--> $DIR/functions.rs:84:30
73+
|
74+
LL | println!("{}", unsafe { *p });
75+
| ^
76+
77+
error: this public function might dereference a raw pointer but is not marked `unsafe`
78+
--> $DIR/functions.rs:85:31
79+
|
80+
LL | println!("{:?}", unsafe { p.as_ref() });
81+
| ^
82+
83+
error: this public function might dereference a raw pointer but is not marked `unsafe`
84+
--> $DIR/functions.rs:86:29
85+
|
86+
LL | unsafe { std::ptr::read(p) };
87+
| ^
88+
89+
error: this public function might dereference a raw pointer but is not marked `unsafe`
90+
--> $DIR/functions.rs:95:34
7391
|
7492
LL | println!("{}", unsafe { *p });
7593
| ^
7694

7795
error: this public function might dereference a raw pointer but is not marked `unsafe`
78-
--> $DIR/functions.rs:88:35
96+
--> $DIR/functions.rs:96:35
7997
|
8098
LL | println!("{:?}", unsafe { p.as_ref() });
8199
| ^
82100

83101
error: this public function might dereference a raw pointer but is not marked `unsafe`
84-
--> $DIR/functions.rs:89:33
102+
--> $DIR/functions.rs:97:33
85103
|
86104
LL | unsafe { std::ptr::read(p) };
87105
| ^
88106

89-
error: aborting due to 13 previous errors
107+
error: aborting due to 16 previous errors
90108

0 commit comments

Comments
 (0)