Suggest using ptr::null_mut when user provided ptr::null to a function expecting ptr::null_mut#112302
Merged
bors merged 1 commit intorust-lang:masterfrom Jun 12, 2023
Merged
Conversation
Collaborator
|
r? @TaKO8Ki (rustbot has picked a reviewer for you, use r? to override) |
WaffleLapkin
reviewed
Jun 8, 2023
Comment on lines
1282
to
1301
Member
There was a problem hiding this comment.
You can simplify the condition quite a lot, maybe something like
Suggested change
| 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 | |
| }); | |
| } |
Member
Author
|
@rustbot ready |
WaffleLapkin
reviewed
Jun 9, 2023
Comment on lines
1296
to
1297
Member
There was a problem hiding this comment.
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.
Comment on lines
1294
to
1295
Member
There was a problem hiding this comment.
Suggested change
| && 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
Suggested change
| && 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`
WaffleLapkin
approved these changes
Jun 12, 2023
Member
|
@bors r+ rollup |
Collaborator
bors
added a commit
to rust-lang-ci/rust
that referenced
this pull request
Jun 12, 2023
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #85184.