1 use crate::utils::{span_lint, SpanlessEq};
2 use if_chain::if_chain;
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::{declare_tool_lint, lint_array};
8 /// **What it does:** Detects classic underflow/overflow checks.
10 /// **Why is this bad?** Most classic C underflow/overflow checks will fail in
11 /// Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead.
13 /// **Known problems:** None.
19 pub OVERFLOW_CHECK_CONDITIONAL,
21 "overflow checks inspired by C which are likely to panic"
24 #[derive(Copy, Clone)]
25 pub struct OverflowCheckConditional;
27 impl LintPass for OverflowCheckConditional {
28 fn get_lints(&self) -> LintArray {
29 lint_array!(OVERFLOW_CHECK_CONDITIONAL)
32 fn name(&self) -> &'static str {
33 "OverflowCheckConditional"
37 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OverflowCheckConditional {
38 // a + b < a, a > a + b, a < a - b, a - b > a
39 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
40 let eq = |l, r| SpanlessEq::new(cx).eq_path_segment(l, r);
42 if let ExprKind::Binary(ref op, ref first, ref second) = expr.node;
43 if let ExprKind::Binary(ref op2, ref ident1, ref ident2) = first.node;
44 if let ExprKind::Path(QPath::Resolved(_, ref path1)) = ident1.node;
45 if let ExprKind::Path(QPath::Resolved(_, ref path2)) = ident2.node;
46 if let ExprKind::Path(QPath::Resolved(_, ref path3)) = second.node;
47 if eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]);
48 if cx.tables.expr_ty(ident1).is_integral();
49 if cx.tables.expr_ty(ident2).is_integral();
51 if let BinOpKind::Lt = op.node {
52 if let BinOpKind::Add = op2.node {
53 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
54 "You are trying to use classic C overflow conditions that will fail in Rust.");
57 if let BinOpKind::Gt = op.node {
58 if let BinOpKind::Sub = op2.node {
59 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
60 "You are trying to use classic C underflow conditions that will fail in Rust.");
67 if let ExprKind::Binary(ref op, ref first, ref second) = expr.node;
68 if let ExprKind::Binary(ref op2, ref ident1, ref ident2) = second.node;
69 if let ExprKind::Path(QPath::Resolved(_, ref path1)) = ident1.node;
70 if let ExprKind::Path(QPath::Resolved(_, ref path2)) = ident2.node;
71 if let ExprKind::Path(QPath::Resolved(_, ref path3)) = first.node;
72 if eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]);
73 if cx.tables.expr_ty(ident1).is_integral();
74 if cx.tables.expr_ty(ident2).is_integral();
76 if let BinOpKind::Gt = op.node {
77 if let BinOpKind::Add = op2.node {
78 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
79 "You are trying to use classic C overflow conditions that will fail in Rust.");
82 if let BinOpKind::Lt = op.node {
83 if let BinOpKind::Sub = op2.node {
84 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
85 "You are trying to use classic C underflow conditions that will fail in Rust.");