]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/overflow_check_conditional.rs
bump version to 1.80.1+dfsg1-1~bpo12+pve1
[rustc.git] / src / tools / clippy / clippy_lints / src / overflow_check_conditional.rs
CommitLineData
cdc7bbd5
XL
1use clippy_utils::diagnostics::span_lint;
2use clippy_utils::SpanlessEq;
f20569fa
XL
3use rustc_hir::{BinOpKind, Expr, ExprKind, QPath};
4use rustc_lint::{LateContext, LateLintPass};
4b012472 5use rustc_session::declare_lint_pass;
f20569fa
XL
6
7declare_clippy_lint! {
94222f64
XL
8 /// ### What it does
9 /// Detects classic underflow/overflow checks.
f20569fa 10 ///
94222f64
XL
11 /// ### Why is this bad?
12 /// Most classic C underflow/overflow checks will fail in
f20569fa
XL
13 /// Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead.
14 ///
94222f64 15 /// ### Example
ed00b5ec 16 /// ```no_run
f20569fa
XL
17 /// # let a = 1;
18 /// # let b = 2;
19 /// a + b < a;
20 /// ```
a2a8927a 21 #[clippy::version = "pre 1.29.0"]
f20569fa
XL
22 pub OVERFLOW_CHECK_CONDITIONAL,
23 complexity,
24 "overflow checks inspired by C which are likely to panic"
25}
26
27declare_lint_pass!(OverflowCheckConditional => [OVERFLOW_CHECK_CONDITIONAL]);
28
c295e0f8
XL
29const OVERFLOW_MSG: &str = "you are trying to use classic C overflow conditions that will fail in Rust";
30const UNDERFLOW_MSG: &str = "you are trying to use classic C underflow conditions that will fail in Rust";
31
f20569fa
XL
32impl<'tcx> LateLintPass<'tcx> for OverflowCheckConditional {
33 // a + b < a, a > a + b, a < a - b, a - b > a
34 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
35 let eq = |l, r| SpanlessEq::new(cx).eq_path_segment(l, r);
4b012472
FG
36 if let ExprKind::Binary(ref op, first, second) = expr.kind
37 && let ExprKind::Binary(ref op2, ident1, ident2) = first.kind
38 && let ExprKind::Path(QPath::Resolved(_, path1)) = ident1.kind
39 && let ExprKind::Path(QPath::Resolved(_, path2)) = ident2.kind
40 && let ExprKind::Path(QPath::Resolved(_, path3)) = second.kind
41 && (eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]))
42 && cx.typeck_results().expr_ty(ident1).is_integral()
43 && cx.typeck_results().expr_ty(ident2).is_integral()
44 {
45 if op.node == BinOpKind::Lt && op2.node == BinOpKind::Add {
46 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, OVERFLOW_MSG);
47 }
48 if op.node == BinOpKind::Gt && op2.node == BinOpKind::Sub {
49 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, UNDERFLOW_MSG);
f20569fa
XL
50 }
51 }
52
4b012472
FG
53 if let ExprKind::Binary(ref op, first, second) = expr.kind
54 && let ExprKind::Binary(ref op2, ident1, ident2) = second.kind
55 && let ExprKind::Path(QPath::Resolved(_, path1)) = ident1.kind
56 && let ExprKind::Path(QPath::Resolved(_, path2)) = ident2.kind
57 && let ExprKind::Path(QPath::Resolved(_, path3)) = first.kind
58 && (eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]))
59 && cx.typeck_results().expr_ty(ident1).is_integral()
60 && cx.typeck_results().expr_ty(ident2).is_integral()
61 {
62 if op.node == BinOpKind::Gt && op2.node == BinOpKind::Add {
63 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, OVERFLOW_MSG);
64 }
65 if op.node == BinOpKind::Lt && op2.node == BinOpKind::Sub {
66 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, UNDERFLOW_MSG);
f20569fa
XL
67 }
68 }
69 }
70}