-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Make use of ptr::null(_mut)
instead of casting zero
#61864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
3666052
to
b652879
Compare
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
src/test/ui/fat-ptr-cast.rs
Outdated
let t: *mut (dyn Trait + 'static) = 0 as *mut _; //~ ERROR casting | ||
let mut fail: *const str = 0 as *const str; //~ ERROR casting | ||
|
||
let x: *mut (dyn Trait + 'static) = std::ptr::null_mut(); | ||
//~^ ERROR E0277 |
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.
cc #61870
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.
Might be best to leave this one unchanged if it breaks the test.
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.
Okey thanks! I removed it.
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
Mark as ready for review as I don't know what to do with #61864 (comment) . |
92db647
to
ae0c043
Compare
ptr::{null,null_mut}
instead of casting zeroptr::null(mut)
instead of casting zero
ptr::null(mut)
instead of casting zeroptr::null(_mut)
instead of casting zero
The build is green. |
@bors r+ |
📌 Commit 7d69d4c has been approved by |
Make use of `ptr::null(_mut)` instead of casting zero There are few places that I don't replace the zero casting pointer with `ptr::null` or `ptr::null_mut`: ```bash % git grep -E '[ ([{]0 as \*' src/libcore/ptr/mod.rs:216:pub const fn null<T>() -> *const T { 0 as *const T } src/libcore/ptr/mod.rs:231:pub const fn null_mut<T>() -> *mut T { 0 as *mut T } src/test/run-pass/consts/const-cast-ptr-int.rs:12:static a: TestStruct = TestStruct{x: 0 as *const u8}; src/test/ui/issues/issue-45730.rs:5: let x: *const _ = 0 as *const _; //~ ERROR cannot cast src/test/ui/issues/issue-45730.rs:8: let x = 0 as *const i32 as *const _ as *mut _; //~ ERROR cannot cast src/test/ui/issues/issue-45730.stderr:14:LL | let x: *const _ = 0 as *const _; src/test/ui/issues/issue-45730.stderr:24:LL | let x = 0 as *const i32 as *const _ as *mut _; src/test/ui/lint/lint-forbid-internal-unsafe.rs:15: println!("{}", evil!(*(0 as *const u8))); src/test/ui/order-dependent-cast-inference.rs:5: let mut y = 0 as *const _; src/test/ui/order-dependent-cast-inference.stderr:4:LL | let mut y = 0 as *const _; ``` r? @sfackler
☀️ Test successful - checks-travis, status-appveyor |
Tested on commit rust-lang/rust@a6a8f6c. Direct link to PR: <rust-lang/rust#61864> 🎉 rls on linux: test-fail → test-pass (cc @Xanewok, @rust-lang/infra).
@@ -3,5 +3,5 @@ struct Lorem { | |||
} | |||
|
|||
fn main() { | |||
let _foo: *mut Lorem = 0 as *mut _; // no error here |
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.
…crum Restore a test that was intended to test `as` cast to ptr This test was changed in rust-lang#61864, but the original bug rust-lang#46365 was about casts so I doubt the changed test still even tests what this was intended to test. Let's restore the original test.
…crum Restore a test that was intended to test `as` cast to ptr This test was changed in rust-lang#61864, but the original bug rust-lang#46365 was about casts so I doubt the changed test still even tests what this was intended to test. Let's restore the original test.
There are few places that I don't replace the zero casting pointer with
ptr::null
or
ptr::null_mut
:r? @sfackler