Skip to content

Commit d6d4094

Browse files
Adding underflow checks and tests
1 parent 55fbf59 commit d6d4094

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/overflow_check_conditional.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,20 @@ impl LateLintPass for OverflowCheckConditional {
3737
], {
3838
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust.");
3939
}}
40+
41+
if_let_chain! {[
42+
let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node,
43+
let BinOp_::BiGt = op.node,
44+
let Expr_::ExprBinary(ref op2, ref sub1, ref sub2) = first.node,
45+
let BinOp_::BiSub = op2.node,
46+
let Expr_::ExprPath(_,ref path1) = sub1.node,
47+
let Expr_::ExprPath(_, ref path2) = sub2.node,
48+
let Expr_::ExprPath(_, ref path3) = second.node,
49+
(&path1.segments[0]).identifier == (&path3.segments[0]).identifier || (&path2.segments[0]).identifier == (&path3.segments[0]).identifier,
50+
cx.tcx.expr_ty(sub1).is_integral(),
51+
cx.tcx.expr_ty(sub2).is_integral()
52+
], {
53+
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditons that will fail in Rust.");
54+
}}
4055
}
4156
}

tests/compile-fail/overflow_check_conditional.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,26 @@ fn main() {
1212
}
1313
if a + b < b { //~ERROR You are trying to use classic C overflow conditons that will fail in Rust.
1414

15+
}
16+
if a - b > b { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust.
17+
18+
}
19+
if a - b > a { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust.
20+
1521
}
1622
if a + b < c {
1723

24+
}
25+
if a - b < c {
26+
1827
}
1928
let i = 1.1;
2029
let j = 2.2;
2130
if i + j < i {
2231

32+
}
33+
if i - j < i {
34+
2335
}
2436
}
2537

0 commit comments

Comments
 (0)