Skip to content

Commit 9faffd2

Browse files
Adding symmetric lints and test cases
1 parent d6d4094 commit 9faffd2

File tree

2 files changed

+60
-19
lines changed

2 files changed

+60
-19
lines changed

src/overflow_check_conditional.rs

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
#![allow(cyclomatic_complexity)]
12
use rustc::lint::*;
23
use rustc_front::hir::*;
34
use utils::{span_lint};
45

5-
/// **What it does:** This lint finds classic overflow checks.
6+
/// **What it does:** This lint finds classic underflow / overflow checks.
67
///
7-
/// **Why is this bad?** Most classic C overflow checks will fail in Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead.
8+
/// **Why is this bad?** Most classic C underflow / overflow checks will fail in Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead.
89
///
910
/// **Known problems:** None.
1011
///
1112
/// **Example:** `a + b < a`
13+
1214
declare_lint!(pub OVERFLOW_CHECK_CONDITIONAL, Warn,
1315
"Using overflow checks which are likely to panic");
1416

@@ -22,35 +24,50 @@ impl LintPass for OverflowCheckConditional {
2224
}
2325

2426
impl LateLintPass for OverflowCheckConditional {
27+
// a + b < a, a > a + b, a < a - b, a - b > a
2528
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
2629
if_let_chain! {[
2730
let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node,
28-
let BinOp_::BiLt = op.node,
29-
let Expr_::ExprBinary(ref op2, ref add1, ref add2) = first.node,
30-
let BinOp_::BiAdd = op2.node,
31-
let Expr_::ExprPath(_,ref path1) = add1.node,
32-
let Expr_::ExprPath(_, ref path2) = add2.node,
31+
let Expr_::ExprBinary(ref op2, ref ident1, ref ident2) = first.node,
32+
let Expr_::ExprPath(_,ref path1) = ident1.node,
33+
let Expr_::ExprPath(_, ref path2) = ident2.node,
3334
let Expr_::ExprPath(_, ref path3) = second.node,
3435
(&path1.segments[0]).identifier == (&path3.segments[0]).identifier || (&path2.segments[0]).identifier == (&path3.segments[0]).identifier,
35-
cx.tcx.expr_ty(add1).is_integral(),
36-
cx.tcx.expr_ty(add2).is_integral()
36+
cx.tcx.expr_ty(ident1).is_integral(),
37+
cx.tcx.expr_ty(ident2).is_integral()
3738
], {
38-
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust.");
39+
if let BinOp_::BiLt = op.node {
40+
if let BinOp_::BiAdd = op2.node {
41+
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust.");
42+
}
43+
}
44+
if let BinOp_::BiGt = op.node {
45+
if let BinOp_::BiSub = op2.node {
46+
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditons that will fail in Rust.");
47+
}
48+
}
3949
}}
4050

4151
if_let_chain! {[
4252
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,
53+
let Expr_::ExprBinary(ref op2, ref ident1, ref ident2) = second.node,
54+
let Expr_::ExprPath(_,ref path1) = ident1.node,
55+
let Expr_::ExprPath(_, ref path2) = ident2.node,
56+
let Expr_::ExprPath(_, ref path3) = first.node,
4957
(&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()
58+
cx.tcx.expr_ty(ident1).is_integral(),
59+
cx.tcx.expr_ty(ident2).is_integral()
5260
], {
53-
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditons that will fail in Rust.");
61+
if let BinOp_::BiGt = op.node {
62+
if let BinOp_::BiAdd = op2.node {
63+
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust.");
64+
}
65+
}
66+
if let BinOp_::BiLt = op.node {
67+
if let BinOp_::BiSub = op2.node {
68+
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditons that will fail in Rust.");
69+
}
70+
}
5471
}}
5572
}
5673
}

tests/compile-fail/overflow_check_conditional.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,39 @@ fn main() {
99
let c: u32 = 3;
1010
if a + b < a { //~ERROR You are trying to use classic C overflow conditons that will fail in Rust.
1111

12+
}
13+
if a > a + b { //~ERROR You are trying to use classic C overflow conditons that will fail in Rust.
14+
1215
}
1316
if a + b < b { //~ERROR You are trying to use classic C overflow conditons that will fail in Rust.
1417

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

24+
}
25+
if b < a - b { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust.
26+
1827
}
1928
if a - b > a { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust.
2029

30+
}
31+
if a < a - b { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust.
32+
2133
}
2234
if a + b < c {
2335

36+
}
37+
if c > a + b {
38+
2439
}
2540
if a - b < c {
2641

42+
}
43+
if c > a - b {
44+
2745
}
2846
let i = 1.1;
2947
let j = 2.2;
@@ -32,6 +50,12 @@ fn main() {
3250
}
3351
if i - j < i {
3452

53+
}
54+
if i > i + j {
55+
56+
}
57+
if i - j < i {
58+
3559
}
3660
}
3761

0 commit comments

Comments
 (0)