Suggest using `ptr::null_mut` when user provided `ptr::null` to a function expec...
source link: https://github.com/rust-lang/rust/pull/112302
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Suggest using ptr::null_mut
when user provided ptr::null
to a function expecting ptr::null_mut
#112302
Conversation
Contributor
error[E0308]: mismatched types
--> $DIR/ptr-null-mutability-suggestions.rs:9:24
|
LL | expecting_null_mut(ptr::null());
| ------------------ ^^^^^^^^^^^
| | |
| | types differ in mutability
| | help: consider using `core::ptr::null_mut` instead: `core::ptr::null_mut()`
| arguments to this function are incorrect
|
= note: expected raw pointer `*mut u8`
found raw pointer `*const _`
note: function defined here
--> $DIR/ptr-null-mutability-suggestions.rs:6:4
|
LL | fn expecting_null_mut(_: *mut u8) {}
| ^^^^^^^^^^^^^^^^^^ ----------
Closes #85184.
Collaborator
r? @TaKO8Ki (rustbot has picked a reviewer for you, use r? to override) |
added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
labels
Comment on lines
1282 to 1301
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 |
||
}); |
||
} |
||
}, |
||
_ => {}, |
||
} |
||
} |
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 | |
}); | |
} |
added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
labels
Contributor
Author
@rustbot ready |
added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
labels
Comment on lines
1296 to 1297
&& *expected_mutbl == hir::Mutability::Mut |
||
&& *provided_mutbl == hir::Mutability::Not |
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
&& let Some(ptr_null_def_id) = self.tcx.get_diagnostic_item(sym::ptr_null) |
||
&& def_id == ptr_null_def_id |
&& 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) |
Member
@bors r+ rollup |
added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
labels
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Successfully merging this pull request may close these issues.
Suggestion for diagnostic imoprovement in case of null pointer that differs in mutability
Recommend
-
9
Details (Keywords: good-first-bug, Whiteboard: [lang=js]) The ChromeUtils.import(..., null) form of importing modules causes the import t...
-
5
The function always gives me a background `NULL ' advertisements I have writen this function with several test cases: character...
-
11
How can I pass null * array to a C function via ctypes? advertisements I have a C function in a dll file defined as follows: my...
-
7
Copy link Contributor chordtoll
-
4
New issue Do not suggest "is a function" for free variables #93996
-
6
PC recession: Computer sales decline for a second quarter and by more than expected...
-
4
The UK Government Just Decided The Fate Of Loot Boxes And It's Not What We Expected ...
-
7
C-Suite AgendaVeeamON 2022: Veeam Backup & Replication v12 and Other New Products to Expect Soon
-
7
Global NFT (Non-Fungible Token) Market Intelligence Report 2022: Market is Expected to Grow by 51% to Reach US$46,157.4 Million by the End of 2022 Global NFT (Non-Fungible Token) Market...
-
7
Conversation Contributor Curren...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK