Skip to content

Commit ecfacb5

Browse files
committed
Remove unnecesary "as" casts.
- "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 <nodakai@gmail.com>
1 parent 8f55218 commit ecfacb5

File tree

23 files changed

+59
-54
lines changed

23 files changed

+59
-54
lines changed

src/bootstrap/build/job.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ use self::kernel32::*;
4949

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

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

9595
let parent = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid.parse().unwrap());
96-
assert!(parent != 0 as *mut _, "{}", io::Error::last_os_error());
97-
let mut parent_handle = 0 as *mut _;
96+
assert!(parent != ptr::null_mut(), "{}", io::Error::last_os_error());
97+
let mut parent_handle = ptr::null_mut();
9898
let r = DuplicateHandle(GetCurrentProcess(), job,
9999
parent, &mut parent_handle,
100100
0, FALSE, DUPLICATE_SAME_ACCESS);

src/libarena/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<T> TypedArenaChunk<T> {
106106
unsafe {
107107
if mem::size_of::<T>() == 0 {
108108
// A pointer as large as possible for zero-sized elements.
109-
!0 as *mut T
109+
(!0usize) as *mut T
110110
} else {
111111
self.start().offset(self.storage.cap() as isize)
112112
}

src/librustc_back/dynamic_lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ mod dl {
276276
None => {
277277
let mut handle = ptr::null_mut();
278278
let succeeded = unsafe {
279-
GetModuleHandleExW(0 as DWORD, ptr::null(), &mut handle)
279+
GetModuleHandleExW(0, ptr::null(), &mut handle)
280280
};
281281
if succeeded == 0 {
282282
Err(io::Error::last_os_error().to_string())

src/librustc_trans/base.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ use libc::c_uint;
9999
use std::ffi::{CStr, CString};
100100
use std::cell::{Cell, RefCell};
101101
use std::collections::{HashMap, HashSet};
102-
use std::str;
102+
use std::{str, ptr};
103103
use std::{i8, i16, i32, i64};
104104
use syntax::codemap::{Span, DUMMY_SP};
105105
use syntax::parse::token::InternedString;
@@ -2409,7 +2409,7 @@ pub fn create_entry_wrapper(ccx: &CrateContext, sp: Span, main_llfn: ValueRef) {
24092409
(start_fn, args)
24102410
} else {
24112411
debug!("using user-defined start fn");
2412-
let args = vec![get_param(llfn, 0 as c_uint), get_param(llfn, 1 as c_uint)];
2412+
let args = vec![get_param(llfn, 0), get_param(llfn, 1)];
24132413

24142414
(rust_main, args)
24152415
};
@@ -2418,7 +2418,7 @@ pub fn create_entry_wrapper(ccx: &CrateContext, sp: Span, main_llfn: ValueRef) {
24182418
start_fn,
24192419
args.as_ptr(),
24202420
args.len() as c_uint,
2421-
0 as *mut _,
2421+
ptr::null_mut(),
24222422
noname());
24232423

24242424
llvm::LLVMBuildRet(bld, result);

src/librustc_trans/builder.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
176176
.collect::<Vec<String>>()
177177
.join(", "));
178178

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

181181
unsafe {
182182
llvm::LLVMRustBuildInvoke(self.llbuilder,
@@ -879,7 +879,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
879879
}
880880
}
881881

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

884884
unsafe {
885885
llvm::LLVMRustBuildCall(self.llbuilder, llfn, args.as_ptr(),
@@ -981,7 +981,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
981981
self.count_insn("trap");
982982
llvm::LLVMRustBuildCall(self.llbuilder, t,
983983
args.as_ptr(), args.len() as c_uint,
984-
0 as *mut _,
984+
ptr::null_mut(),
985985
noname());
986986
}
987987
}
@@ -1020,7 +1020,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
10201020
parent: Option<ValueRef>,
10211021
args: &[ValueRef]) -> ValueRef {
10221022
self.count_insn("cleanuppad");
1023-
let parent = parent.unwrap_or(0 as *mut _);
1023+
let parent = parent.unwrap_or(ptr::null_mut());
10241024
let name = CString::new("cleanuppad").unwrap();
10251025
let ret = unsafe {
10261026
llvm::LLVMRustBuildCleanupPad(self.llbuilder,
@@ -1036,7 +1036,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
10361036
pub fn cleanup_ret(&self, cleanup: ValueRef,
10371037
unwind: Option<BasicBlockRef>) -> ValueRef {
10381038
self.count_insn("cleanupret");
1039-
let unwind = unwind.unwrap_or(0 as *mut _);
1039+
let unwind = unwind.unwrap_or(ptr::null_mut());
10401040
let ret = unsafe {
10411041
llvm::LLVMRustBuildCleanupRet(self.llbuilder, cleanup, unwind)
10421042
};
@@ -1072,8 +1072,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
10721072
unwind: Option<BasicBlockRef>,
10731073
num_handlers: usize) -> ValueRef {
10741074
self.count_insn("catchswitch");
1075-
let parent = parent.unwrap_or(0 as *mut _);
1076-
let unwind = unwind.unwrap_or(0 as *mut _);
1075+
let parent = parent.unwrap_or(ptr::null_mut());
1076+
let unwind = unwind.unwrap_or(ptr::null_mut());
10771077
let name = CString::new("catchswitch").unwrap();
10781078
let ret = unsafe {
10791079
llvm::LLVMRustBuildCatchSwitch(self.llbuilder, parent, unwind,

src/librustc_trans/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ impl<'tcx> LocalCrateContext<'tcx> {
552552
CrateContext {
553553
shared: shared,
554554
local: self,
555-
index: !0 as usize,
555+
index: !0,
556556
}
557557
}
558558
}

src/librustc_trans/tvec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ fn iter_vec_loop<'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
336336
Br(bcx, loop_bcx.llbb, DebugLoc::None);
337337

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

341341
let bcx = loop_bcx;
342342

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

352352
let cond_val = ICmp(bcx, llvm::IntULT, plusone, count, DebugLoc::None);

src/libstd/io/cursor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ mod tests {
324324

325325
#[test]
326326
fn test_buf_writer() {
327-
let mut buf = [0 as u8; 9];
327+
let mut buf = [0u8; 9];
328328
{
329329
let mut writer = Cursor::new(&mut buf[..]);
330330
assert_eq!(writer.position(), 0);
@@ -345,7 +345,7 @@ mod tests {
345345

346346
#[test]
347347
fn test_buf_writer_seek() {
348-
let mut buf = [0 as u8; 8];
348+
let mut buf = [0u8; 8];
349349
{
350350
let mut writer = Cursor::new(&mut buf[..]);
351351
assert_eq!(writer.position(), 0);
@@ -374,7 +374,7 @@ mod tests {
374374

375375
#[test]
376376
fn test_buf_writer_error() {
377-
let mut buf = [0 as u8; 2];
377+
let mut buf = [0u8; 2];
378378
let mut writer = Cursor::new(&mut buf[..]);
379379
assert_eq!(writer.write(&[0]).unwrap(), 1);
380380
assert_eq!(writer.write(&[0, 0]).unwrap(), 1);

src/libstd/sync/once.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
use marker;
6868
use sync::atomic::{AtomicUsize, AtomicBool, Ordering};
6969
use thread::{self, Thread};
70+
use ptr;
7071

7172
/// A synchronization primitive which can be used to run a one-time global
7273
/// initialization. Useful for one-time initialization for FFI or related
@@ -298,7 +299,7 @@ impl Once {
298299
let mut node = Waiter {
299300
thread: Some(thread::current()),
300301
signaled: AtomicBool::new(false),
301-
next: 0 as *mut Waiter,
302+
next: ptr::null_mut(),
302303
};
303304
let me = &mut node as *mut Waiter as usize;
304305
assert!(me & STATE_MASK == 0);

src/libstd/sys/common/dwarf/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl DwarfReader {
7777
}
7878
// sign-extend
7979
if shift < 8 * mem::size_of::<u64>() && (byte & 0x40) != 0 {
80-
result |= (!0 as u64) << shift;
80+
result |= (!0u64) << shift;
8181
}
8282
result as i64
8383
}

src/libstd/sys/common/unwind/gcc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use prelude::v1::*;
1414

1515
use any::Any;
1616
use sys_common::libunwind as uw;
17+
use ptr;
1718

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

4445
pub fn payload() -> *mut u8 {
45-
0 as *mut u8
46+
ptr::null_mut()
4647
}
4748

4849
pub unsafe fn cleanup(ptr: *mut u8) -> Box<Any + Send + 'static> {

src/libstd/sys/common/unwind/seh64_gnu.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub unsafe fn panic(data: Box<Any + Send + 'static>) -> ! {
5151
}
5252

5353
pub fn payload() -> *mut u8 {
54-
0 as *mut u8
54+
ptr::null_mut()
5555
}
5656

5757
pub unsafe fn cleanup(ptr: *mut u8) -> Box<Any + Send + 'static> {

src/libstd/sys/unix/os.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,11 @@ pub fn current_exe() -> io::Result<PathBuf> {
227227
libc::KERN_PROC_ARGV];
228228
let mib = mib.as_mut_ptr();
229229
let mut argv_len = 0;
230-
cvt(libc::sysctl(mib, 4, 0 as *mut _, &mut argv_len,
231-
0 as *mut _, 0))?;
230+
cvt(libc::sysctl(mib, 4, ptr::null_mut(), &mut argv_len,
231+
ptr::null_mut(), 0))?;
232232
let mut argv = Vec::<*const libc::c_char>::with_capacity(argv_len as usize);
233233
cvt(libc::sysctl(mib, 4, argv.as_mut_ptr() as *mut _,
234-
&mut argv_len, 0 as *mut _, 0))?;
234+
&mut argv_len, ptr::null_mut(), 0))?;
235235
argv.set_len(argv_len as usize);
236236
if argv[0].is_null() {
237237
return Err(io::Error::new(io::ErrorKind::Other,

src/libstd/sys/unix/pipe.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use cmp;
1414
use io;
1515
use libc::{self, c_int};
1616
use mem;
17+
use ptr;
1718
use sys::cvt_r;
1819
use sys::fd::FileDesc;
1920

@@ -91,8 +92,8 @@ pub fn read2(p1: AnonPipe,
9192
let mut read: libc::fd_set = mem::zeroed();
9293
libc::FD_SET(p1.raw(), &mut read);
9394
libc::FD_SET(p2.raw(), &mut read);
94-
libc::select(max + 1, &mut read, 0 as *mut _, 0 as *mut _,
95-
0 as *mut _)
95+
libc::select(max + 1, &mut read, ptr::null_mut(), ptr::null_mut(),
96+
ptr::null_mut())
9697
})?;
9798

9899
// Read as much as we can from each pipe, ignoring EWOULDBLOCK or

src/libstd/sys/unix/process.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl Command {
9797
let mut saw_nul = false;
9898
let program = os2c(program, &mut saw_nul);
9999
Command {
100-
argv: vec![program.as_ptr(), 0 as *const _],
100+
argv: vec![program.as_ptr(), ptr::null()],
101101
program: program,
102102
args: Vec::new(),
103103
env: None,
@@ -119,7 +119,7 @@ impl Command {
119119
// pointer.
120120
let arg = os2c(arg, &mut self.saw_nul);
121121
self.argv[self.args.len() + 1] = arg.as_ptr();
122-
self.argv.push(0 as *const _);
122+
self.argv.push(ptr::null());
123123

124124
// Also make sure we keep track of the owned value to schedule a
125125
// destructor for this memory.
@@ -136,7 +136,7 @@ impl Command {
136136
envp.push(s.as_ptr());
137137
map.insert(k, (envp.len() - 1, s));
138138
}
139-
envp.push(0 as *const _);
139+
envp.push(ptr::null());
140140
self.env = Some(map);
141141
self.envp = Some(envp);
142142
}
@@ -160,7 +160,7 @@ impl Command {
160160
Entry::Vacant(e) => {
161161
let len = envp.len();
162162
envp[len - 1] = new_key.as_ptr();
163-
envp.push(0 as *const _);
163+
envp.push(ptr::null());
164164
e.insert((len - 1, new_key));
165165
}
166166
}
@@ -185,7 +185,7 @@ impl Command {
185185

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

191191
pub fn cwd(&mut self, dir: &OsStr) {
@@ -588,7 +588,7 @@ impl Process {
588588
if let Some(status) = self.status {
589589
return Ok(status)
590590
}
591-
let mut status = 0 as c_int;
591+
let mut status: c_int = 0;
592592
cvt_r(|| unsafe { libc::waitpid(self.pid, &mut status, 0) })?;
593593
self.status = Some(ExitStatus(status));
594594
Ok(ExitStatus(status))

src/libstd/sys/unix/time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ mod inner {
7979
},
8080
};
8181
cvt(unsafe {
82-
libc::gettimeofday(&mut s.t, 0 as *mut _)
82+
libc::gettimeofday(&mut s.t, ptr::null_mut())
8383
}).unwrap();
8484
return s
8585
}

src/libstd/sys/windows/c.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ pub const ERROR_OPERATION_ABORTED: DWORD = 995;
191191
pub const ERROR_IO_PENDING: DWORD = 997;
192192
pub const ERROR_TIMEOUT: DWORD = 0x5B4;
193193

194-
pub const INVALID_HANDLE_VALUE: HANDLE = !0 as HANDLE;
194+
pub const INVALID_HANDLE_VALUE: HANDLE = (!0usize) as HANDLE;
195195

196196
pub const FORMAT_MESSAGE_FROM_SYSTEM: DWORD = 0x00001000;
197197
pub const FORMAT_MESSAGE_IGNORE_INSERTS: DWORD = 0x00000200;

src/libstd/sys/windows/handle.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ impl Handle {
4646

4747
pub fn new_event(manual: bool, init: bool) -> io::Result<Handle> {
4848
unsafe {
49-
let event = c::CreateEventW(0 as *mut _,
49+
let event = c::CreateEventW(ptr::null_mut(),
5050
manual as c::BOOL,
5151
init as c::BOOL,
52-
0 as *const _);
52+
ptr::null());
5353
if event.is_null() {
5454
Err(io::Error::last_os_error())
5555
} else {

src/libstd/sys/windows/os.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn errno() -> i32 {
3838
pub fn error_string(errnum: i32) -> String {
3939
// This value is calculated from the macro
4040
// MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT)
41-
let langId = 0x0800 as c::DWORD;
41+
let langId: c::DWORD = 0x0800;
4242

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

src/libstd/sys/windows/pipe.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use ffi::OsStr;
1515
use path::Path;
1616
use io;
1717
use mem;
18+
use ptr;
1819
use rand::{self, Rng};
1920
use slice;
2021
use sys::c;
@@ -66,7 +67,7 @@ pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
6667
4096,
6768
4096,
6869
0,
69-
0 as *mut _);
70+
ptr::null_mut());
7071

7172
// We pass the FILE_FLAG_FIRST_PIPE_INSTANCE flag above, and we're
7273
// also just doing a best effort at selecting a unique name. If

src/libstd/sys/windows/rand.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use io;
1313
use mem;
14+
use ptr;
1415
use rand::Rng;
1516
use sys::c;
1617

@@ -23,7 +24,7 @@ impl OsRng {
2324
pub fn new() -> io::Result<OsRng> {
2425
let mut hcp = 0;
2526
let ret = unsafe {
26-
c::CryptAcquireContextA(&mut hcp, 0 as c::LPCSTR, 0 as c::LPCSTR,
27+
c::CryptAcquireContextA(&mut hcp, ptr::null(), ptr::null(),
2728
c::PROV_RSA_FULL,
2829
c::CRYPT_VERIFYCONTEXT | c::CRYPT_SILENT)
2930
};

0 commit comments

Comments
 (0)