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.
11 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
12 use crate::rustc::{declare_tool_lint, lint_array};
13 use if_chain::if_chain;
14 use crate::rustc::hir::*;
15 use crate::utils::{span_lint, SpanlessEq};
17 /// **What it does:** Detects classic underflow/overflow checks.
19 /// **Why is this bad?** Most classic C underflow/overflow checks will fail in
20 /// Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead.
22 /// **Known problems:** None.
28 declare_clippy_lint! {
29 pub OVERFLOW_CHECK_CONDITIONAL,
31 "overflow checks inspired by C which are likely to panic"
34 #[derive(Copy, Clone)]
35 pub struct OverflowCheckConditional;
37 impl LintPass for OverflowCheckConditional {
38 fn get_lints(&self) -> LintArray {
39 lint_array!(OVERFLOW_CHECK_CONDITIONAL)
43 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OverflowCheckConditional {
44 // a + b < a, a > a + b, a < a - b, a - b > a
45 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
46 let eq = |l, r| SpanlessEq::new(cx).eq_path_segment(l, r);
48 if let ExprKind::Binary(ref op, ref first, ref second) = expr.node;
49 if let ExprKind::Binary(ref op2, ref ident1, ref ident2) = first.node;
50 if let ExprKind::Path(QPath::Resolved(_, ref path1)) = ident1.node;
51 if let ExprKind::Path(QPath::Resolved(_, ref path2)) = ident2.node;
52 if let ExprKind::Path(QPath::Resolved(_, ref path3)) = second.node;
53 if eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]);
54 if cx.tables.expr_ty(ident1).is_integral();
55 if cx.tables.expr_ty(ident2).is_integral();
57 if let BinOpKind::Lt = op.node {
58 if let BinOpKind::Add = op2.node {
59 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
60 "You are trying to use classic C overflow conditions that will fail in Rust.");
63 if let BinOpKind::Gt = op.node {
64 if let BinOpKind::Sub = op2.node {
65 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
66 "You are trying to use classic C underflow conditions that will fail in Rust.");
73 if let ExprKind::Binary(ref op, ref first, ref second) = expr.node;
74 if let ExprKind::Binary(ref op2, ref ident1, ref ident2) = second.node;
75 if let ExprKind::Path(QPath::Resolved(_, ref path1)) = ident1.node;
76 if let ExprKind::Path(QPath::Resolved(_, ref path2)) = ident2.node;
77 if let ExprKind::Path(QPath::Resolved(_, ref path3)) = first.node;
78 if eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]);
79 if cx.tables.expr_ty(ident1).is_integral();
80 if cx.tables.expr_ty(ident2).is_integral();
82 if let BinOpKind::Gt = op.node {
83 if let BinOpKind::Add = op2.node {
84 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
85 "You are trying to use classic C overflow conditions that will fail in Rust.");
88 if let BinOpKind::Lt = op.node {
89 if let BinOpKind::Sub = op2.node {
90 span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
91 "You are trying to use classic C underflow conditions that will fail in Rust.");