1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 use crate::utils::{span_lint, SpanlessEq};
11 use if_chain::if_chain;
13 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
14 use rustc::{declare_tool_lint, lint_array};
16 /// **What it does:** Detects classic underflow/overflow checks.
18 /// **Why is this bad?** Most classic C underflow/overflow checks will fail in
19 /// Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead.
21 /// **Known problems:** None.
27 declare_clippy_lint! {
28 pub OVERFLOW_CHECK_CONDITIONAL,
30 "overflow checks inspired by C which are likely to panic"
33 #[derive(Copy, Clone)]
34 pub struct OverflowCheckConditional;
36 impl LintPass for OverflowCheckConditional {
37 fn get_lints(&self) -> LintArray {
38 lint_array!(OVERFLOW_CHECK_CONDITIONAL)
42 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OverflowCheckConditional {
43 // a + b < a, a > a + b, a < a - b, a - b > a
44 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
45 let eq = |l, r| SpanlessEq::new(cx).eq_path_segment(l, r);
47 if let ExprKind::Binary(ref op, ref first, ref second) = expr.node;
48 if let ExprKind::Binary(ref op2, ref ident1, ref ident2) = first.node;
49 if let ExprKind::Path(QPath::Resolved(_, ref path1)) = ident1.node;
50 if let ExprKind::Path(QPath::Resolved(_, ref path2)) = ident2.node;
51 if let ExprKind::Path(QPath::Resolved(_, ref path3)) = second.node;
52 if eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]);
53 if cx.tables.expr_ty(ident1).is_integral();
54 if cx.tables.expr_ty(ident2).is_integral();
56 if let BinOpKind::Lt = op.node {
57 if let BinOpKind::Add = op2.node {
58 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
59 "You are trying to use classic C overflow conditions that will fail in Rust.");
62 if let BinOpKind::Gt = op.node {
63 if let BinOpKind::Sub = op2.node {
64 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
65 "You are trying to use classic C underflow conditions that will fail in Rust.");
72 if let ExprKind::Binary(ref op, ref first, ref second) = expr.node;
73 if let ExprKind::Binary(ref op2, ref ident1, ref ident2) = second.node;
74 if let ExprKind::Path(QPath::Resolved(_, ref path1)) = ident1.node;
75 if let ExprKind::Path(QPath::Resolved(_, ref path2)) = ident2.node;
76 if let ExprKind::Path(QPath::Resolved(_, ref path3)) = first.node;
77 if eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]);
78 if cx.tables.expr_ty(ident1).is_integral();
79 if cx.tables.expr_ty(ident2).is_integral();
81 if let BinOpKind::Gt = op.node {
82 if let BinOpKind::Add = op2.node {
83 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
84 "You are trying to use classic C overflow conditions that will fail in Rust.");
87 if let BinOpKind::Lt = op.node {
88 if let BinOpKind::Sub = op2.node {
89 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
90 "You are trying to use classic C underflow conditions that will fail in Rust.");