Skip to content

Commit

Permalink
Create new inverted_saturating_sub lint
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Aug 29, 2024
1 parent 27c6343 commit d20fc38
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5500,6 +5500,7 @@ Released 2018-09-13
[`invalid_regex`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_regex
[`invalid_upcast_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_upcast_comparisons
[`invalid_utf8_in_unchecked`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_utf8_in_unchecked
[`inverted_saturating_sub`]: https://rust-lang.github.io/rust-clippy/master/index.html#inverted_saturating_sub
[`invisible_characters`]: https://rust-lang.github.io/rust-clippy/master/index.html#invisible_characters
[`is_digit_ascii_radix`]: https://rust-lang.github.io/rust-clippy/master/index.html#is_digit_ascii_radix
[`items_after_statements`]: https://rust-lang.github.io/rust-clippy/master/index.html#items_after_statements
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
crate::implicit_return::IMPLICIT_RETURN_INFO,
crate::implicit_saturating_add::IMPLICIT_SATURATING_ADD_INFO,
crate::implicit_saturating_sub::IMPLICIT_SATURATING_SUB_INFO,
crate::implicit_saturating_sub::INVERTED_SATURATING_SUB_INFO,
crate::implied_bounds_in_impls::IMPLIED_BOUNDS_IN_IMPLS_INFO,
crate::incompatible_msrv::INCOMPATIBLE_MSRV_INFO,
crate::inconsistent_struct_constructor::INCONSISTENT_STRUCT_CONSTRUCTOR_INFO,
Expand Down
34 changes: 32 additions & 2 deletions clippy_lints/src/implicit_saturating_sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,37 @@ declare_clippy_lint! {
"Perform saturating subtraction instead of implicitly checking lower bound of data type"
}

declare_lint_pass!(ImplicitSaturatingSub => [IMPLICIT_SATURATING_SUB]);
declare_clippy_lint! {
/// ### What it does
/// Checks for comparisons between integers, followed by subtracting the greater value from the
/// lower one.
///
/// ### Why is this bad?
/// This could result in an underflow and is most likely not what the user wants. If this was
/// intended to be a saturated subtraction, consider using the `saturating_sub` method directly.
///
/// ### Example
/// ```no_run
/// let a = 12u32;
/// let b = 13u32;
///
/// let result = if a > b { b - a } else { 0 };
/// ```
///
/// Use instead:
/// ```no_run
/// let a = 12u32;
/// let b = 13u32;
///
/// let result = a.saturating_sub(b);
/// ```
#[clippy::version = "1.44.0"]
pub INVERTED_SATURATING_SUB,
correctness,
"Check if a variable is smaller than another one and still subtract from it even if smaller"
}

declare_lint_pass!(ImplicitSaturatingSub => [IMPLICIT_SATURATING_SUB, INVERTED_SATURATING_SUB]);

impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
Expand Down Expand Up @@ -182,7 +212,7 @@ fn check_subtraction(
{
span_lint_and_then(
cx,
IMPLICIT_SATURATING_SUB,
INVERTED_SATURATING_SUB,
condition_span,
"inverted arithmetic check before subtraction",
|diag| {
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/manual_arithmetic_check-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ note: this subtraction underflows when `b < a`
|
LL | let result = if a > b { b - a } else { 0 };
| ^^^^^
= note: `-D clippy::implicit-saturating-sub` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::implicit_saturating_sub)]`
= note: `#[deny(clippy::inverted_saturating_sub)]` on by default

error: inverted arithmetic check before subtraction
--> tests/ui/manual_arithmetic_check-2.rs:11:23
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/manual_arithmetic_check.fixed
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![warn(clippy::implicit_saturating_sub)]
#![warn(clippy::implicit_saturating_sub, clippy::inverted_saturating_sub)]
#![allow(clippy::if_same_then_else)]

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/manual_arithmetic_check.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![warn(clippy::implicit_saturating_sub)]
#![warn(clippy::implicit_saturating_sub, clippy::inverted_saturating_sub)]
#![allow(clippy::if_same_then_else)]

fn main() {
Expand Down

0 comments on commit d20fc38

Please sign in to comment.