]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/overflow_check_conditional.rs
New upstream version 1.52.1+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / overflow_check_conditional.rs
CommitLineData
f20569fa
XL
1use crate::utils::{span_lint, SpanlessEq};
2use if_chain::if_chain;
3use rustc_hir::{BinOpKind, Expr, ExprKind, QPath};
4use rustc_lint::{LateContext, LateLintPass};
5use rustc_session::{declare_lint_pass, declare_tool_lint};
6
7declare_clippy_lint! {
8 /// **What it does:** Detects classic underflow/overflow checks.
9 ///
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.
12 ///
13 /// **Known problems:** None.
14 ///
15 /// **Example:**
16 /// ```rust
17 /// # let a = 1;
18 /// # let b = 2;
19 /// a + b < a;
20 /// ```
21 pub OVERFLOW_CHECK_CONDITIONAL,
22 complexity,
23 "overflow checks inspired by C which are likely to panic"
24}
25
26declare_lint_pass!(OverflowCheckConditional => [OVERFLOW_CHECK_CONDITIONAL]);
27
28impl<'tcx> LateLintPass<'tcx> for OverflowCheckConditional {
29 // a + b < a, a > a + b, a < a - b, a - b > a
30 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
31 let eq = |l, r| SpanlessEq::new(cx).eq_path_segment(l, r);
32 if_chain! {
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.typeck_results().expr_ty(ident1).is_integral();
40 if cx.typeck_results().expr_ty(ident2).is_integral();
41 then {
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");
46 }
47 }
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");
52 }
53 }
54 }
55 }
56
57 if_chain! {
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.typeck_results().expr_ty(ident1).is_integral();
65 if cx.typeck_results().expr_ty(ident2).is_integral();
66 then {
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");
71 }
72 }
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");
77 }
78 }
79 }
80 }
81 }
82}