Skip to content

Make all coercion valid casts and add trivial cast lints #23630

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

Merged
merged 6 commits into from
Mar 25, 2015
Merged
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
3 changes: 2 additions & 1 deletion src/libarena/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,8 @@ impl<T> TypedArenaChunk<T> {
// Destroy the next chunk.
let next = self.next;
let size = calculate_size::<T>(self.capacity);
deallocate(self as *mut TypedArenaChunk<T> as *mut u8, size,
let self_ptr: *mut TypedArenaChunk<T> = self;
deallocate(self_ptr as *mut u8, size,
mem::min_align_of::<TypedArenaChunk<T>>());
if !next.is_null() {
let capacity = (*next).capacity;
Expand Down
2 changes: 2 additions & 0 deletions src/libcollections/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
html_playground_url = "http://play.rust-lang.org/")]
#![doc(test(no_crate_inject))]

#![allow(trivial_casts)]
#![allow(trivial_numeric_casts)]
#![feature(alloc)]
#![feature(box_syntax)]
#![feature(box_patterns)]
Expand Down
4 changes: 2 additions & 2 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1199,8 +1199,8 @@ impl<T: PartialEq> Vec<T> {

// Avoid bounds checks by using unsafe pointers.
let p = self.as_mut_ptr();
let mut r = 1;
let mut w = 1;
let mut r: usize = 1;
let mut w: usize = 1;

while r < ln {
let p_r = p.offset(r as isize);
Expand Down
6 changes: 5 additions & 1 deletion src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,11 @@ impl<T> UnsafeCell<T> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get(&self) -> *mut T { &self.value as *const T as *mut T }
pub fn get(&self) -> *mut T {
// FIXME(#23542) Replace with type ascription.
#![allow(trivial_casts)]
&self.value as *const T as *mut T
}

/// Unwraps the value
///
Expand Down
6 changes: 6 additions & 0 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,20 +833,26 @@ impl<T> Pointer for *const T {
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Pointer for *mut T {
fn fmt(&self, f: &mut Formatter) -> Result {
// FIXME(#23542) Replace with type ascription.
#![allow(trivial_casts)]
Pointer::fmt(&(*self as *const T), f)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> Pointer for &'a T {
fn fmt(&self, f: &mut Formatter) -> Result {
// FIXME(#23542) Replace with type ascription.
#![allow(trivial_casts)]
Pointer::fmt(&(*self as *const T), f)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> Pointer for &'a mut T {
fn fmt(&self, f: &mut Formatter) -> Result {
// FIXME(#23542) Replace with type ascription.
#![allow(trivial_casts)]
Pointer::fmt(&(&**self as *const T), f)
}
}
Expand Down
1 change: 1 addition & 0 deletions src/libcore/fmt/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// FIXME: #6220 Implement floating point formatting

#![allow(unsigned_negation)]
#![allow(trivial_numeric_casts)]

use fmt;
use iter::IteratorExt;
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ mod impls {
}

fn hash_slice<H: Hasher>(data: &[$ty], state: &mut H) {
// FIXME(#23542) Replace with type ascription.
#![allow(trivial_casts)]
let newlen = data.len() * ::$ty::BYTES as usize;
let ptr = data.as_ptr() as *const u8;
state.write(unsafe { slice::from_raw_parts(ptr, newlen) })
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ pub fn drop<T>(_x: T) { }
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
// FIXME(#23542) Replace with type ascription.
#![allow(trivial_casts)]
ptr::read(src as *const T as *const U)
}

Expand Down
1 change: 1 addition & 0 deletions src/libcore/num/i16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@

#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "i16")]
#![allow(trivial_numeric_casts)]

int_module! { i16, 16 }
1 change: 1 addition & 0 deletions src/libcore/num/i32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@

#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "i32")]
#![allow(trivial_numeric_casts)]

int_module! { i32, 32 }
1 change: 1 addition & 0 deletions src/libcore/num/i64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@

#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "i64")]
#![allow(trivial_numeric_casts)]

int_module! { i64, 64 }
1 change: 1 addition & 0 deletions src/libcore/num/i8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@

#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "i8")]
#![allow(trivial_numeric_casts)]

int_module! { i8, 8 }
1 change: 1 addition & 0 deletions src/libcore/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

#![doc(hidden)]
#![allow(trivial_numeric_casts)]

macro_rules! int_module { ($T:ty, $bits:expr) => (

Expand Down
1 change: 1 addition & 0 deletions src/libcore/num/isize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "isize")]
#![allow(trivial_numeric_casts)]

#[cfg(target_pointer_width = "32")]
int_module! { isize, 32 }
Expand Down
1 change: 1 addition & 0 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#![stable(feature = "rust1", since = "1.0.0")]
#![allow(missing_docs)]
#![allow(trivial_numeric_casts)]

use self::wrapping::{OverflowingOps, WrappingOps};

Expand Down
1 change: 1 addition & 0 deletions src/libcore/num/u16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@

#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "u16")]
#![allow(trivial_numeric_casts)]

uint_module! { u16, i16, 16 }
1 change: 1 addition & 0 deletions src/libcore/num/u32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@

#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "u32")]
#![allow(trivial_numeric_casts)]

uint_module! { u32, i32, 32 }
1 change: 1 addition & 0 deletions src/libcore/num/u64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@

#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "u64")]
#![allow(trivial_numeric_casts)]

uint_module! { u64, i64, 64 }
1 change: 1 addition & 0 deletions src/libcore/num/u8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@

#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "u8")]
#![allow(trivial_numeric_casts)]

uint_module! { u8, i8, 8 }
1 change: 1 addition & 0 deletions src/libcore/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

#![doc(hidden)]
#![allow(trivial_numeric_casts)]

macro_rules! uint_module { ($T:ty, $T_SIGNED:ty, $bits:expr) => (

Expand Down
1 change: 1 addition & 0 deletions src/libcore/num/usize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@

#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "usize")]
#![allow(trivial_numeric_casts)]

uint_module! { usize, isize, ::isize::BITS }
2 changes: 1 addition & 1 deletion src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ impl<T: ?Sized> Unique<T> {
/// Create a new `Unique`.
#[unstable(feature = "unique")]
pub unsafe fn new(ptr: *mut T) -> Unique<T> {
Unique { pointer: NonZero::new(ptr as *const T), _marker: PhantomData }
Unique { pointer: NonZero::new(ptr), _marker: PhantomData }
}

/// Dereference the content.
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ pub unsafe fn from_utf8_unchecked<'a>(v: &'a [u8]) -> &'a str {
reason = "use std::ffi::c_str_to_bytes + str::from_utf8")]
pub unsafe fn from_c_str(s: *const i8) -> &'static str {
let s = s as *const u8;
let mut len = 0;
let mut len: usize = 0;
while *s.offset(len as isize) != 0 {
len += 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcoretest/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn test_transmute() {
trait Foo { fn dummy(&self) { } }
impl Foo for int {}

let a = box 100 as Box<Foo>;
let a = box 100isize as Box<Foo>;
unsafe {
let x: ::core::raw::TraitObject = transmute(a);
assert!(*(x.data as *const int) == 100);
Expand Down
14 changes: 7 additions & 7 deletions src/libcoretest/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ fn test_as_ref() {
assert_eq!(q.as_ref().unwrap(), &2);

// Lifetime inference
let u = 2;
let u = 2isize;
{
let p: *const int = &u as *const _;
let p = &u as *const int;
assert_eq!(p.as_ref().unwrap(), &2);
}
}
Expand All @@ -102,9 +102,9 @@ fn test_as_mut() {
assert!(q.as_mut().unwrap() == &mut 2);

// Lifetime inference
let mut u = 2;
let mut u = 2isize;
{
let p: *mut int = &mut u as *mut _;
let p = &mut u as *mut int;
assert!(p.as_mut().unwrap() == &mut 2);
}
}
Expand Down Expand Up @@ -170,9 +170,9 @@ fn test_set_memory() {

#[test]
fn test_unsized_unique() {
let xs: &mut [_] = &mut [1, 2, 3];
let ptr = unsafe { Unique::new(xs as *mut [_]) };
let xs: &mut [i32] = &mut [1, 2, 3];
let ptr = unsafe { Unique::new(xs as *mut [i32]) };
let ys = unsafe { &mut **ptr };
let zs: &mut [_] = &mut [1, 2, 3];
let zs: &mut [i32] = &mut [1, 2, 3];
assert!(ys == zs);
}
4 changes: 2 additions & 2 deletions src/liblog/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@ pub fn log(level: u32, loc: &'static LogLocation, args: fmt::Arguments) {
// Completely remove the local logger from TLS in case anyone attempts to
// frob the slot while we're doing the logging. This will destroy any logger
// set during logging.
let mut logger = LOCAL_LOGGER.with(|s| {
let mut logger: Box<Logger + Send> = LOCAL_LOGGER.with(|s| {
s.borrow_mut().take()
}).unwrap_or_else(|| {
box DefaultLogger { handle: io::stderr() } as Box<Logger + Send>
box DefaultLogger { handle: io::stderr() }
});
logger.log(&LogRecord {
level: LogLevel(level),
Expand Down
2 changes: 2 additions & 0 deletions src/librand/distributions/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

//! Generating numbers between two others.

#![allow(trivial_numeric_casts)]

// this is surprisingly complicated to be both generic & correct

use core::prelude::{PartialOrd};
Expand Down
1 change: 1 addition & 0 deletions src/librand/isaac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ impl Rng for Isaac64Rng {

#[inline]
fn next_u64(&mut self) -> u64 {
#![allow(trivial_numeric_casts)]
if self.cnt == 0 {
// make some more numbers
self.isaac64();
Expand Down
4 changes: 2 additions & 2 deletions src/librbml/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ pub mod reader {
let (shift, mask) = SHIFT_MASK_TABLE[i];
Ok(Res {
val: ((val >> shift) & mask) as uint,
next: start + (((32 - shift) >> 3) as uint)
next: start + ((32 - shift) >> 3),
})
}
}
Expand Down Expand Up @@ -573,7 +573,7 @@ pub mod reader {
0 => doc_as_u8(r_doc) as u64,
1 => doc_as_u16(r_doc) as u64,
2 => doc_as_u32(r_doc) as u64,
3 => doc_as_u64(r_doc) as u64,
3 => doc_as_u64(r_doc),
_ => unreachable!(),
}
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
#![feature(into_cow)]
#![cfg_attr(test, feature(test))]

#![allow(trivial_casts)]
#![allow(trivial_numeric_casts)]

extern crate arena;
extern crate flate;
extern crate fmt_macros;
Expand Down
15 changes: 14 additions & 1 deletion src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ declare_lint! {
"detects transmutes of fat pointers"
}

declare_lint! {
pub TRIVIAL_CASTS,
Warn,
"detects trivial casts which could be removed"
}

declare_lint! {
pub TRIVIAL_NUMERIC_CASTS,
Warn,
"detects trivial casts of numeric types which could be removed"
}
/// Does nothing as a lint pass, but registers some `Lint`s
/// which are used by other parts of the compiler.
#[derive(Copy)]
Expand All @@ -121,7 +132,9 @@ impl LintPass for HardwiredLints {
STABLE_FEATURES,
UNKNOWN_CRATE_TYPES,
VARIANT_SIZE_DIFFERENCES,
FAT_PTR_TRANSMUTES
FAT_PTR_TRANSMUTES,
TRIVIAL_CASTS,
TRIVIAL_NUMERIC_CASTS
)
}
}
Loading