Adding symmetric lints and test cases

This commit is contained in:
KALPESH KRISHNA
2016-03-08 11:03:30 +05:30
parent d6d409414e
commit 9faffd2870
2 changed files with 60 additions and 19 deletions

View File

@@ -1,14 +1,16 @@
#![allow(cyclomatic_complexity)]
use rustc::lint::*;
use rustc_front::hir::*;
use utils::{span_lint};
/// **What it does:** This lint finds classic overflow checks.
/// **What it does:** This lint finds classic underflow / overflow checks.
///
/// **Why is this bad?** Most classic C overflow checks will fail in Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead.
/// **Why is this bad?** Most classic C underflow / overflow checks will fail in Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead.
///
/// **Known problems:** None.
///
/// **Example:** `a + b < a`
declare_lint!(pub OVERFLOW_CHECK_CONDITIONAL, Warn,
"Using overflow checks which are likely to panic");
@@ -22,35 +24,50 @@ impl LintPass for OverflowCheckConditional {
}
impl LateLintPass for OverflowCheckConditional {
// a + b < a, a > a + b, a < a - b, a - b > a
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
if_let_chain! {[
let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node,
let BinOp_::BiLt = op.node,
let Expr_::ExprBinary(ref op2, ref add1, ref add2) = first.node,
let BinOp_::BiAdd = op2.node,
let Expr_::ExprPath(_,ref path1) = add1.node,
let Expr_::ExprPath(_, ref path2) = add2.node,
let Expr_::ExprBinary(ref op2, ref ident1, ref ident2) = first.node,
let Expr_::ExprPath(_,ref path1) = ident1.node,
let Expr_::ExprPath(_, ref path2) = ident2.node,
let Expr_::ExprPath(_, ref path3) = second.node,
(&path1.segments[0]).identifier == (&path3.segments[0]).identifier || (&path2.segments[0]).identifier == (&path3.segments[0]).identifier,
cx.tcx.expr_ty(add1).is_integral(),
cx.tcx.expr_ty(add2).is_integral()
cx.tcx.expr_ty(ident1).is_integral(),
cx.tcx.expr_ty(ident2).is_integral()
], {
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust.");
if let BinOp_::BiLt = op.node {
if let BinOp_::BiAdd = op2.node {
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust.");
}
}
if let BinOp_::BiGt = op.node {
if let BinOp_::BiSub = op2.node {
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditons that will fail in Rust.");
}
}
}}
if_let_chain! {[
let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node,
let BinOp_::BiGt = op.node,
let Expr_::ExprBinary(ref op2, ref sub1, ref sub2) = first.node,
let BinOp_::BiSub = op2.node,
let Expr_::ExprPath(_,ref path1) = sub1.node,
let Expr_::ExprPath(_, ref path2) = sub2.node,
let Expr_::ExprPath(_, ref path3) = second.node,
let Expr_::ExprBinary(ref op2, ref ident1, ref ident2) = second.node,
let Expr_::ExprPath(_,ref path1) = ident1.node,
let Expr_::ExprPath(_, ref path2) = ident2.node,
let Expr_::ExprPath(_, ref path3) = first.node,
(&path1.segments[0]).identifier == (&path3.segments[0]).identifier || (&path2.segments[0]).identifier == (&path3.segments[0]).identifier,
cx.tcx.expr_ty(sub1).is_integral(),
cx.tcx.expr_ty(sub2).is_integral()
cx.tcx.expr_ty(ident1).is_integral(),
cx.tcx.expr_ty(ident2).is_integral()
], {
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditons that will fail in Rust.");
if let BinOp_::BiGt = op.node {
if let BinOp_::BiAdd = op2.node {
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust.");
}
}
if let BinOp_::BiLt = op.node {
if let BinOp_::BiSub = op2.node {
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditons that will fail in Rust.");
}
}
}}
}
}