Skip to content

Commit dcde1ee

Browse files
committed
auto merge of #13936 : Armavica/rust/lint_check-range, r=kballard
Some cases were not correctly handled by this lint, for instance `let a = 42u8; a < 0` and `let a = 42u8; a > 255`. It led to the discovery of two useless comparisons, which I removed.
2 parents abdacec + 169a57e commit dcde1ee

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

src/librustc/middle/lint.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -788,10 +788,10 @@ fn check_type_limits(cx: &Context, e: &ast::Expr) {
788788
fn is_valid<T:cmp::Ord>(binop: ast::BinOp, v: T,
789789
min: T, max: T) -> bool {
790790
match binop {
791-
ast::BiLt => v <= max,
792-
ast::BiLe => v < max,
793-
ast::BiGt => v >= min,
794-
ast::BiGe => v > min,
791+
ast::BiLt => v > min && v <= max,
792+
ast::BiLe => v >= min && v < max,
793+
ast::BiGt => v >= min && v < max,
794+
ast::BiGe => v > min && v <= max,
795795
ast::BiEq | ast::BiNe => v >= min && v <= max,
796796
_ => fail!()
797797
}

src/libsyntax/ext/format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl<'a, 'b> Context<'a, 'b> {
268268
fn verify_arg_type(&mut self, arg: Position, ty: ArgumentType) {
269269
match arg {
270270
Exact(arg) => {
271-
if arg < 0 || self.args.len() <= arg {
271+
if self.args.len() <= arg {
272272
let msg = format!("invalid reference to argument `{}` (there \
273273
are {} arguments)", arg, self.args.len());
274274
self.ecx.span_err(self.fmtsp, msg);

src/libterm/terminfo/parser/compiled.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,7 @@ pub fn parse(file: &mut io::Reader,
220220
if bools_bytes != 0 {
221221
for i in range(0, bools_bytes) {
222222
let b = try!(file.read_byte());
223-
if b < 0 {
224-
return Err("error: expected more bools but hit EOF".to_owned());
225-
} else if b == 1 {
223+
if b == 1 {
226224
bools_map.insert(bnames[i as uint].to_owned(), true);
227225
}
228226
}

src/test/compile-fail/lint-type-limits.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ fn baz() -> bool {
2929
//~^ WARNING literal out of range for its type
3030
}
3131

32+
fn bleh() {
33+
let u = 42u8;
34+
let _ = u > 255; //~ ERROR comparison is useless due to type limits
35+
let _ = 255 < u; //~ ERROR comparison is useless due to type limits
36+
let _ = u < 0; //~ ERROR comparison is useless due to type limits
37+
let _ = 0 > u; //~ ERROR comparison is useless due to type limits
38+
let _ = u <= 255; //~ ERROR comparison is useless due to type limits
39+
let _ = 255 >= u; //~ ERROR comparison is useless due to type limits
40+
let _ = u >= 0; //~ ERROR comparison is useless due to type limits
41+
let _ = 0 <= u; //~ ERROR comparison is useless due to type limits
42+
}
43+
3244
fn qux() {
3345
let mut i = 1i8;
3446
while 200 != i { //~ ERROR comparison is useless due to type limits

0 commit comments

Comments
 (0)