From 55fbf59f3bec1425e03b56516050912ed02147c9 Mon Sep 17 00:00:00 2001 From: KALPESH KRISHNA Date: Sun, 6 Mar 2016 20:31:17 +0530 Subject: [PATCH 1/3] Linting classical overflow checks. --- README.md | 3 +- src/lib.rs | 3 ++ src/overflow_check_conditional.rs | 41 +++++++++++++++++++ .../overflow_check_conditional.rs | 25 +++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/overflow_check_conditional.rs create mode 100644 tests/compile-fail/overflow_check_conditional.rs diff --git a/README.md b/README.md index ff875085da21..77518df6b399 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A collection of lints to catch common mistakes and improve your Rust code. [Jump to usage instructions](#usage) ##Lints -There are 130 lints included in this crate: +There are 131 lints included in this crate: name | default | meaning ---------------------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ @@ -92,6 +92,7 @@ name [option_unwrap_used](https://github.com/Manishearth/rust-clippy/wiki#option_unwrap_used) | allow | using `Option.unwrap()`, which should at least get a better message using `expect()` [or_fun_call](https://github.com/Manishearth/rust-clippy/wiki#or_fun_call) | warn | using any `*or` method when the `*or_else` would do [out_of_bounds_indexing](https://github.com/Manishearth/rust-clippy/wiki#out_of_bounds_indexing) | deny | out of bound constant indexing +[overflow_check_conditional](https://github.com/Manishearth/rust-clippy/wiki#overflow_check_conditional) | warn | Using overflow checks which are likely to panic [panic_params](https://github.com/Manishearth/rust-clippy/wiki#panic_params) | warn | missing parameters in `panic!` [precedence](https://github.com/Manishearth/rust-clippy/wiki#precedence) | warn | catches operations where precedence may be unclear. See the wiki for a list of cases caught [print_stdout](https://github.com/Manishearth/rust-clippy/wiki#print_stdout) | allow | printing on stdout diff --git a/src/lib.rs b/src/lib.rs index 803a4c2345c3..51292bea8b2f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -79,6 +79,7 @@ pub mod needless_features; pub mod needless_update; pub mod no_effect; pub mod open_options; +pub mod overflow_check_conditional; pub mod panic; pub mod precedence; pub mod print; @@ -173,6 +174,7 @@ pub fn plugin_registrar(reg: &mut Registry) { reg.register_early_lint_pass(box formatting::Formatting); reg.register_late_lint_pass(box swap::Swap); reg.register_early_lint_pass(box if_not_else::IfNotElse); + reg.register_late_lint_pass(box overflow_check_conditional::OverflowCheckConditional); reg.register_lint_group("clippy_pedantic", vec![ enum_glob_use::ENUM_GLOB_USE, @@ -283,6 +285,7 @@ pub fn plugin_registrar(reg: &mut Registry) { needless_update::NEEDLESS_UPDATE, no_effect::NO_EFFECT, open_options::NONSENSICAL_OPEN_OPTIONS, + overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL, panic::PANIC_PARAMS, precedence::PRECEDENCE, ptr_arg::PTR_ARG, diff --git a/src/overflow_check_conditional.rs b/src/overflow_check_conditional.rs new file mode 100644 index 000000000000..7f4f4b3597cd --- /dev/null +++ b/src/overflow_check_conditional.rs @@ -0,0 +1,41 @@ +use rustc::lint::*; +use rustc_front::hir::*; +use utils::{span_lint}; + +/// **What it does:** This lint finds classic overflow checks. +/// +/// **Why is this bad?** Most classic C overflow checks will fail in Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead. +/// +/// **Known problems:** None. +/// +/// **Example:** `a + b < a` +declare_lint!(pub OVERFLOW_CHECK_CONDITIONAL, Warn, + "Using overflow checks which are likely to panic"); + +#[derive(Copy, Clone)] +pub struct OverflowCheckConditional; + +impl LintPass for OverflowCheckConditional { + fn get_lints(&self) -> LintArray { + lint_array!(OVERFLOW_CHECK_CONDITIONAL) + } +} + +impl LateLintPass for OverflowCheckConditional { + fn check_expr(&mut self, cx: &LateContext, expr: &Expr) { + if_let_chain! {[ + let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node, + let BinOp_::BiLt = op.node, + let Expr_::ExprBinary(ref op2, ref add1, ref add2) = first.node, + let BinOp_::BiAdd = op2.node, + let Expr_::ExprPath(_,ref path1) = add1.node, + let Expr_::ExprPath(_, ref path2) = add2.node, + let Expr_::ExprPath(_, ref path3) = second.node, + (&path1.segments[0]).identifier == (&path3.segments[0]).identifier || (&path2.segments[0]).identifier == (&path3.segments[0]).identifier, + cx.tcx.expr_ty(add1).is_integral(), + cx.tcx.expr_ty(add2).is_integral() + ], { + span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust."); + }} + } +} diff --git a/tests/compile-fail/overflow_check_conditional.rs b/tests/compile-fail/overflow_check_conditional.rs new file mode 100644 index 000000000000..0b5e486df469 --- /dev/null +++ b/tests/compile-fail/overflow_check_conditional.rs @@ -0,0 +1,25 @@ +#![feature(plugin)] +#![plugin(clippy)] + +#![deny(overflow_check_conditional)] + +fn main() { + let a: u32 = 1; + let b: u32 = 2; + let c: u32 = 3; + if a + b < a { //~ERROR You are trying to use classic C overflow conditons that will fail in Rust. + + } + if a + b < b { //~ERROR You are trying to use classic C overflow conditons that will fail in Rust. + + } + if a + b < c { + + } + let i = 1.1; + let j = 2.2; + if i + j < i { + + } +} + From d6d409414ea82866bac7f4c7698c16f9884a1b9b Mon Sep 17 00:00:00 2001 From: KALPESH KRISHNA Date: Tue, 8 Mar 2016 02:57:45 +0530 Subject: [PATCH 2/3] Adding underflow checks and tests --- src/overflow_check_conditional.rs | 15 +++++++++++++++ tests/compile-fail/overflow_check_conditional.rs | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/overflow_check_conditional.rs b/src/overflow_check_conditional.rs index 7f4f4b3597cd..c89a3dd27327 100644 --- a/src/overflow_check_conditional.rs +++ b/src/overflow_check_conditional.rs @@ -37,5 +37,20 @@ impl LateLintPass for OverflowCheckConditional { ], { span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust."); }} + + if_let_chain! {[ + let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node, + let BinOp_::BiGt = op.node, + let Expr_::ExprBinary(ref op2, ref sub1, ref sub2) = first.node, + let BinOp_::BiSub = op2.node, + let Expr_::ExprPath(_,ref path1) = sub1.node, + let Expr_::ExprPath(_, ref path2) = sub2.node, + let Expr_::ExprPath(_, ref path3) = second.node, + (&path1.segments[0]).identifier == (&path3.segments[0]).identifier || (&path2.segments[0]).identifier == (&path3.segments[0]).identifier, + cx.tcx.expr_ty(sub1).is_integral(), + cx.tcx.expr_ty(sub2).is_integral() + ], { + span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditons that will fail in Rust."); + }} } } diff --git a/tests/compile-fail/overflow_check_conditional.rs b/tests/compile-fail/overflow_check_conditional.rs index 0b5e486df469..a59fa2a444a5 100644 --- a/tests/compile-fail/overflow_check_conditional.rs +++ b/tests/compile-fail/overflow_check_conditional.rs @@ -12,14 +12,26 @@ fn main() { } if a + b < b { //~ERROR You are trying to use classic C overflow conditons that will fail in Rust. + } + if a - b > b { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust. + + } + if a - b > a { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust. + } if a + b < c { + } + if a - b < c { + } let i = 1.1; let j = 2.2; if i + j < i { + } + if i - j < i { + } } From 9faffd28705b39c37526f964b637bb405e348f35 Mon Sep 17 00:00:00 2001 From: KALPESH KRISHNA Date: Tue, 8 Mar 2016 11:03:30 +0530 Subject: [PATCH 3/3] Adding symmetric lints and test cases --- src/overflow_check_conditional.rs | 55 ++++++++++++------- .../overflow_check_conditional.rs | 24 ++++++++ 2 files changed, 60 insertions(+), 19 deletions(-) diff --git a/src/overflow_check_conditional.rs b/src/overflow_check_conditional.rs index c89a3dd27327..2dccbfdb26ac 100644 --- a/src/overflow_check_conditional.rs +++ b/src/overflow_check_conditional.rs @@ -1,14 +1,16 @@ +#![allow(cyclomatic_complexity)] use rustc::lint::*; use rustc_front::hir::*; use utils::{span_lint}; -/// **What it does:** This lint finds classic overflow checks. +/// **What it does:** This lint finds classic underflow / overflow checks. /// -/// **Why is this bad?** Most classic C overflow checks will fail in Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead. +/// **Why is this bad?** Most classic C underflow / overflow checks will fail in Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead. /// /// **Known problems:** None. /// /// **Example:** `a + b < a` + declare_lint!(pub OVERFLOW_CHECK_CONDITIONAL, Warn, "Using overflow checks which are likely to panic"); @@ -22,35 +24,50 @@ impl LintPass for OverflowCheckConditional { } impl LateLintPass for OverflowCheckConditional { + // a + b < a, a > a + b, a < a - b, a - b > a fn check_expr(&mut self, cx: &LateContext, expr: &Expr) { if_let_chain! {[ let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node, - let BinOp_::BiLt = op.node, - let Expr_::ExprBinary(ref op2, ref add1, ref add2) = first.node, - let BinOp_::BiAdd = op2.node, - let Expr_::ExprPath(_,ref path1) = add1.node, - let Expr_::ExprPath(_, ref path2) = add2.node, + let Expr_::ExprBinary(ref op2, ref ident1, ref ident2) = first.node, + let Expr_::ExprPath(_,ref path1) = ident1.node, + let Expr_::ExprPath(_, ref path2) = ident2.node, let Expr_::ExprPath(_, ref path3) = second.node, (&path1.segments[0]).identifier == (&path3.segments[0]).identifier || (&path2.segments[0]).identifier == (&path3.segments[0]).identifier, - cx.tcx.expr_ty(add1).is_integral(), - cx.tcx.expr_ty(add2).is_integral() + cx.tcx.expr_ty(ident1).is_integral(), + cx.tcx.expr_ty(ident2).is_integral() ], { - span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust."); + if let BinOp_::BiLt = op.node { + if let BinOp_::BiAdd = op2.node { + span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust."); + } + } + if let BinOp_::BiGt = op.node { + if let BinOp_::BiSub = op2.node { + span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditons that will fail in Rust."); + } + } }} if_let_chain! {[ let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node, - let BinOp_::BiGt = op.node, - let Expr_::ExprBinary(ref op2, ref sub1, ref sub2) = first.node, - let BinOp_::BiSub = op2.node, - let Expr_::ExprPath(_,ref path1) = sub1.node, - let Expr_::ExprPath(_, ref path2) = sub2.node, - let Expr_::ExprPath(_, ref path3) = second.node, + let Expr_::ExprBinary(ref op2, ref ident1, ref ident2) = second.node, + let Expr_::ExprPath(_,ref path1) = ident1.node, + let Expr_::ExprPath(_, ref path2) = ident2.node, + let Expr_::ExprPath(_, ref path3) = first.node, (&path1.segments[0]).identifier == (&path3.segments[0]).identifier || (&path2.segments[0]).identifier == (&path3.segments[0]).identifier, - cx.tcx.expr_ty(sub1).is_integral(), - cx.tcx.expr_ty(sub2).is_integral() + cx.tcx.expr_ty(ident1).is_integral(), + cx.tcx.expr_ty(ident2).is_integral() ], { - span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditons that will fail in Rust."); + if let BinOp_::BiGt = op.node { + if let BinOp_::BiAdd = op2.node { + span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust."); + } + } + if let BinOp_::BiLt = op.node { + if let BinOp_::BiSub = op2.node { + span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditons that will fail in Rust."); + } + } }} } } diff --git a/tests/compile-fail/overflow_check_conditional.rs b/tests/compile-fail/overflow_check_conditional.rs index a59fa2a444a5..df629146deec 100644 --- a/tests/compile-fail/overflow_check_conditional.rs +++ b/tests/compile-fail/overflow_check_conditional.rs @@ -9,21 +9,39 @@ fn main() { let c: u32 = 3; if a + b < a { //~ERROR You are trying to use classic C overflow conditons that will fail in Rust. + } + if a > a + b { //~ERROR You are trying to use classic C overflow conditons that will fail in Rust. + } if a + b < b { //~ERROR You are trying to use classic C overflow conditons that will fail in Rust. + } + if b > a + b { //~ERROR You are trying to use classic C overflow conditons that will fail in Rust. + } if a - b > b { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust. + } + if b < a - b { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust. + } if a - b > a { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust. + } + if a < a - b { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust. + } if a + b < c { + } + if c > a + b { + } if a - b < c { + } + if c > a - b { + } let i = 1.1; let j = 2.2; @@ -32,6 +50,12 @@ fn main() { } if i - j < i { + } + if i > i + j { + + } + if i - j < i { + } }