1 use crate::utils::{span_lint, SpanlessEq};
2 use if_chain::if_chain;
3 use rustc_hir::{BinOpKind, Expr, ExprKind, QPath};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
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.
21 pub OVERFLOW_CHECK_CONDITIONAL,
23 "overflow checks inspired by C which are likely to panic"
26 declare_lint_pass!(OverflowCheckConditional => [OVERFLOW_CHECK_CONDITIONAL]);
28 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OverflowCheckConditional {
29 // a + b < a, a > a + b, a < a - b, a - b > a
30 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
31 let eq = |l, r| SpanlessEq::new(cx).eq_path_segment(l, r);
33 if let ExprKind::Binary(ref op, ref first, ref second) = expr.kind;
34 if let ExprKind::Binary(ref op2, ref ident1, ref ident2) = first.kind;
35 if let ExprKind::Path(QPath::Resolved(_, ref path1)) = ident1.kind;
36 if let ExprKind::Path(QPath::Resolved(_, ref path2)) = ident2.kind;
37 if let ExprKind::Path(QPath::Resolved(_, ref path3)) = second.kind;
38 if eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]);
39 if cx.tables.expr_ty(ident1).is_integral();
40 if cx.tables.expr_ty(ident2).is_integral();
42 if let BinOpKind::Lt = op.node {
43 if let BinOpKind::Add = op2.node {
44 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
45 "You are trying to use classic C overflow conditions that will fail in Rust.");
48 if let BinOpKind::Gt = op.node {
49 if let BinOpKind::Sub = op2.node {
50 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
51 "You are trying to use classic C underflow conditions that will fail in Rust.");
58 if let ExprKind::Binary(ref op, ref first, ref second) = expr.kind;
59 if let ExprKind::Binary(ref op2, ref ident1, ref ident2) = second.kind;
60 if let ExprKind::Path(QPath::Resolved(_, ref path1)) = ident1.kind;
61 if let ExprKind::Path(QPath::Resolved(_, ref path2)) = ident2.kind;
62 if let ExprKind::Path(QPath::Resolved(_, ref path3)) = first.kind;
63 if eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]);
64 if cx.tables.expr_ty(ident1).is_integral();
65 if cx.tables.expr_ty(ident2).is_integral();
67 if let BinOpKind::Gt = op.node {
68 if let BinOpKind::Add = op2.node {
69 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
70 "You are trying to use classic C overflow conditions that will fail in Rust.");
73 if let BinOpKind::Lt = op.node {
74 if let BinOpKind::Sub = op2.node {
75 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
76 "You are trying to use classic C underflow conditions that will fail in Rust.");