Skip to content

Commit 0e8e0b2

Browse files
committed
Add missing cases to the type_limits lint
and exhaustive testing for the `u8` type.
1 parent 4f1b0b5 commit 0e8e0b2

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
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/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)