Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5947,6 +5947,7 @@ Released 2018-09-13
[`allow_attributes_without_reason`]: https://rust-lang.github.io/rust-clippy/master/index.html#allow_attributes_without_reason
[`almost_complete_letter_range`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_complete_letter_range
[`almost_complete_range`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_complete_range
[`almost_saturating_sub`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_saturating_sub
[`almost_swapped`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_swapped
[`approx_constant`]: https://rust-lang.github.io/rust-clippy/master/index.html#approx_constant
[`arbitrary_source_item_ordering`]: https://rust-lang.github.io/rust-clippy/master/index.html#arbitrary_source_item_ordering
Expand Down Expand Up @@ -6323,7 +6324,9 @@ Released 2018-09-13
[`manual_repeat_n`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_repeat_n
[`manual_retain`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_retain
[`manual_rotate`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_rotate
[`manual_saturating_add`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_saturating_add
[`manual_saturating_arithmetic`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_saturating_arithmetic
[`manual_saturating_sub`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_saturating_sub
[`manual_slice_fill`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_slice_fill
[`manual_slice_size_calculation`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_slice_size_calculation
[`manual_split_once`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_split_once
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,6 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
crate::impl_hash_with_borrow_str_and_bytes::IMPL_HASH_BORROW_WITH_STR_AND_BYTES_INFO,
crate::implicit_hasher::IMPLICIT_HASHER_INFO,
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 Expand Up @@ -313,6 +310,9 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
crate::manual_rem_euclid::MANUAL_REM_EUCLID_INFO,
crate::manual_retain::MANUAL_RETAIN_INFO,
crate::manual_rotate::MANUAL_ROTATE_INFO,
crate::manual_saturating_add::MANUAL_SATURATING_ADD_INFO,
crate::manual_saturating_sub::ALMOST_SATURATING_SUB_INFO,
crate::manual_saturating_sub::MANUAL_SATURATING_SUB_INFO,
crate::manual_slice_size_calculation::MANUAL_SLICE_SIZE_CALCULATION_INFO,
crate::manual_string_new::MANUAL_STRING_NEW_INFO,
crate::manual_strip::MANUAL_STRIP_INFO,
Expand Down
6 changes: 6 additions & 0 deletions clippy_lints/src/deprecated_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ declare_with_version! { RENAMED(RENAMED_VERSION) = [
("clippy::if_let_redundant_pattern_matching", "clippy::redundant_pattern_matching"),
#[clippy::version = ""]
("clippy::if_let_some_result", "clippy::match_result_ok"),
#[clippy::version = "1.91.0"]
("clippy::implicit_saturating_add", "clippy::manual_saturating_add"),
#[clippy::version = "1.91.0"]
("clippy::implicit_saturating_sub", "clippy::manual_saturating_sub"),
#[clippy::version = ""]
("clippy::incorrect_clone_impl_on_copy_type", "clippy::non_canonical_clone_impl"),
#[clippy::version = ""]
Expand All @@ -127,6 +131,8 @@ declare_with_version! { RENAMED(RENAMED_VERSION) = [
("clippy::invalid_ref", "invalid_value"),
#[clippy::version = ""]
("clippy::invalid_utf8_in_unchecked", "invalid_from_utf8_unchecked"),
#[clippy::version = "1.91.0"]
("clippy::inverted_saturating_sub", "clippy::almost_saturating_sub"),
#[clippy::version = ""]
("clippy::let_underscore_drop", "let_underscore_drop"),
#[clippy::version = ""]
Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ mod ignored_unit_patterns;
mod impl_hash_with_borrow_str_and_bytes;
mod implicit_hasher;
mod implicit_return;
mod implicit_saturating_add;
mod implicit_saturating_sub;
mod implied_bounds_in_impls;
mod incompatible_msrv;
mod inconsistent_struct_constructor;
Expand Down Expand Up @@ -222,6 +220,8 @@ mod manual_range_patterns;
mod manual_rem_euclid;
mod manual_retain;
mod manual_rotate;
mod manual_saturating_add;
mod manual_saturating_sub;
mod manual_slice_size_calculation;
mod manual_string_new;
mod manual_strip;
Expand Down Expand Up @@ -493,7 +493,7 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
store.register_late_pass(|_| Box::new(unit_return_expecting_ord::UnitReturnExpectingOrd));
store.register_late_pass(|_| Box::new(strings::StringAdd));
store.register_late_pass(|_| Box::new(implicit_return::ImplicitReturn));
store.register_late_pass(move |_| Box::new(implicit_saturating_sub::ImplicitSaturatingSub::new(conf)));
store.register_late_pass(move |_| Box::new(manual_saturating_sub::ManualSaturatingSub::new(conf)));
store.register_late_pass(|_| Box::new(default_numeric_fallback::DefaultNumericFallback));
store.register_late_pass(move |_| {
Box::new(inconsistent_struct_constructor::InconsistentStructConstructor::new(
Expand Down Expand Up @@ -727,7 +727,7 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
store.register_early_pass(|| Box::new(multi_assignments::MultiAssignments));
store.register_late_pass(|_| Box::new(bool_to_int_with_if::BoolToIntWithIf));
store.register_late_pass(|_| Box::new(box_default::BoxDefault));
store.register_late_pass(|_| Box::new(implicit_saturating_add::ImplicitSaturatingAdd));
store.register_late_pass(|_| Box::new(manual_saturating_add::ManualSaturatingAdd));
store.register_early_pass(|| Box::new(partial_pub_fields::PartialPubFields));
store.register_late_pass(|_| Box::new(missing_trait_methods::MissingTraitMethods));
store.register_late_pass(|_| Box::new(from_raw_with_void_ptr::FromRawWithVoidPtr));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ declare_clippy_lint! {
/// u = u.saturating_add(1);
/// ```
#[clippy::version = "1.66.0"]
pub IMPLICIT_SATURATING_ADD,
pub MANUAL_SATURATING_ADD,
style,
"Perform saturating addition instead of implicitly checking max bound of data type"
}
declare_lint_pass!(ImplicitSaturatingAdd => [IMPLICIT_SATURATING_ADD]);
declare_lint_pass!(ManualSaturatingAdd => [MANUAL_SATURATING_ADD]);

impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingAdd {
impl<'tcx> LateLintPass<'tcx> for ManualSaturatingAdd {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
if let ExprKind::If(cond, then, None) = expr.kind
&& let Some((c, op_node, l)) = get_const(cx, cond)
Expand Down Expand Up @@ -84,7 +84,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingAdd {
};
span_lint_and_sugg(
cx,
IMPLICIT_SATURATING_ADD,
MANUAL_SATURATING_ADD,
expr.span,
"manual saturating add detected",
"use instead",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ declare_clippy_lint! {
/// i = i.saturating_sub(1);
/// ```
#[clippy::version = "1.44.0"]
pub IMPLICIT_SATURATING_SUB,
pub MANUAL_SATURATING_SUB,
style,
"Perform saturating subtraction instead of implicitly checking lower bound of data type"
}
Expand Down Expand Up @@ -70,24 +70,24 @@ declare_clippy_lint! {
/// let result = a.saturating_sub(b);
/// ```
#[clippy::version = "1.83.0"]
pub INVERTED_SATURATING_SUB,
pub ALMOST_SATURATING_SUB,
correctness,
"Check if a variable is smaller than another one and still subtract from it even if smaller"
}

pub struct ImplicitSaturatingSub {
pub struct ManualSaturatingSub {
msrv: Msrv,
}

impl_lint_pass!(ImplicitSaturatingSub => [IMPLICIT_SATURATING_SUB, INVERTED_SATURATING_SUB]);
impl_lint_pass!(ManualSaturatingSub => [MANUAL_SATURATING_SUB, ALMOST_SATURATING_SUB]);

impl ImplicitSaturatingSub {
impl ManualSaturatingSub {
pub fn new(conf: &'static Conf) -> Self {
Self { msrv: conf.msrv }
}
}

impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
impl<'tcx> LateLintPass<'tcx> for ManualSaturatingSub {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
if expr.span.from_expansion() {
return;
Expand Down Expand Up @@ -249,7 +249,7 @@ fn check_subtraction(
);
span_lint_and_sugg(
cx,
IMPLICIT_SATURATING_SUB,
MANUAL_SATURATING_SUB,
expr_span,
"manual arithmetic check found",
"replace it with",
Expand All @@ -265,7 +265,7 @@ fn check_subtraction(
let sugg = make_binop(BinOpKind::Sub, &big_expr_sugg, &little_expr_sugg);
span_lint_and_then(
cx,
INVERTED_SATURATING_SUB,
ALMOST_SATURATING_SUB,
condition_span,
"inverted arithmetic check before subtraction",
|diag| {
Expand Down Expand Up @@ -386,7 +386,7 @@ fn subtracts_one<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<&'a Exp
fn print_lint_and_sugg(cx: &LateContext<'_>, var_name: Symbol, expr: &Expr<'_>) {
span_lint_and_sugg(
cx,
IMPLICIT_SATURATING_SUB,
MANUAL_SATURATING_SUB,
expr.span,
"implicitly performing saturating subtraction",
"try",
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/manual_abs_diff.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn fixme() {
}
}

#[allow(clippy::implicit_saturating_sub)]
#[allow(clippy::manual_saturating_sub)]
let _ = if a > b {
a - b
} else if a < b {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/manual_abs_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn fixme() {
}
}

#[allow(clippy::implicit_saturating_sub)]
#[allow(clippy::manual_saturating_sub)]
let _ = if a > b {
a - b
} else if a < b {
Expand Down
20 changes: 10 additions & 10 deletions tests/ui/manual_arithmetic_check-2.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
//@no-rustfix
#![warn(clippy::implicit_saturating_sub)]
#![warn(clippy::manual_saturating_sub)]
#![allow(arithmetic_overflow)]

fn main() {
let a = 12u32;
let b = 13u32;

let result = if a > b { b - a } else { 0 };
//~^ inverted_saturating_sub
//~^ almost_saturating_sub

let result = if b < a { b - a } else { 0 };
//~^ inverted_saturating_sub
//~^ almost_saturating_sub

let result = if a > b { 0 } else { a - b };
//~^ inverted_saturating_sub
//~^ almost_saturating_sub

let result = if a >= b { 0 } else { a - b };
//~^ inverted_saturating_sub
//~^ almost_saturating_sub

let result = if b < a { 0 } else { a - b };
//~^ inverted_saturating_sub
//~^ almost_saturating_sub

let result = if b <= a { 0 } else { a - b };
//~^ inverted_saturating_sub
//~^ almost_saturating_sub

let result = if b * 2 <= a { 0 } else { a - b * 2 };
//~^ inverted_saturating_sub
//~^ almost_saturating_sub

let result = if b <= a * 2 { 0 } else { a * 2 - b };
//~^ inverted_saturating_sub
//~^ almost_saturating_sub

let result = if b + 3 <= a + 2 { 0 } else { (a + 2) - (b + 3) };
//~^ inverted_saturating_sub
//~^ almost_saturating_sub

let af = 12f32;
let bf = 13f32;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/manual_arithmetic_check-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ note: this subtraction underflows when `b < a`
|
LL | let result = if a > b { b - a } else { 0 };
| ^^^^^
= note: `#[deny(clippy::inverted_saturating_sub)]` on by default
= note: `#[deny(clippy::almost_saturating_sub)]` on by default

error: inverted arithmetic check before subtraction
--> tests/ui/manual_arithmetic_check-2.rs:12:23
Expand Down
10 changes: 5 additions & 5 deletions 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, clippy::inverted_saturating_sub)]
#![warn(clippy::manual_saturating_sub, clippy::almost_saturating_sub)]
#![allow(clippy::if_same_then_else)]

fn main() {
Expand All @@ -7,16 +7,16 @@ fn main() {
let c = 8u32;

let result = a.saturating_sub(b);
//~^ implicit_saturating_sub
//~^ manual_saturating_sub

let result = a.saturating_sub(b);
//~^ implicit_saturating_sub
//~^ manual_saturating_sub

let result = a.saturating_sub(b);
//~^ implicit_saturating_sub
//~^ manual_saturating_sub

let result = a.saturating_sub(b);
//~^ implicit_saturating_sub
//~^ manual_saturating_sub

// Should not warn!
let result = if a > b { a - b } else { a - c };
Expand Down
10 changes: 5 additions & 5 deletions 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, clippy::inverted_saturating_sub)]
#![warn(clippy::manual_saturating_sub, clippy::almost_saturating_sub)]
#![allow(clippy::if_same_then_else)]

fn main() {
Expand All @@ -7,16 +7,16 @@ fn main() {
let c = 8u32;

let result = if a > b { a - b } else { 0 };
//~^ implicit_saturating_sub
//~^ manual_saturating_sub

let result = if b < a { a - b } else { 0 };
//~^ implicit_saturating_sub
//~^ manual_saturating_sub

let result = if a < b { 0 } else { a - b };
//~^ implicit_saturating_sub
//~^ manual_saturating_sub

let result = if b > a { 0 } else { a - b };
//~^ implicit_saturating_sub
//~^ manual_saturating_sub

// Should not warn!
let result = if a > b { a - b } else { a - c };
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/manual_arithmetic_check.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ error: manual arithmetic check found
LL | let result = if a > b { a - b } else { 0 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.saturating_sub(b)`
|
= note: `-D clippy::implicit-saturating-sub` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::implicit_saturating_sub)]`
= note: `-D clippy::manual-saturating-sub` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::manual_saturating_sub)]`

error: manual arithmetic check found
--> tests/ui/manual_arithmetic_check.rs:12:18
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(unused)]
#![warn(clippy::implicit_saturating_add)]
#![warn(clippy::manual_saturating_add)]

fn main() {
let mut u_8: u8 = 255;
Expand Down
Loading