Skip to content

Commit bc395bc

Browse files
committed
auto merge of #11329 : fhahn/rust/unused-cast-lint2, r=alexcrichton
Updates as mentioned in #11135
2 parents 777f1e8 + 8236550 commit bc395bc

File tree

15 files changed

+93
-26
lines changed

15 files changed

+93
-26
lines changed

src/libextra/bitv.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl SmallBitv {
8080
self.bits |= 1<<i;
8181
}
8282
else {
83-
self.bits &= !(1<<i as uint);
83+
self.bits &= !(1<<i);
8484
}
8585
}
8686

src/libextra/ebml.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ pub mod writer {
647647
let cur_pos = self.writer.tell();
648648
self.writer.seek(last_size_pos as i64, io::SeekSet);
649649
let size = (cur_pos as uint - last_size_pos - 4);
650-
write_sized_vuint(self.writer, size as uint, 4u);
650+
write_sized_vuint(self.writer, size, 4u);
651651
self.writer.seek(cur_pos as i64, io::SeekSet);
652652

653653
debug!("End tag (size = {})", size);

src/libextra/test.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -929,8 +929,8 @@ fn calc_result(desc: &TestDesc, task_succeeded: bool) -> TestResult {
929929
impl ToJson for Metric {
930930
fn to_json(&self) -> json::Json {
931931
let mut map = ~TreeMap::new();
932-
map.insert(~"value", json::Number(self.value as f64));
933-
map.insert(~"noise", json::Number(self.noise as f64));
932+
map.insert(~"value", json::Number(self.value));
933+
map.insert(~"noise", json::Number(self.noise));
934934
json::Object(map)
935935
}
936936
}
@@ -1132,15 +1132,15 @@ impl BenchHarness {
11321132
let loop_start = precise_time_ns();
11331133
11341134
for p in samples.mut_iter() {
1135-
self.bench_n(n as u64, |x| f(x));
1135+
self.bench_n(n, |x| f(x));
11361136
*p = self.ns_per_iter() as f64;
11371137
};
11381138
11391139
stats::winsorize(samples, 5.0);
11401140
let summ = stats::Summary::new(samples);
11411141
11421142
for p in samples.mut_iter() {
1143-
self.bench_n(5 * n as u64, |x| f(x));
1143+
self.bench_n(5 * n, |x| f(x));
11441144
*p = self.ns_per_iter() as f64;
11451145
};
11461146

src/libextra/time.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -767,13 +767,13 @@ pub fn strptime(s: &str, format: &str) -> Result<Tm, ~str> {
767767

768768
let mut buf = [0];
769769
let c = match rdr.read(buf) {
770-
Some(..) => buf[0] as u8 as char,
770+
Some(..) => buf[0] as char,
771771
None => break
772772
};
773773
match c {
774774
'%' => {
775775
let ch = match rdr.read(buf) {
776-
Some(..) => buf[0] as u8 as char,
776+
Some(..) => buf[0] as char,
777777
None => break
778778
};
779779
match parse_type(s, pos, ch, &mut tm) {

src/librustc/middle/lint.rs

+44-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ use middle::typeck;
4141
use middle::pat_util;
4242
use metadata::csearch;
4343
use util::ppaux::{ty_to_str};
44+
use std::to_str::ToStr;
45+
46+
use middle::typeck::infer;
47+
use middle::typeck::astconv::{ast_ty_to_ty, AstConv};
4448

4549
use std::cmp;
4650
use std::hashmap::HashMap;
@@ -91,6 +95,7 @@ pub enum lint {
9195
unused_mut,
9296
unnecessary_allocation,
9397
dead_code,
98+
unnecessary_typecast,
9499

95100
missing_doc,
96101
unreachable_code,
@@ -267,6 +272,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
267272
default: warn
268273
}),
269274

275+
("unnecessary_typecast",
276+
LintSpec {
277+
lint: unnecessary_typecast,
278+
desc: "detects unnecessary type casts, that can be removed",
279+
default: allow,
280+
}),
281+
270282
("unused_mut",
271283
LintSpec {
272284
lint: unused_mut,
@@ -336,7 +348,6 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
336348
desc: "unknown features found in crate-level #[feature] directives",
337349
default: deny,
338350
}),
339-
340351
("unknown_crate_type",
341352
LintSpec {
342353
lint: unknown_crate_type,
@@ -569,6 +580,37 @@ fn check_while_true_expr(cx: &Context, e: &ast::Expr) {
569580
_ => ()
570581
}
571582
}
583+
impl<'a> AstConv for Context<'a>{
584+
fn tcx(&self) -> ty::ctxt { self.tcx }
585+
586+
fn get_item_ty(&self, id: ast::DefId) -> ty::ty_param_bounds_and_ty {
587+
ty::lookup_item_type(self.tcx, id)
588+
}
589+
590+
fn get_trait_def(&self, id: ast::DefId) -> @ty::TraitDef {
591+
ty::lookup_trait_def(self.tcx, id)
592+
}
593+
594+
fn ty_infer(&self, _span: Span) -> ty::t {
595+
let infcx: @infer::InferCtxt = infer::new_infer_ctxt(self.tcx);
596+
infcx.next_ty_var()
597+
}
598+
}
599+
600+
601+
fn check_unused_casts(cx: &Context, e: &ast::Expr) {
602+
return match e.node {
603+
ast::ExprCast(expr, ty) => {
604+
let infcx: @infer::InferCtxt = infer::new_infer_ctxt(cx.tcx);
605+
let t_t = ast_ty_to_ty(cx, &infcx, ty);
606+
if ty::get(ty::expr_ty(cx.tcx, expr)).sty == ty::get(t_t).sty {
607+
cx.span_lint(unnecessary_typecast, ty.span,
608+
"unnecessary type cast");
609+
}
610+
}
611+
_ => ()
612+
};
613+
}
572614

573615
fn check_type_limits(cx: &Context, e: &ast::Expr) {
574616
return match e.node {
@@ -1361,6 +1403,7 @@ impl<'a> Visitor<()> for Context<'a> {
13611403
check_heap_expr(self, e);
13621404

13631405
check_type_limits(self, e);
1406+
check_unused_casts(self, e);
13641407

13651408
visit::walk_expr(self, e, ());
13661409
}

src/libstd/cleanup.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ unsafe fn each_live_alloc(read_next_before: bool,
4242
let next_before = (*alloc).next;
4343
let uniq = (*alloc).ref_count == managed::RC_MANAGED_UNIQUE;
4444

45-
if !f(alloc as *mut raw::Box<()>, uniq) {
45+
if !f(alloc, uniq) {
4646
return false;
4747
}
4848

src/libstd/io/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ pub trait Reader {
725725
///
726726
/// `u64`s are 8 bytes long.
727727
fn read_be_u64(&mut self) -> u64 {
728-
self.read_be_uint_n(8) as u64
728+
self.read_be_uint_n(8)
729729
}
730730

731731
/// Reads a big-endian `u32`.
@@ -746,7 +746,7 @@ pub trait Reader {
746746
///
747747
/// `i64`s are 8 bytes long.
748748
fn read_be_i64(&mut self) -> i64 {
749-
self.read_be_int_n(8) as i64
749+
self.read_be_int_n(8)
750750
}
751751

752752
/// Reads a big-endian `i32`.
@@ -785,7 +785,7 @@ pub trait Reader {
785785
///
786786
/// `u64`s are 8 bytes long.
787787
fn read_le_u64(&mut self) -> u64 {
788-
self.read_le_uint_n(8) as u64
788+
self.read_le_uint_n(8)
789789
}
790790

791791
/// Reads a little-endian `u32`.
@@ -806,7 +806,7 @@ pub trait Reader {
806806
///
807807
/// `i64`s are 8 bytes long.
808808
fn read_le_i64(&mut self) -> i64 {
809-
self.read_le_int_n(8) as i64
809+
self.read_le_int_n(8)
810810
}
811811

812812
/// Reads a little-endian `i32`.
@@ -846,7 +846,7 @@ pub trait Reader {
846846
/// `u8`s are 1 byte.
847847
fn read_u8(&mut self) -> u8 {
848848
match self.read_byte() {
849-
Some(b) => b as u8,
849+
Some(b) => b,
850850
None => 0
851851
}
852852
}

src/libstd/io/net/ip.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ impl<'a> Parser<'a> {
153153
let c = c as u8;
154154
// assuming radix is either 10 or 16
155155
if c >= '0' as u8 && c <= '9' as u8 {
156-
Some((c - '0' as u8) as u8)
156+
Some(c - '0' as u8)
157157
} else if radix > 10 && c >= 'a' as u8 && c < 'a' as u8 + (radix - 10) {
158-
Some((c - 'a' as u8 + 10) as u8)
158+
Some(c - 'a' as u8 + 10)
159159
} else if radix > 10 && c >= 'A' as u8 && c < 'A' as u8 + (radix - 10) {
160-
Some((c - 'A' as u8 + 10) as u8)
160+
Some(c - 'A' as u8 + 10)
161161
} else {
162162
None
163163
}

src/libstd/io/stdio.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl Reader for StdReader {
209209
io_error::cond.raise(standard_error(EndOfFile));
210210
None
211211
}
212-
Ok(amt) => Some(amt as uint),
212+
Ok(amt) => Some(amt),
213213
Err(e) => {
214214
io_error::cond.raise(e);
215215
None

src/libstd/num/strconv.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ pub fn float_to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Float+Round+
344344
// round the remaining ones.
345345
if limit_digits && dig == digit_count {
346346
let ascii2value = |chr: u8| {
347-
char::to_digit(chr as char, radix).unwrap() as uint
347+
char::to_digit(chr as char, radix).unwrap()
348348
};
349349
let value2ascii = |val: uint| {
350350
char::from_digit(val, radix).unwrap() as u8

src/libstd/rand/distributions/normal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl Rand for StandardNormal {
3232
fn rand<R:Rng>(rng: &mut R) -> StandardNormal {
3333
#[inline]
3434
fn pdf(x: f64) -> f64 {
35-
((-x*x/2.0) as f64).exp()
35+
(-x*x/2.0).exp()
3636
}
3737
#[inline]
3838
fn zero_case<R:Rng>(rng: &mut R, u: f64) -> f64 {

src/libstd/rt/global_heap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub unsafe fn closure_exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char {
7676
assert!(td.is_not_null());
7777

7878
let total_size = get_box_size(size, (*td).align);
79-
let p = malloc_raw(total_size as uint);
79+
let p = malloc_raw(total_size);
8080

8181
let alloc = p as *mut raw::Box<()>;
8282
(*alloc).type_desc = td;

src/libstd/str.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ pub fn utf16_chars(v: &[u16], f: |char|) {
901901
let mut c: u32 = (u - 0xD800_u16) as u32;
902902
c = c << 10;
903903
c |= (u2 - 0xDC00_u16) as u32;
904-
c |= 0x1_0000_u32 as u32;
904+
c |= 0x1_0000_u32;
905905
f(unsafe { cast::transmute(c) });
906906
i += 2u;
907907
}
@@ -987,7 +987,7 @@ pub mod raw {
987987
/// Create a Rust string from a *u8 buffer of the given length
988988
pub unsafe fn from_buf_len(buf: *u8, len: uint) -> ~str {
989989
let mut v: ~[u8] = vec::with_capacity(len);
990-
ptr::copy_memory(v.as_mut_ptr(), buf as *u8, len);
990+
ptr::copy_memory(v.as_mut_ptr(), buf, len);
991991
v.set_len(len);
992992

993993
assert!(is_utf8(v));

src/libstd/unstable/mutex.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ impl Mutex {
6666
/// Creates a new mutex, with the lock/condition variable pre-initialized
6767
pub unsafe fn new() -> Mutex {
6868
Mutex {
69-
lock: atomics::AtomicUint::new(imp::init_lock() as uint),
70-
cond: atomics::AtomicUint::new(imp::init_cond() as uint),
69+
lock: atomics::AtomicUint::new(imp::init_lock()),
70+
cond: atomics::AtomicUint::new(imp::init_cond()),
7171
}
7272
}
7373

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[forbid(unnecessary_typecast)];
12+
13+
fn foo_i32(_: i32) {}
14+
15+
fn foo_u64(a: u64) {
16+
let b: i32 = a as i32;
17+
foo_i32(b as i32); //~ ERROR: unnecessary type cast
18+
}
19+
20+
fn main() {
21+
let x: u64 = 1;
22+
let y: u64 = x as u64; //~ ERROR: unnecessary type cast
23+
foo_u64(y as u64); //~ ERROR: unnecessary type cast
24+
}

0 commit comments

Comments
 (0)