diff --git a/CHANGELOG.md b/CHANGELOG.md index 529ab6c20d62..7a2c23bc8ba0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5718,6 +5718,7 @@ Released 2018-09-13 [`manual_main_separator_str`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_main_separator_str [`manual_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_map [`manual_memcpy`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_memcpy +[`manual_midpoint`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_midpoint [`manual_next_back`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_next_back [`manual_non_exhaustive`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_non_exhaustive [`manual_ok_or`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_ok_or diff --git a/book/src/lint_configuration.md b/book/src/lint_configuration.md index 275d125096e9..74a55c19afe0 100644 --- a/book/src/lint_configuration.md +++ b/book/src/lint_configuration.md @@ -709,6 +709,7 @@ The minimum rust version that the project supports. Defaults to the `rust-versio * [`manual_hash_one`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_hash_one) * [`manual_is_ascii_check`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_is_ascii_check) * [`manual_let_else`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_let_else) +* [`manual_midpoint`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_midpoint) * [`manual_non_exhaustive`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_non_exhaustive) * [`manual_pattern_char_comparison`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_pattern_char_comparison) * [`manual_range_contains`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_range_contains) diff --git a/clippy_config/src/conf.rs b/clippy_config/src/conf.rs index 41b56b45d9ae..96383d78d61a 100644 --- a/clippy_config/src/conf.rs +++ b/clippy_config/src/conf.rs @@ -592,6 +592,7 @@ define_Conf! { manual_hash_one, manual_is_ascii_check, manual_let_else, + manual_midpoint, manual_non_exhaustive, manual_pattern_char_comparison, manual_range_contains, diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index 7451fb909ef8..6e956394d2a2 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -593,6 +593,7 @@ pub static LINTS: &[&crate::LintInfo] = &[ crate::operators::IMPOSSIBLE_COMPARISONS_INFO, crate::operators::INEFFECTIVE_BIT_MASK_INFO, crate::operators::INTEGER_DIVISION_INFO, + crate::operators::MANUAL_MIDPOINT_INFO, crate::operators::MISREFACTORED_ASSIGN_OP_INFO, crate::operators::MODULO_ARITHMETIC_INFO, crate::operators::MODULO_ONE_INFO, diff --git a/clippy_lints/src/operators/manual_midpoint.rs b/clippy_lints/src/operators/manual_midpoint.rs new file mode 100644 index 000000000000..f9f2a0455afa --- /dev/null +++ b/clippy_lints/src/operators/manual_midpoint.rs @@ -0,0 +1,56 @@ +use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; +use clippy_utils::sugg::Sugg; +use clippy_utils::{is_floating_point_integer_literal, is_integer_literal}; +use rustc_ast::BinOpKind; +use rustc_errors::Applicability; +use rustc_hir::{Expr, ExprKind}; +use rustc_lint::LateContext; +use rustc_middle::ty; + +use super::MANUAL_MIDPOINT; + +pub(super) fn check<'tcx>( + cx: &LateContext<'tcx>, + expr: &'tcx Expr<'_>, + op: BinOpKind, + left: &'tcx Expr<'_>, + right: &'tcx Expr<'_>, + msrv: &Msrv, +) { + if msrv.meets(msrvs::UINT_FLOAT_MIDPOINT) + && !left.span.from_expansion() + && !right.span.from_expansion() + && op == BinOpKind::Div + && (is_integer_literal(right, 2) || is_floating_point_integer_literal(right, 2)) + && let Some((ll_expr, lr_expr)) = add_operands(left) + && add_operands(ll_expr).is_none() && add_operands(lr_expr).is_none() + && let left_ty = cx.typeck_results().expr_ty_adjusted(ll_expr) + && let right_ty = cx.typeck_results().expr_ty_adjusted(lr_expr) + && left_ty == right_ty + // FIXME: Also lint on signed integers when rust-lang/rust#134340 is merged + && matches!(left_ty.kind(), ty::Uint(_) | ty::Float(_)) + { + let mut app = Applicability::MachineApplicable; + let left_sugg = Sugg::hir_with_context(cx, ll_expr, expr.span.ctxt(), "..", &mut app); + let right_sugg = Sugg::hir_with_context(cx, lr_expr, expr.span.ctxt(), "..", &mut app); + let sugg = format!("{left_ty}::midpoint({left_sugg}, {right_sugg})"); + span_lint_and_sugg( + cx, + MANUAL_MIDPOINT, + expr.span, + "manual implementation of `midpoint` which can overflow", + format!("use `{left_ty}::midpoint` instead"), + sugg, + app, + ); + } +} + +/// Return the left and right operands if `expr` represents an addition +fn add_operands<'e, 'tcx>(expr: &'e Expr<'tcx>) -> Option<(&'e Expr<'tcx>, &'e Expr<'tcx>)> { + match expr.kind { + ExprKind::Binary(op, left, right) if op.node == BinOpKind::Add => Some((left, right)), + _ => None, + } +} diff --git a/clippy_lints/src/operators/mod.rs b/clippy_lints/src/operators/mod.rs index 9e8a821c3f4e..a38383a29967 100644 --- a/clippy_lints/src/operators/mod.rs +++ b/clippy_lints/src/operators/mod.rs @@ -11,6 +11,7 @@ mod float_cmp; mod float_equality_without_abs; mod identity_op; mod integer_division; +mod manual_midpoint; mod misrefactored_assign_op; mod modulo_arithmetic; mod modulo_one; @@ -24,6 +25,7 @@ mod verbose_bit_mask; pub(crate) mod arithmetic_side_effects; use clippy_config::Conf; +use clippy_utils::msrvs::Msrv; use rustc_hir::{Body, Expr, ExprKind, UnOp}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::impl_lint_pass; @@ -837,10 +839,35 @@ declare_clippy_lint! { "explicit self-assignment" } +declare_clippy_lint! { + /// ### What it does + /// Checks for manual implementation of `midpoint`. + /// + /// ### Why is this bad? + /// Using `(x + y) / 2` might cause an overflow on the intermediate + /// addition result. + /// + /// ### Example + /// ```no_run + /// # let a: u32 = 0; + /// let c = (a + 10) / 2; + /// ``` + /// Use instead: + /// ```no_run + /// # let a: u32 = 0; + /// let c = u32::midpoint(a, 10); + /// ``` + #[clippy::version = "1.85.0"] + pub MANUAL_MIDPOINT, + correctness, + "manual implementation of `midpoint` which can overflow" +} + pub struct Operators { arithmetic_context: numeric_arithmetic::Context, verbose_bit_mask_threshold: u64, modulo_arithmetic_allow_comparison_to_zero: bool, + msrv: Msrv, } impl Operators { pub fn new(conf: &'static Conf) -> Self { @@ -848,6 +875,7 @@ impl Operators { arithmetic_context: numeric_arithmetic::Context::default(), verbose_bit_mask_threshold: conf.verbose_bit_mask_threshold, modulo_arithmetic_allow_comparison_to_zero: conf.allow_comparison_to_zero, + msrv: conf.msrv.clone(), } } } @@ -879,6 +907,7 @@ impl_lint_pass!(Operators => [ NEEDLESS_BITWISE_BOOL, PTR_EQ, SELF_ASSIGNMENT, + MANUAL_MIDPOINT, ]); impl<'tcx> LateLintPass<'tcx> for Operators { @@ -896,6 +925,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators { identity_op::check(cx, e, op.node, lhs, rhs); needless_bitwise_bool::check(cx, e, op.node, lhs, rhs); ptr_eq::check(cx, e, op.node, lhs, rhs); + manual_midpoint::check(cx, e, op.node, lhs, rhs, &self.msrv); } self.arithmetic_context.check_binary(cx, e, op.node, lhs, rhs); bit_mask::check(cx, e, op.node, lhs, rhs); @@ -946,6 +976,8 @@ impl<'tcx> LateLintPass<'tcx> for Operators { fn check_body_post(&mut self, cx: &LateContext<'tcx>, b: &Body<'_>) { self.arithmetic_context.body_post(cx, b); } + + extract_msrv_attr!(LateContext); } fn macro_with_not_op(e: &Expr<'_>) -> bool { diff --git a/clippy_utils/src/msrvs.rs b/clippy_utils/src/msrvs.rs index 43ea8d249505..7f962761c551 100644 --- a/clippy_utils/src/msrvs.rs +++ b/clippy_utils/src/msrvs.rs @@ -18,6 +18,7 @@ macro_rules! msrv_aliases { // names may refer to stabilized feature flags or library items msrv_aliases! { + 1,85,0 { UINT_FLOAT_MIDPOINT } 1,83,0 { CONST_EXTERN_FN, CONST_FLOAT_BITS_CONV, CONST_FLOAT_CLASSIFY } 1,82,0 { IS_NONE_OR, REPEAT_N, RAW_REF_OP } 1,81,0 { LINT_REASONS_STABILIZATION, ERROR_IN_CORE } diff --git a/tests/ui/manual_midpoint.fixed b/tests/ui/manual_midpoint.fixed new file mode 100644 index 000000000000..6818a0738ab1 --- /dev/null +++ b/tests/ui/manual_midpoint.fixed @@ -0,0 +1,48 @@ +#![warn(clippy::manual_midpoint)] + +macro_rules! mac { + ($a: expr, $b: expr) => { + ($a + $b) / 2 + }; +} + +macro_rules! add { + ($a: expr, $b: expr) => { + ($a + $b) + }; +} + +macro_rules! two { + () => { + 2 + }; +} + +fn main() { + let a: u32 = 10; + let _ = u32::midpoint(a, 5); //~ ERROR: manual implementation of `midpoint` + + let f: f32 = 10.0; + let _ = f32::midpoint(f, 5.0); //~ ERROR: manual implementation of `midpoint` + + let _: u32 = 5 + u32::midpoint(8, 8) + 2; //~ ERROR: manual implementation of `midpoint` + let _: u32 = const { u32::midpoint(8, 8) }; //~ ERROR: manual implementation of `midpoint` + let _: f64 = const { f64::midpoint(8.0f64, 8.) }; //~ ERROR: manual implementation of `midpoint` + let _: u32 = u32::midpoint(u32::default(), u32::default()); //~ ERROR: manual implementation of `midpoint` + let _: u32 = u32::midpoint(two!(), two!()); //~ ERROR: manual implementation of `midpoint` + + // Do not lint in presence of an addition with more than 2 operands + let _: u32 = (10 + 20 + 30) / 2; + + // Do not lint if whole or part is coming from a macro + let _ = mac!(10, 20); + let _: u32 = add!(10u32, 20u32) / 2; + let _: u32 = (10 + 20) / two!(); + + // Do not lint if a literal is not present + let _ = (f + 5.0) / (1.0 + 1.0); + + // Do not lint on signed integer types + let i: i32 = 10; + let _ = (i + 5) / 2; +} diff --git a/tests/ui/manual_midpoint.rs b/tests/ui/manual_midpoint.rs new file mode 100644 index 000000000000..4189f16f46cb --- /dev/null +++ b/tests/ui/manual_midpoint.rs @@ -0,0 +1,48 @@ +#![warn(clippy::manual_midpoint)] + +macro_rules! mac { + ($a: expr, $b: expr) => { + ($a + $b) / 2 + }; +} + +macro_rules! add { + ($a: expr, $b: expr) => { + ($a + $b) + }; +} + +macro_rules! two { + () => { + 2 + }; +} + +fn main() { + let a: u32 = 10; + let _ = (a + 5) / 2; //~ ERROR: manual implementation of `midpoint` + + let f: f32 = 10.0; + let _ = (f + 5.0) / 2.0; //~ ERROR: manual implementation of `midpoint` + + let _: u32 = 5 + (8 + 8) / 2 + 2; //~ ERROR: manual implementation of `midpoint` + let _: u32 = const { (8 + 8) / 2 }; //~ ERROR: manual implementation of `midpoint` + let _: f64 = const { (8.0f64 + 8.) / 2. }; //~ ERROR: manual implementation of `midpoint` + let _: u32 = (u32::default() + u32::default()) / 2; //~ ERROR: manual implementation of `midpoint` + let _: u32 = (two!() + two!()) / 2; //~ ERROR: manual implementation of `midpoint` + + // Do not lint in presence of an addition with more than 2 operands + let _: u32 = (10 + 20 + 30) / 2; + + // Do not lint if whole or part is coming from a macro + let _ = mac!(10, 20); + let _: u32 = add!(10u32, 20u32) / 2; + let _: u32 = (10 + 20) / two!(); + + // Do not lint if a literal is not present + let _ = (f + 5.0) / (1.0 + 1.0); + + // Do not lint on signed integer types + let i: i32 = 10; + let _ = (i + 5) / 2; +} diff --git a/tests/ui/manual_midpoint.stderr b/tests/ui/manual_midpoint.stderr new file mode 100644 index 000000000000..833338febe38 --- /dev/null +++ b/tests/ui/manual_midpoint.stderr @@ -0,0 +1,47 @@ +error: manual implementation of `midpoint` which can overflow + --> tests/ui/manual_midpoint.rs:23:13 + | +LL | let _ = (a + 5) / 2; + | ^^^^^^^^^^^ help: use `u32::midpoint` instead: `u32::midpoint(a, 5)` + | + = note: `-D clippy::manual-midpoint` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::manual_midpoint)]` + +error: manual implementation of `midpoint` which can overflow + --> tests/ui/manual_midpoint.rs:26:13 + | +LL | let _ = (f + 5.0) / 2.0; + | ^^^^^^^^^^^^^^^ help: use `f32::midpoint` instead: `f32::midpoint(f, 5.0)` + +error: manual implementation of `midpoint` which can overflow + --> tests/ui/manual_midpoint.rs:28:22 + | +LL | let _: u32 = 5 + (8 + 8) / 2 + 2; + | ^^^^^^^^^^^ help: use `u32::midpoint` instead: `u32::midpoint(8, 8)` + +error: manual implementation of `midpoint` which can overflow + --> tests/ui/manual_midpoint.rs:29:26 + | +LL | let _: u32 = const { (8 + 8) / 2 }; + | ^^^^^^^^^^^ help: use `u32::midpoint` instead: `u32::midpoint(8, 8)` + +error: manual implementation of `midpoint` which can overflow + --> tests/ui/manual_midpoint.rs:30:26 + | +LL | let _: f64 = const { (8.0f64 + 8.) / 2. }; + | ^^^^^^^^^^^^^^^^^^ help: use `f64::midpoint` instead: `f64::midpoint(8.0f64, 8.)` + +error: manual implementation of `midpoint` which can overflow + --> tests/ui/manual_midpoint.rs:31:18 + | +LL | let _: u32 = (u32::default() + u32::default()) / 2; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `u32::midpoint` instead: `u32::midpoint(u32::default(), u32::default())` + +error: manual implementation of `midpoint` which can overflow + --> tests/ui/manual_midpoint.rs:32:18 + | +LL | let _: u32 = (two!() + two!()) / 2; + | ^^^^^^^^^^^^^^^^^^^^^ help: use `u32::midpoint` instead: `u32::midpoint(two!(), two!())` + +error: aborting due to 7 previous errors + diff --git a/tests/ui/option_if_let_else.fixed b/tests/ui/option_if_let_else.fixed index 0ac89bf0d8ea..bce3a2c845e1 100644 --- a/tests/ui/option_if_let_else.fixed +++ b/tests/ui/option_if_let_else.fixed @@ -4,6 +4,7 @@ clippy::equatable_if_let, clippy::let_unit_value, clippy::redundant_locals, + clippy::manual_midpoint, clippy::manual_unwrap_or_default, clippy::manual_unwrap_or )] diff --git a/tests/ui/option_if_let_else.rs b/tests/ui/option_if_let_else.rs index b4f1b2cd1f7f..156ec282f964 100644 --- a/tests/ui/option_if_let_else.rs +++ b/tests/ui/option_if_let_else.rs @@ -4,6 +4,7 @@ clippy::equatable_if_let, clippy::let_unit_value, clippy::redundant_locals, + clippy::manual_midpoint, clippy::manual_unwrap_or_default, clippy::manual_unwrap_or )] diff --git a/tests/ui/option_if_let_else.stderr b/tests/ui/option_if_let_else.stderr index 32ff22763234..633fe12faf4f 100644 --- a/tests/ui/option_if_let_else.stderr +++ b/tests/ui/option_if_let_else.stderr @@ -1,5 +1,5 @@ error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:12:5 + --> tests/ui/option_if_let_else.rs:13:5 | LL | / if let Some(x) = string { LL | | (true, x) @@ -12,19 +12,19 @@ LL | | } = help: to override `-D warnings` add `#[allow(clippy::option_if_let_else)]` error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:30:13 + --> tests/ui/option_if_let_else.rs:31:13 | LL | let _ = if let Some(s) = *string { s.len() } else { 0 }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `string.map_or(0, |s| s.len())` error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:31:13 + --> tests/ui/option_if_let_else.rs:32:13 | LL | let _ = if let Some(s) = &num { s } else { &0 }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `num.as_ref().map_or(&0, |s| s)` error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:32:13 + --> tests/ui/option_if_let_else.rs:33:13 | LL | let _ = if let Some(s) = &mut num { | _____________^ @@ -44,13 +44,13 @@ LL ~ }); | error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:38:13 + --> tests/ui/option_if_let_else.rs:39:13 | LL | let _ = if let Some(ref s) = num { s } else { &0 }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `num.as_ref().map_or(&0, |s| s)` error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:39:13 + --> tests/ui/option_if_let_else.rs:40:13 | LL | let _ = if let Some(mut s) = num { | _____________^ @@ -70,7 +70,7 @@ LL ~ }); | error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:45:13 + --> tests/ui/option_if_let_else.rs:46:13 | LL | let _ = if let Some(ref mut s) = num { | _____________^ @@ -90,7 +90,7 @@ LL ~ }); | error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:54:5 + --> tests/ui/option_if_let_else.rs:55:5 | LL | / if let Some(x) = arg { LL | | let y = x * x; @@ -109,7 +109,7 @@ LL + }) | error: use Option::map_or_else instead of an if let/else - --> tests/ui/option_if_let_else.rs:67:13 + --> tests/ui/option_if_let_else.rs:68:13 | LL | let _ = if let Some(x) = arg { | _____________^ @@ -120,7 +120,7 @@ LL | | }; | |_____^ help: try: `arg.map_or_else(side_effect, |x| x)` error: use Option::map_or_else instead of an if let/else - --> tests/ui/option_if_let_else.rs:76:13 + --> tests/ui/option_if_let_else.rs:77:13 | LL | let _ = if let Some(x) = arg { | _____________^ @@ -143,7 +143,7 @@ LL ~ }, |x| x * x * x * x); | error: use Option::map_or_else instead of an if let/else - --> tests/ui/option_if_let_else.rs:109:13 + --> tests/ui/option_if_let_else.rs:110:13 | LL | / if let Some(idx) = s.find('.') { LL | | vec![s[..idx].to_string(), s[idx..].to_string()] @@ -153,7 +153,7 @@ LL | | } | |_____________^ help: try: `s.find('.').map_or_else(|| vec![s.to_string()], |idx| vec![s[..idx].to_string(), s[idx..].to_string()])` error: use Option::map_or_else instead of an if let/else - --> tests/ui/option_if_let_else.rs:120:5 + --> tests/ui/option_if_let_else.rs:121:5 | LL | / if let Ok(binding) = variable { LL | | println!("Ok {binding}"); @@ -176,13 +176,13 @@ LL + }) | error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:144:13 + --> tests/ui/option_if_let_else.rs:145:13 | LL | let _ = if let Some(x) = optional { x + 2 } else { 5 }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `optional.map_or(5, |x| x + 2)` error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:154:13 + --> tests/ui/option_if_let_else.rs:155:13 | LL | let _ = if let Some(x) = Some(0) { | _____________^ @@ -204,13 +204,13 @@ LL ~ }); | error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:182:13 + --> tests/ui/option_if_let_else.rs:183:13 | LL | let _ = if let Some(x) = Some(0) { s.len() + x } else { s.len() }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(0).map_or(s.len(), |x| s.len() + x)` error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:186:13 + --> tests/ui/option_if_let_else.rs:187:13 | LL | let _ = if let Some(x) = Some(0) { | _____________^ @@ -230,7 +230,7 @@ LL ~ }); | error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:225:13 + --> tests/ui/option_if_let_else.rs:226:13 | LL | let _ = match s { | _____________^ @@ -240,7 +240,7 @@ LL | | }; | |_____^ help: try: `s.map_or(1, |string| string.len())` error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:229:13 + --> tests/ui/option_if_let_else.rs:230:13 | LL | let _ = match Some(10) { | _____________^ @@ -250,7 +250,7 @@ LL | | }; | |_____^ help: try: `Some(10).map_or(5, |a| a + 1)` error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:235:13 + --> tests/ui/option_if_let_else.rs:236:13 | LL | let _ = match res { | _____________^ @@ -260,7 +260,7 @@ LL | | }; | |_____^ help: try: `res.map_or(1, |a| a + 1)` error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:239:13 + --> tests/ui/option_if_let_else.rs:240:13 | LL | let _ = match res { | _____________^ @@ -270,13 +270,13 @@ LL | | }; | |_____^ help: try: `res.map_or(1, |a| a + 1)` error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:243:13 + --> tests/ui/option_if_let_else.rs:244:13 | LL | let _ = if let Ok(a) = res { a + 1 } else { 5 }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `res.map_or(5, |a| a + 1)` error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:260:17 + --> tests/ui/option_if_let_else.rs:261:17 | LL | let _ = match initial { | _________________^ @@ -286,7 +286,7 @@ LL | | }; | |_________^ help: try: `initial.as_ref().map_or(42, |value| do_something(value))` error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:267:17 + --> tests/ui/option_if_let_else.rs:268:17 | LL | let _ = match initial { | _________________^ @@ -296,7 +296,7 @@ LL | | }; | |_________^ help: try: `initial.as_mut().map_or(42, |value| do_something2(value))` error: use Option::map_or_else instead of an if let/else - --> tests/ui/option_if_let_else.rs:290:24 + --> tests/ui/option_if_let_else.rs:291:24 | LL | let mut _hashmap = if let Some(hm) = &opt { | ________________________^ @@ -307,7 +307,7 @@ LL | | }; | |_____^ help: try: `opt.as_ref().map_or_else(HashMap::new, |hm| hm.clone())` error: use Option::map_or_else instead of an if let/else - --> tests/ui/option_if_let_else.rs:296:19 + --> tests/ui/option_if_let_else.rs:297:19 | LL | let mut _hm = if let Some(hm) = &opt { hm.clone() } else { new_map!() }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.as_ref().map_or_else(|| new_map!(), |hm| hm.clone())` diff --git a/tests/ui/suspicious_operation_groupings.fixed b/tests/ui/suspicious_operation_groupings.fixed index 60fde6e22cb6..41f1bdc44291 100644 --- a/tests/ui/suspicious_operation_groupings.fixed +++ b/tests/ui/suspicious_operation_groupings.fixed @@ -1,7 +1,7 @@ //@compile-flags: -Zdeduplicate-diagnostics=yes #![warn(clippy::suspicious_operation_groupings)] -#![allow(dead_code, unused_parens, clippy::eq_op)] +#![allow(dead_code, unused_parens, clippy::eq_op, clippy::manual_midpoint)] struct Vec3 { x: f64, diff --git a/tests/ui/suspicious_operation_groupings.rs b/tests/ui/suspicious_operation_groupings.rs index ce37148a853b..49ef1a8cbe80 100644 --- a/tests/ui/suspicious_operation_groupings.rs +++ b/tests/ui/suspicious_operation_groupings.rs @@ -1,7 +1,7 @@ //@compile-flags: -Zdeduplicate-diagnostics=yes #![warn(clippy::suspicious_operation_groupings)] -#![allow(dead_code, unused_parens, clippy::eq_op)] +#![allow(dead_code, unused_parens, clippy::eq_op, clippy::manual_midpoint)] struct Vec3 { x: f64,