Skip to content

Remove unnecesary "as" casts. #32701

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

Closed
wants to merge 1 commit into from

Conversation

nodakai
Copy link
Contributor

@nodakai nodakai commented Apr 3, 2016

  • 0 as *mut _ -> ptr::null_mut(); and the same goes for ptr::null()
  • Simplify 0 as c_int to 0 when the context already expects c_int
  • And so on...

@rust-highfive
Copy link
Contributor

r? @alexcrichton

(rust_highfive has picked a reviewer for you, use r? to override)

@@ -191,7 +191,7 @@ pub const ERROR_OPERATION_ABORTED: DWORD = 995;
pub const ERROR_IO_PENDING: DWORD = 997;
pub const ERROR_TIMEOUT: DWORD = 0x5B4;

pub const INVALID_HANDLE_VALUE: HANDLE = !0 as HANDLE;
pub const INVALID_HANDLE_VALUE: HANDLE = !(0 as HANDLE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HANDLE is a *mut c_void and you can't apply ! to a pointer.

@nodakai nodakai force-pushed the remove-unnecessary-as-cast branch 2 times, most recently from ee1582a to 7280e4b Compare April 9, 2016 09:26
@nodakai
Copy link
Contributor Author

nodakai commented Apr 10, 2016

Passes "make check" on Linux and on Mingw.

@@ -106,7 +106,7 @@ impl<T> TypedArenaChunk<T> {
unsafe {
if mem::size_of::<T>() == 0 {
// A pointer as large as possible for zero-sized elements.
!0 as *mut T
(!0usize) as *mut T
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come this was changed? Seems slightly less readable now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why the original code did the correct thing (but it did/does). !0 on its own is an i32 (value -1). i32 cast to a pointer is valid, yet this seems to infer using usize.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bluss It appears that !0 as *mut T has a magical power to fix the type of the integer literal to usize. It looks more and more suspicious when we see that LLVM inttoptr is defined as zero extention.

- "0 as *mut _" -> "ptr::null_mut()"; and the same goes for ptr::null()
- Simplify "0 as c_int" to "0" when the context already expects c_int
- And so on...

Signed-off-by: NODA, Kai <[email protected]>
@nodakai nodakai force-pushed the remove-unnecessary-as-cast branch from 7280e4b to ecfacb5 Compare April 27, 2016 02:23
@bors
Copy link
Collaborator

bors commented Apr 28, 2016

☔ The latest upstream changes (presumably #32980) made this pull request unmergeable. Please resolve the merge conflicts.

@alexcrichton
Copy link
Member

Closing due to inactivity, but feel free to resubmit with a rebase!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants