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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/bootstrap/build/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ use self::kernel32::*;

pub unsafe fn setup() {
// Create a new job object for us to use
let job = CreateJobObjectW(0 as *mut _, 0 as *const _);
assert!(job != 0 as *mut _, "{}", io::Error::last_os_error());
let job = CreateJobObjectW(ptr::null_mut(), ptr::null());
assert!(job != ptr::null_mut(), "{}", io::Error::last_os_error());

// Indicate that when all handles to the job object are gone that all
// process in the object should be killed. Note that this includes our
Expand Down Expand Up @@ -93,8 +93,8 @@ pub unsafe fn setup() {
};

let parent = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid.parse().unwrap());
assert!(parent != 0 as *mut _, "{}", io::Error::last_os_error());
let mut parent_handle = 0 as *mut _;
assert!(parent != ptr::null_mut(), "{}", io::Error::last_os_error());
let mut parent_handle = ptr::null_mut();
let r = DuplicateHandle(GetCurrentProcess(), job,
parent, &mut parent_handle,
0, FALSE, DUPLICATE_SAME_ACCESS);
Expand Down
2 changes: 1 addition & 1 deletion src/libarena/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.

} else {
self.start().offset(self.storage.cap() as isize)
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_back/dynamic_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ mod dl {
None => {
let mut handle = ptr::null_mut();
let succeeded = unsafe {
GetModuleHandleExW(0 as DWORD, ptr::null(), &mut handle)
GetModuleHandleExW(0, ptr::null(), &mut handle)
};
if succeeded == 0 {
Err(io::Error::last_os_error().to_string())
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ use libc::c_uint;
use std::ffi::{CStr, CString};
use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet};
use std::str;
use std::{str, ptr};
use std::{i8, i16, i32, i64};
use syntax::codemap::{Span, DUMMY_SP};
use syntax::parse::token::InternedString;
Expand Down Expand Up @@ -2409,7 +2409,7 @@ pub fn create_entry_wrapper(ccx: &CrateContext, sp: Span, main_llfn: ValueRef) {
(start_fn, args)
} else {
debug!("using user-defined start fn");
let args = vec![get_param(llfn, 0 as c_uint), get_param(llfn, 1 as c_uint)];
let args = vec![get_param(llfn, 0), get_param(llfn, 1)];

(rust_main, args)
};
Expand All @@ -2418,7 +2418,7 @@ pub fn create_entry_wrapper(ccx: &CrateContext, sp: Span, main_llfn: ValueRef) {
start_fn,
args.as_ptr(),
args.len() as c_uint,
0 as *mut _,
ptr::null_mut(),
noname());

llvm::LLVMBuildRet(bld, result);
Expand Down
14 changes: 7 additions & 7 deletions src/librustc_trans/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
.collect::<Vec<String>>()
.join(", "));

let bundle = bundle.as_ref().map(|b| b.raw()).unwrap_or(0 as *mut _);
let bundle = bundle.as_ref().map(|b| b.raw()).unwrap_or(ptr::null_mut());

unsafe {
llvm::LLVMRustBuildInvoke(self.llbuilder,
Expand Down Expand Up @@ -879,7 +879,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
}

let bundle = bundle.as_ref().map(|b| b.raw()).unwrap_or(0 as *mut _);
let bundle = bundle.as_ref().map(|b| b.raw()).unwrap_or(ptr::null_mut());

unsafe {
llvm::LLVMRustBuildCall(self.llbuilder, llfn, args.as_ptr(),
Expand Down Expand Up @@ -981,7 +981,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
self.count_insn("trap");
llvm::LLVMRustBuildCall(self.llbuilder, t,
args.as_ptr(), args.len() as c_uint,
0 as *mut _,
ptr::null_mut(),
noname());
}
}
Expand Down Expand Up @@ -1020,7 +1020,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
parent: Option<ValueRef>,
args: &[ValueRef]) -> ValueRef {
self.count_insn("cleanuppad");
let parent = parent.unwrap_or(0 as *mut _);
let parent = parent.unwrap_or(ptr::null_mut());
let name = CString::new("cleanuppad").unwrap();
let ret = unsafe {
llvm::LLVMRustBuildCleanupPad(self.llbuilder,
Expand All @@ -1036,7 +1036,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
pub fn cleanup_ret(&self, cleanup: ValueRef,
unwind: Option<BasicBlockRef>) -> ValueRef {
self.count_insn("cleanupret");
let unwind = unwind.unwrap_or(0 as *mut _);
let unwind = unwind.unwrap_or(ptr::null_mut());
let ret = unsafe {
llvm::LLVMRustBuildCleanupRet(self.llbuilder, cleanup, unwind)
};
Expand Down Expand Up @@ -1072,8 +1072,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
unwind: Option<BasicBlockRef>,
num_handlers: usize) -> ValueRef {
self.count_insn("catchswitch");
let parent = parent.unwrap_or(0 as *mut _);
let unwind = unwind.unwrap_or(0 as *mut _);
let parent = parent.unwrap_or(ptr::null_mut());
let unwind = unwind.unwrap_or(ptr::null_mut());
let name = CString::new("catchswitch").unwrap();
let ret = unsafe {
llvm::LLVMRustBuildCatchSwitch(self.llbuilder, parent, unwind,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ impl<'tcx> LocalCrateContext<'tcx> {
CrateContext {
shared: shared,
local: self,
index: !0 as usize,
index: !0,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_trans/tvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ fn iter_vec_loop<'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
Br(bcx, loop_bcx.llbb, DebugLoc::None);

let loop_counter = Phi(loop_bcx, bcx.ccx().int_type(),
&[C_uint(bcx.ccx(), 0 as usize)], &[bcx.llbb]);
&[C_uint(bcx.ccx(), 0u64)], &[bcx.llbb]);

let bcx = loop_bcx;

Expand All @@ -346,7 +346,7 @@ fn iter_vec_loop<'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
InBoundsGEP(bcx, data_ptr, &[loop_counter])
};
let bcx = f(bcx, lleltptr, vt.unit_ty);
let plusone = Add(bcx, loop_counter, C_uint(bcx.ccx(), 1usize), DebugLoc::None);
let plusone = Add(bcx, loop_counter, C_uint(bcx.ccx(), 1u64), DebugLoc::None);
AddIncomingToPhi(loop_counter, plusone, bcx.llbb);

let cond_val = ICmp(bcx, llvm::IntULT, plusone, count, DebugLoc::None);
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/io/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ mod tests {

#[test]
fn test_buf_writer() {
let mut buf = [0 as u8; 9];
let mut buf = [0u8; 9];
{
let mut writer = Cursor::new(&mut buf[..]);
assert_eq!(writer.position(), 0);
Expand All @@ -345,7 +345,7 @@ mod tests {

#[test]
fn test_buf_writer_seek() {
let mut buf = [0 as u8; 8];
let mut buf = [0u8; 8];
{
let mut writer = Cursor::new(&mut buf[..]);
assert_eq!(writer.position(), 0);
Expand Down Expand Up @@ -374,7 +374,7 @@ mod tests {

#[test]
fn test_buf_writer_error() {
let mut buf = [0 as u8; 2];
let mut buf = [0u8; 2];
let mut writer = Cursor::new(&mut buf[..]);
assert_eq!(writer.write(&[0]).unwrap(), 1);
assert_eq!(writer.write(&[0, 0]).unwrap(), 1);
Expand Down
3 changes: 2 additions & 1 deletion src/libstd/sync/once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
use marker;
use sync::atomic::{AtomicUsize, AtomicBool, Ordering};
use thread::{self, Thread};
use ptr;

/// A synchronization primitive which can be used to run a one-time global
/// initialization. Useful for one-time initialization for FFI or related
Expand Down Expand Up @@ -298,7 +299,7 @@ impl Once {
let mut node = Waiter {
thread: Some(thread::current()),
signaled: AtomicBool::new(false),
next: 0 as *mut Waiter,
next: ptr::null_mut(),
};
let me = &mut node as *mut Waiter as usize;
assert!(me & STATE_MASK == 0);
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/common/dwarf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl DwarfReader {
}
// sign-extend
if shift < 8 * mem::size_of::<u64>() && (byte & 0x40) != 0 {
result |= (!0 as u64) << shift;
result |= (!0u64) << shift;
}
result as i64
}
Expand Down
3 changes: 2 additions & 1 deletion src/libstd/sys/common/unwind/gcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use prelude::v1::*;

use any::Any;
use sys_common::libunwind as uw;
use ptr;

struct Exception {
uwe: uw::_Unwind_Exception,
Expand Down Expand Up @@ -42,7 +43,7 @@ pub unsafe fn panic(data: Box<Any + Send + 'static>) -> ! {
}

pub fn payload() -> *mut u8 {
0 as *mut u8
ptr::null_mut()
}

pub unsafe fn cleanup(ptr: *mut u8) -> Box<Any + Send + 'static> {
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/common/unwind/seh64_gnu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub unsafe fn panic(data: Box<Any + Send + 'static>) -> ! {
}

pub fn payload() -> *mut u8 {
0 as *mut u8
ptr::null_mut()
}

pub unsafe fn cleanup(ptr: *mut u8) -> Box<Any + Send + 'static> {
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/sys/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,11 @@ pub fn current_exe() -> io::Result<PathBuf> {
libc::KERN_PROC_ARGV];
let mib = mib.as_mut_ptr();
let mut argv_len = 0;
cvt(libc::sysctl(mib, 4, 0 as *mut _, &mut argv_len,
0 as *mut _, 0))?;
cvt(libc::sysctl(mib, 4, ptr::null_mut(), &mut argv_len,
ptr::null_mut(), 0))?;
let mut argv = Vec::<*const libc::c_char>::with_capacity(argv_len as usize);
cvt(libc::sysctl(mib, 4, argv.as_mut_ptr() as *mut _,
&mut argv_len, 0 as *mut _, 0))?;
&mut argv_len, ptr::null_mut(), 0))?;
argv.set_len(argv_len as usize);
if argv[0].is_null() {
return Err(io::Error::new(io::ErrorKind::Other,
Expand Down
5 changes: 3 additions & 2 deletions src/libstd/sys/unix/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use cmp;
use io;
use libc::{self, c_int};
use mem;
use ptr;
use sys::cvt_r;
use sys::fd::FileDesc;

Expand Down Expand Up @@ -91,8 +92,8 @@ pub fn read2(p1: AnonPipe,
let mut read: libc::fd_set = mem::zeroed();
libc::FD_SET(p1.raw(), &mut read);
libc::FD_SET(p2.raw(), &mut read);
libc::select(max + 1, &mut read, 0 as *mut _, 0 as *mut _,
0 as *mut _)
libc::select(max + 1, &mut read, ptr::null_mut(), ptr::null_mut(),
ptr::null_mut())
})?;

// Read as much as we can from each pipe, ignoring EWOULDBLOCK or
Expand Down
12 changes: 6 additions & 6 deletions src/libstd/sys/unix/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl Command {
let mut saw_nul = false;
let program = os2c(program, &mut saw_nul);
Command {
argv: vec![program.as_ptr(), 0 as *const _],
argv: vec![program.as_ptr(), ptr::null()],
program: program,
args: Vec::new(),
env: None,
Expand All @@ -119,7 +119,7 @@ impl Command {
// pointer.
let arg = os2c(arg, &mut self.saw_nul);
self.argv[self.args.len() + 1] = arg.as_ptr();
self.argv.push(0 as *const _);
self.argv.push(ptr::null());

// Also make sure we keep track of the owned value to schedule a
// destructor for this memory.
Expand All @@ -136,7 +136,7 @@ impl Command {
envp.push(s.as_ptr());
map.insert(k, (envp.len() - 1, s));
}
envp.push(0 as *const _);
envp.push(ptr::null());
self.env = Some(map);
self.envp = Some(envp);
}
Expand All @@ -160,7 +160,7 @@ impl Command {
Entry::Vacant(e) => {
let len = envp.len();
envp[len - 1] = new_key.as_ptr();
envp.push(0 as *const _);
envp.push(ptr::null());
e.insert((len - 1, new_key));
}
}
Expand All @@ -185,7 +185,7 @@ impl Command {

pub fn env_clear(&mut self) {
self.env = Some(HashMap::new());
self.envp = Some(vec![0 as *const _]);
self.envp = Some(vec![ptr::null()]);
}

pub fn cwd(&mut self, dir: &OsStr) {
Expand Down Expand Up @@ -588,7 +588,7 @@ impl Process {
if let Some(status) = self.status {
return Ok(status)
}
let mut status = 0 as c_int;
let mut status: c_int = 0;
cvt_r(|| unsafe { libc::waitpid(self.pid, &mut status, 0) })?;
self.status = Some(ExitStatus(status));
Ok(ExitStatus(status))
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/unix/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ mod inner {
},
};
cvt(unsafe {
libc::gettimeofday(&mut s.t, 0 as *mut _)
libc::gettimeofday(&mut s.t, ptr::null_mut())
}).unwrap();
return s
}
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/windows/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (!0usize) as HANDLE;

pub const FORMAT_MESSAGE_FROM_SYSTEM: DWORD = 0x00001000;
pub const FORMAT_MESSAGE_IGNORE_INSERTS: DWORD = 0x00000200;
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/sys/windows/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ impl Handle {

pub fn new_event(manual: bool, init: bool) -> io::Result<Handle> {
unsafe {
let event = c::CreateEventW(0 as *mut _,
let event = c::CreateEventW(ptr::null_mut(),
manual as c::BOOL,
init as c::BOOL,
0 as *const _);
ptr::null());
if event.is_null() {
Err(io::Error::last_os_error())
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/windows/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn errno() -> i32 {
pub fn error_string(errnum: i32) -> String {
// This value is calculated from the macro
// MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT)
let langId = 0x0800 as c::DWORD;
let langId: c::DWORD = 0x0800;

let mut buf = [0 as c::WCHAR; 2048];

Expand Down
3 changes: 2 additions & 1 deletion src/libstd/sys/windows/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use ffi::OsStr;
use path::Path;
use io;
use mem;
use ptr;
use rand::{self, Rng};
use slice;
use sys::c;
Expand Down Expand Up @@ -66,7 +67,7 @@ pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
4096,
4096,
0,
0 as *mut _);
ptr::null_mut());

// We pass the FILE_FLAG_FIRST_PIPE_INSTANCE flag above, and we're
// also just doing a best effort at selecting a unique name. If
Expand Down
3 changes: 2 additions & 1 deletion src/libstd/sys/windows/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use io;
use mem;
use ptr;
use rand::Rng;
use sys::c;

Expand All @@ -23,7 +24,7 @@ impl OsRng {
pub fn new() -> io::Result<OsRng> {
let mut hcp = 0;
let ret = unsafe {
c::CryptAcquireContextA(&mut hcp, 0 as c::LPCSTR, 0 as c::LPCSTR,
c::CryptAcquireContextA(&mut hcp, ptr::null(), ptr::null(),
c::PROV_RSA_FULL,
c::CRYPT_VERIFYCONTEXT | c::CRYPT_SILENT)
};
Expand Down
Loading