-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Suggest using ptr::null_mut
when user provided ptr::null
to a function expecting ptr::null_mut
#112302
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
r? @TaKO8Ki (rustbot has picked a reviewer for you, use r? to override) |
if let ty::RawPtr(ty::TypeAndMut { mutbl: expected_mutbl, .. }) = expected_ty.kind() | ||
&& let ty::RawPtr(ty::TypeAndMut { mutbl: provided_mutbl, .. }) = provided_ty.kind() | ||
&& let provided_arg_expr = *provided_args[provided_idx] | ||
&& let hir::ExprKind::Call(callee, _) = provided_arg_expr.kind | ||
&& let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = callee.kind | ||
&& let Res::Def(_, def_id) = path.res | ||
{ | ||
match (expected_mutbl, provided_mutbl) { | ||
(hir::Mutability::Mut, hir::Mutability::Not) => { | ||
if let Some(ptr_null_def_id) = self.tcx.get_diagnostic_item(sym::ptr_null) | ||
&& def_id == ptr_null_def_id | ||
{ | ||
// The user provided `ptr::null()`, but the function expects | ||
// `ptr::null_mut()`. | ||
err.subdiagnostic(SuggestPtrNullMut { | ||
span: provided_arg_expr.span | ||
}); | ||
} | ||
}, | ||
_ => {}, | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can simplify the condition quite a lot, maybe something like
if let ty::RawPtr(ty::TypeAndMut { mutbl: expected_mutbl, .. }) = expected_ty.kind() | |
&& let ty::RawPtr(ty::TypeAndMut { mutbl: provided_mutbl, .. }) = provided_ty.kind() | |
&& let provided_arg_expr = *provided_args[provided_idx] | |
&& let hir::ExprKind::Call(callee, _) = provided_arg_expr.kind | |
&& let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = callee.kind | |
&& let Res::Def(_, def_id) = path.res | |
{ | |
match (expected_mutbl, provided_mutbl) { | |
(hir::Mutability::Mut, hir::Mutability::Not) => { | |
if let Some(ptr_null_def_id) = self.tcx.get_diagnostic_item(sym::ptr_null) | |
&& def_id == ptr_null_def_id | |
{ | |
// The user provided `ptr::null()`, but the function expects | |
// `ptr::null_mut()`. | |
err.subdiagnostic(SuggestPtrNullMut { | |
span: provided_arg_expr.span | |
}); | |
} | |
}, | |
_ => {}, | |
} | |
} | |
if let ty::RawPtr(ty::TypeAndMut { mutbl: hir::Mutability::Mut, .. }) = expected_ty.kind() | |
&& let ty::RawPtr(ty::TypeAndMut { mutbl: hir::Mutability::Not, .. }) = provided_ty.kind() | |
&& let provided_arg_expr = *provided_args[provided_idx] | |
&& let hir::ExprKind::Call(callee, _) = provided_arg_expr.kind | |
&& let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = callee.kind | |
&& let Res::Def(_, def_id) = path.res | |
&& Some(def_id) == self.tcx.get_diagnostic_item(sym::ptr_null) | |
{ | |
// The user provided `ptr::null()`, but the function expects | |
// `ptr::null_mut()`. | |
err.subdiagnostic(SuggestPtrNullMut { | |
span: provided_arg_expr.span | |
}); | |
} |
@rustbot ready |
&& *expected_mutbl == hir::Mutability::Mut | ||
&& *provided_mutbl == hir::Mutability::Not |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why you don't want to just ty::RawPtr(ty::TypeAndMut { mutbl: hir::Mutability::Mut, .. })
in the pattern above, as imo it reads quite good: expected_ty is a raw pointer with mut mutability
...
But if you don't like to do it in the pattern, at least use .is_mut()
/.is_not()
and move those two checks to the top, just after the corresponding let raw_ptr = ty.kind()
-ish code.
&& let Some(ptr_null_def_id) = self.tcx.get_diagnostic_item(sym::ptr_null) | ||
&& def_id == ptr_null_def_id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
&& let Some(ptr_null_def_id) = self.tcx.get_diagnostic_item(sym::ptr_null) | |
&& def_id == ptr_null_def_id | |
&& Some(def_id) == self.tcx.get_diagnostic_item(sym::ptr_null) |
or maybe
&& let Some(ptr_null_def_id) = self.tcx.get_diagnostic_item(sym::ptr_null) | |
&& def_id == ptr_null_def_id | |
&& self.tcx.get_diagnostic_item(sym::ptr_null) == Some(def_id) |
…ction expecting `ptr::null_mut`
@bors r+ rollup |
…iaskrgr Rollup of 4 pull requests Successful merges: - rust-lang#112302 (Suggest using `ptr::null_mut` when user provided `ptr::null` to a function expecting `ptr::null_mut`) - rust-lang#112416 (Fix debug ICE for extern type with where clauses) - rust-lang#112527 (Add windows_sys type definitions for ARM32 manually) - rust-lang#112546 (new solver: extend assert to other aliases) r? `@ghost` `@rustbot` modify labels: rollup
Closes #85184.