diff options
| author | Philipp Krones <hello@philkrones.com> | 2020-04-15 20:12:29 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-15 20:12:29 +0200 |
| commit | 2538e6388590fe592cd41c7d38bb1ade63f7d309 (patch) | |
| tree | bb6b04904ce7f089d67d575b79d209c4abd62b2d | |
| parent | ceea3c6a35bfada9536bac674fc6308831f1938c (diff) | |
| parent | 23df4a0183e0d954d47db98824295411d50f742e (diff) | |
| download | rust-2538e6388590fe592cd41c7d38bb1ade63f7d309.tar.gz rust-2538e6388590fe592cd41c7d38bb1ade63f7d309.zip | |
Rollup merge of #5430 - michaelsproul:integer-arithmetic, r=flip1995
Disallow bit-shifting in integer_arithmetic Make the `integer_arithmetic` lint detect all the operations that are defined as being capable of overflow in the [Rust Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow), by also linting for bit-shifting operations (`<<`, `>>`). changelog: Disallow bit-shifting in `integer_arithmetic`
| -rw-r--r-- | clippy_lints/src/arithmetic.rs | 18 | ||||
| -rw-r--r-- | src/lintlist/mod.rs | 2 | ||||
| -rw-r--r-- | tests/ui/integer_arithmetic.rs | 10 | ||||
| -rw-r--r-- | tests/ui/integer_arithmetic.stderr | 40 |
4 files changed, 48 insertions, 22 deletions
diff --git a/clippy_lints/src/arithmetic.rs b/clippy_lints/src/arithmetic.rs index a138c9d3545..6cbe10a5352 100644 --- a/clippy_lints/src/arithmetic.rs +++ b/clippy_lints/src/arithmetic.rs @@ -6,11 +6,17 @@ use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::source_map::Span; declare_clippy_lint! { - /// **What it does:** Checks for plain integer arithmetic. + /// **What it does:** Checks for integer arithmetic operations which could overflow or panic. /// - /// **Why is this bad?** This is only checked against overflow in debug builds. - /// In some applications one wants explicitly checked, wrapping or saturating - /// arithmetic. + /// Specifically, checks for any operators (`+`, `-`, `*`, `<<`, etc) which are capable + /// of overflowing according to the [Rust + /// Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow), + /// or which can panic (`/`, `%`). No bounds analysis or sophisticated reasoning is + /// attempted. + /// + /// **Why is this bad?** Integer overflow will trigger a panic in debug builds or will wrap in + /// release mode. Division by zero will cause a panic in either mode. In some applications one + /// wants explicitly checked, wrapping or saturating arithmetic. /// /// **Known problems:** None. /// @@ -21,7 +27,7 @@ declare_clippy_lint! { /// ``` pub INTEGER_ARITHMETIC, restriction, - "any integer arithmetic statement" + "any integer arithmetic expression which could overflow or panic" } declare_clippy_lint! { @@ -71,8 +77,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic { | hir::BinOpKind::BitAnd | hir::BinOpKind::BitOr | hir::BinOpKind::BitXor - | hir::BinOpKind::Shl - | hir::BinOpKind::Shr | hir::BinOpKind::Eq | hir::BinOpKind::Lt | hir::BinOpKind::Le diff --git a/src/lintlist/mod.rs b/src/lintlist/mod.rs index ca1faf6cfb7..d4602a3ad8e 100644 --- a/src/lintlist/mod.rs +++ b/src/lintlist/mod.rs @@ -853,7 +853,7 @@ pub static ref ALL_LINTS: Vec<Lint> = vec