From 0e8e0b2ede175be6a4874700674b259c8c63c2cc Mon Sep 17 00:00:00 2001 From: Virgile Andreani Date: Sun, 4 May 2014 20:42:45 +0200 Subject: [PATCH 1/2] Add missing cases to the type_limits lint and exhaustive testing for the `u8` type. --- src/librustc/middle/lint.rs | 8 ++++---- src/test/compile-fail/lint-type-limits.rs | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/librustc/middle/lint.rs b/src/librustc/middle/lint.rs index 93e8c637f1738..aca8dc6125201 100644 --- a/src/librustc/middle/lint.rs +++ b/src/librustc/middle/lint.rs @@ -788,10 +788,10 @@ fn check_type_limits(cx: &Context, e: &ast::Expr) { fn is_valid(binop: ast::BinOp, v: T, min: T, max: T) -> bool { match binop { - ast::BiLt => v <= max, - ast::BiLe => v < max, - ast::BiGt => v >= min, - ast::BiGe => v > min, + ast::BiLt => v > min && v <= max, + ast::BiLe => v >= min && v < max, + ast::BiGt => v >= min && v < max, + ast::BiGe => v > min && v <= max, ast::BiEq | ast::BiNe => v >= min && v <= max, _ => fail!() } diff --git a/src/test/compile-fail/lint-type-limits.rs b/src/test/compile-fail/lint-type-limits.rs index 1aaf68d7c1ad1..67ef9cd06803f 100644 --- a/src/test/compile-fail/lint-type-limits.rs +++ b/src/test/compile-fail/lint-type-limits.rs @@ -29,6 +29,18 @@ fn baz() -> bool { //~^ WARNING literal out of range for its type } +fn bleh() { + let u = 42u8; + let _ = u > 255; //~ ERROR comparison is useless due to type limits + let _ = 255 < u; //~ ERROR comparison is useless due to type limits + let _ = u < 0; //~ ERROR comparison is useless due to type limits + let _ = 0 > u; //~ ERROR comparison is useless due to type limits + let _ = u <= 255; //~ ERROR comparison is useless due to type limits + let _ = 255 >= u; //~ ERROR comparison is useless due to type limits + let _ = u >= 0; //~ ERROR comparison is useless due to type limits + let _ = 0 <= u; //~ ERROR comparison is useless due to type limits +} + fn qux() { let mut i = 1i8; while 200 != i { //~ ERROR comparison is useless due to type limits From 169a57ee8d301c44fb285cc95fc1309d96aa37ab Mon Sep 17 00:00:00 2001 From: Virgile Andreani Date: Sun, 4 May 2014 20:48:16 +0200 Subject: [PATCH 2/2] Remove two useless comparisons according to the updated type_limits lint. --- src/libsyntax/ext/format.rs | 2 +- src/libterm/terminfo/parser/compiled.rs | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/libsyntax/ext/format.rs b/src/libsyntax/ext/format.rs index df79b10544432..0d856fb689d44 100644 --- a/src/libsyntax/ext/format.rs +++ b/src/libsyntax/ext/format.rs @@ -268,7 +268,7 @@ impl<'a, 'b> Context<'a, 'b> { fn verify_arg_type(&mut self, arg: Position, ty: ArgumentType) { match arg { Exact(arg) => { - if arg < 0 || self.args.len() <= arg { + if self.args.len() <= arg { let msg = format!("invalid reference to argument `{}` (there \ are {} arguments)", arg, self.args.len()); self.ecx.span_err(self.fmtsp, msg); diff --git a/src/libterm/terminfo/parser/compiled.rs b/src/libterm/terminfo/parser/compiled.rs index 538a1513d65a7..db5dd2d5c1907 100644 --- a/src/libterm/terminfo/parser/compiled.rs +++ b/src/libterm/terminfo/parser/compiled.rs @@ -220,9 +220,7 @@ pub fn parse(file: &mut io::Reader, if bools_bytes != 0 { for i in range(0, bools_bytes) { let b = try!(file.read_byte()); - if b < 0 { - return Err("error: expected more bools but hit EOF".to_owned()); - } else if b == 1 { + if b == 1 { bools_map.insert(bnames[i as uint].to_owned(), true); } }