about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJason Newcomb <jsnewcomb@pm.me>2024-06-16 20:53:10 -0400
committerJason Newcomb <jsnewcomb@pm.me>2024-07-07 10:24:46 -0400
commitd2ff2b98507d0abaa2c878f74035774f9d9bcd95 (patch)
treec6e26d3d2641220e10d27f0ef6ddab70b9eae292
parent23d96f65e42c3451dae2b70abfe0a0f709225021 (diff)
downloadrust-d2ff2b98507d0abaa2c878f74035774f9d9bcd95.tar.gz
rust-d2ff2b98507d0abaa2c878f74035774f9d9bcd95.zip
Move `panicking_overflow_checks` into `correctness` and clean up docs.
-rw-r--r--clippy_lints/src/panicking_overflow_checks.rs36
1 files changed, 28 insertions, 8 deletions
diff --git a/clippy_lints/src/panicking_overflow_checks.rs b/clippy_lints/src/panicking_overflow_checks.rs
index c9c2935b3ae..7f100a746d5 100644
--- a/clippy_lints/src/panicking_overflow_checks.rs
+++ b/clippy_lints/src/panicking_overflow_checks.rs
@@ -8,22 +8,42 @@ use rustc_session::declare_lint_pass;
 
 declare_clippy_lint! {
     /// ### What it does
-    /// Detects classic underflow/overflow checks.
+    /// Detects C-style underflow/overflow checks.
     ///
     /// ### Why is this bad?
-    /// Most classic C underflow/overflow checks will fail in
-    /// Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead.
+    /// These checks will, by default, panic in debug builds rather than check
+    /// whether the operation caused an overflow.
     ///
     /// ### Example
     /// ```no_run
-    /// # let a = 1;
-    /// # let b = 2;
-    /// a + b < a;
+    /// # let a = 1i32;
+    /// # let b = 2i32;
+    /// if a + b < a {
+    ///     // handle overflow
+    /// }
+    /// ```
+    ///
+    /// Use instead:
+    /// ```no_run
+    /// # let a = 1i32;
+    /// # let b = 2i32;
+    /// if a.checked_add(b).is_none() {
+    ///     // handle overflow
+    /// }
+    /// ```
+    ///
+    /// Or:
+    /// ```no_run
+    /// # let a = 1i32;
+    /// # let b = 2i32;
+    /// if a.overflowing_add(b).1 {
+    ///     // handle overflow
+    /// }
     /// ```
     #[clippy::version = "pre 1.29.0"]
     pub PANICKING_OVERFLOW_CHECKS,
-    complexity,
-    "overflow checks inspired by C which are likely to panic"
+    correctness,
+    "overflow checks which will panic in debug mode"
 }
 
 declare_lint_pass!(PanickingOverflowChecks => [PANICKING_OVERFLOW_CHECKS]);