Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 28fd9ba

Browse files
committedFeb 15, 2025·
New lint: manual_midpoint
1 parent 00042a8 commit 28fd9ba

15 files changed

+309
-27
lines changed
 

‎CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5762,6 +5762,7 @@ Released 2018-09-13
57625762
[`manual_main_separator_str`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_main_separator_str
57635763
[`manual_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_map
57645764
[`manual_memcpy`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_memcpy
5765+
[`manual_midpoint`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_midpoint
57655766
[`manual_next_back`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_next_back
57665767
[`manual_non_exhaustive`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_non_exhaustive
57675768
[`manual_ok_err`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_ok_err

‎book/src/lint_configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ The minimum rust version that the project supports. Defaults to the `rust-versio
768768
* [`manual_hash_one`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_hash_one)
769769
* [`manual_is_ascii_check`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_is_ascii_check)
770770
* [`manual_let_else`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_let_else)
771+
* [`manual_midpoint`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_midpoint)
771772
* [`manual_non_exhaustive`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_non_exhaustive)
772773
* [`manual_option_as_slice`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_option_as_slice)
773774
* [`manual_pattern_char_comparison`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_pattern_char_comparison)

‎clippy_config/src/conf.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ define_Conf! {
623623
manual_hash_one,
624624
manual_is_ascii_check,
625625
manual_let_else,
626+
manual_midpoint,
626627
manual_non_exhaustive,
627628
manual_option_as_slice,
628629
manual_pattern_char_comparison,

‎clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
604604
crate::operators::IMPOSSIBLE_COMPARISONS_INFO,
605605
crate::operators::INEFFECTIVE_BIT_MASK_INFO,
606606
crate::operators::INTEGER_DIVISION_INFO,
607+
crate::operators::MANUAL_MIDPOINT_INFO,
607608
crate::operators::MISREFACTORED_ASSIGN_OP_INFO,
608609
crate::operators::MODULO_ARITHMETIC_INFO,
609610
crate::operators::MODULO_ONE_INFO,
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::msrvs::{self, Msrv};
3+
use clippy_utils::sugg::Sugg;
4+
use clippy_utils::{is_float_literal, is_integer_literal};
5+
use rustc_ast::BinOpKind;
6+
use rustc_errors::Applicability;
7+
use rustc_hir::{Expr, ExprKind};
8+
use rustc_lint::LateContext;
9+
use rustc_middle::ty;
10+
11+
use super::MANUAL_MIDPOINT;
12+
13+
pub(super) fn check<'tcx>(
14+
cx: &LateContext<'tcx>,
15+
expr: &'tcx Expr<'_>,
16+
op: BinOpKind,
17+
left: &'tcx Expr<'_>,
18+
right: &'tcx Expr<'_>,
19+
msrv: &Msrv,
20+
) {
21+
if msrv.meets(msrvs::UINT_FLOAT_MIDPOINT)
22+
&& !left.span.from_expansion()
23+
&& !right.span.from_expansion()
24+
&& op == BinOpKind::Div
25+
&& (is_integer_literal(right, 2) || is_float_literal(right, 2.0))
26+
&& let Some((ll_expr, lr_expr)) = add_operands(left)
27+
&& add_operands(ll_expr).is_none() && add_operands(lr_expr).is_none()
28+
&& let left_ty = cx.typeck_results().expr_ty_adjusted(ll_expr)
29+
&& let right_ty = cx.typeck_results().expr_ty_adjusted(lr_expr)
30+
&& left_ty == right_ty
31+
// Do not lint on `(_+1)/2` and `(1+_)/2`, it is likely a `div_ceil()` operation
32+
&& !is_integer_literal(ll_expr, 1) && !is_integer_literal(lr_expr, 1)
33+
// FIXME: Also lint on signed integers when rust-lang/rust#134340 is merged
34+
&& matches!(left_ty.kind(), ty::Uint(_) | ty::Float(_))
35+
{
36+
let mut app = Applicability::MachineApplicable;
37+
let left_sugg = Sugg::hir_with_context(cx, ll_expr, expr.span.ctxt(), "..", &mut app);
38+
let right_sugg = Sugg::hir_with_context(cx, lr_expr, expr.span.ctxt(), "..", &mut app);
39+
let sugg = format!("{left_ty}::midpoint({left_sugg}, {right_sugg})");
40+
span_lint_and_sugg(
41+
cx,
42+
MANUAL_MIDPOINT,
43+
expr.span,
44+
"manual implementation of `midpoint` which can overflow",
45+
format!("use `{left_ty}::midpoint` instead"),
46+
sugg,
47+
app,
48+
);
49+
}
50+
}
51+
52+
/// Return the left and right operands if `expr` represents an addition
53+
fn add_operands<'e, 'tcx>(expr: &'e Expr<'tcx>) -> Option<(&'e Expr<'tcx>, &'e Expr<'tcx>)> {
54+
match expr.kind {
55+
ExprKind::Binary(op, left, right) if op.node == BinOpKind::Add => Some((left, right)),
56+
_ => None,
57+
}
58+
}

‎clippy_lints/src/operators/mod.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod float_cmp;
1111
mod float_equality_without_abs;
1212
mod identity_op;
1313
mod integer_division;
14+
mod manual_midpoint;
1415
mod misrefactored_assign_op;
1516
mod modulo_arithmetic;
1617
mod modulo_one;
@@ -24,6 +25,7 @@ mod verbose_bit_mask;
2425
pub(crate) mod arithmetic_side_effects;
2526

2627
use clippy_config::Conf;
28+
use clippy_utils::msrvs::Msrv;
2729
use rustc_hir::{Body, Expr, ExprKind, UnOp};
2830
use rustc_lint::{LateContext, LateLintPass};
2931
use rustc_session::impl_lint_pass;
@@ -834,17 +836,43 @@ declare_clippy_lint! {
834836
"explicit self-assignment"
835837
}
836838

839+
declare_clippy_lint! {
840+
/// ### What it does
841+
/// Checks for manual implementation of `midpoint`.
842+
///
843+
/// ### Why is this bad?
844+
/// Using `(x + y) / 2` might cause an overflow on the intermediate
845+
/// addition result.
846+
///
847+
/// ### Example
848+
/// ```no_run
849+
/// # let a: u32 = 0;
850+
/// let c = (a + 10) / 2;
851+
/// ```
852+
/// Use instead:
853+
/// ```no_run
854+
/// # let a: u32 = 0;
855+
/// let c = u32::midpoint(a, 10);
856+
/// ```
857+
#[clippy::version = "1.86.0"]
858+
pub MANUAL_MIDPOINT,
859+
correctness,
860+
"manual implementation of `midpoint` which can overflow"
861+
}
862+
837863
pub struct Operators {
838864
arithmetic_context: numeric_arithmetic::Context,
839865
verbose_bit_mask_threshold: u64,
840866
modulo_arithmetic_allow_comparison_to_zero: bool,
867+
msrv: Msrv,
841868
}
842869
impl Operators {
843870
pub fn new(conf: &'static Conf) -> Self {
844871
Self {
845872
arithmetic_context: numeric_arithmetic::Context::default(),
846873
verbose_bit_mask_threshold: conf.verbose_bit_mask_threshold,
847874
modulo_arithmetic_allow_comparison_to_zero: conf.allow_comparison_to_zero,
875+
msrv: conf.msrv.clone(),
848876
}
849877
}
850878
}
@@ -876,6 +904,7 @@ impl_lint_pass!(Operators => [
876904
NEEDLESS_BITWISE_BOOL,
877905
PTR_EQ,
878906
SELF_ASSIGNMENT,
907+
MANUAL_MIDPOINT,
879908
]);
880909

881910
impl<'tcx> LateLintPass<'tcx> for Operators {
@@ -893,6 +922,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
893922
identity_op::check(cx, e, op.node, lhs, rhs);
894923
needless_bitwise_bool::check(cx, e, op.node, lhs, rhs);
895924
ptr_eq::check(cx, e, op.node, lhs, rhs);
925+
manual_midpoint::check(cx, e, op.node, lhs, rhs, &self.msrv);
896926
}
897927
self.arithmetic_context.check_binary(cx, e, op.node, lhs, rhs);
898928
bit_mask::check(cx, e, op.node, lhs, rhs);
@@ -943,6 +973,8 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
943973
fn check_body_post(&mut self, cx: &LateContext<'tcx>, b: &Body<'_>) {
944974
self.arithmetic_context.body_post(cx, b);
945975
}
976+
977+
extract_msrv_attr!(LateContext);
946978
}
947979

948980
fn macro_with_not_op(e: &Expr<'_>) -> bool {

‎clippy_utils/src/msrvs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ macro_rules! msrv_aliases {
1919

2020
// names may refer to stabilized feature flags or library items
2121
msrv_aliases! {
22+
1,85,0 { UINT_FLOAT_MIDPOINT }
2223
1,84,0 { CONST_OPTION_AS_SLICE }
2324
1,83,0 { CONST_EXTERN_FN, CONST_FLOAT_BITS_CONV, CONST_FLOAT_CLASSIFY, CONST_MUT_REFS, CONST_UNWRAP }
2425
1,82,0 { IS_NONE_OR, REPEAT_N, RAW_REF_OP }

‎tests/ui/manual_midpoint.fixed

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#![warn(clippy::manual_midpoint)]
2+
3+
macro_rules! mac {
4+
($a: expr, $b: expr) => {
5+
($a + $b) / 2
6+
};
7+
}
8+
9+
macro_rules! add {
10+
($a: expr, $b: expr) => {
11+
($a + $b)
12+
};
13+
}
14+
15+
macro_rules! two {
16+
() => {
17+
2
18+
};
19+
}
20+
21+
#[clippy::msrv = "1.84"]
22+
fn older_msrv() {
23+
let a: u32 = 10;
24+
let _ = (a + 5) / 2;
25+
}
26+
27+
#[clippy::msrv = "1.85"]
28+
fn main() {
29+
let a: u32 = 10;
30+
let _ = u32::midpoint(a, 5); //~ ERROR: manual implementation of `midpoint`
31+
32+
let f: f32 = 10.0;
33+
let _ = f32::midpoint(f, 5.0); //~ ERROR: manual implementation of `midpoint`
34+
35+
let _: u32 = 5 + u32::midpoint(8, 8) + 2; //~ ERROR: manual implementation of `midpoint`
36+
let _: u32 = const { u32::midpoint(8, 8) }; //~ ERROR: manual implementation of `midpoint`
37+
let _: f64 = const { f64::midpoint(8.0f64, 8.) }; //~ ERROR: manual implementation of `midpoint`
38+
let _: u32 = u32::midpoint(u32::default(), u32::default()); //~ ERROR: manual implementation of `midpoint`
39+
let _: u32 = u32::midpoint(two!(), two!()); //~ ERROR: manual implementation of `midpoint`
40+
41+
// Do not lint in presence of an addition with more than 2 operands
42+
let _: u32 = (10 + 20 + 30) / 2;
43+
44+
// Do not lint if whole or part is coming from a macro
45+
let _ = mac!(10, 20);
46+
let _: u32 = add!(10u32, 20u32) / 2;
47+
let _: u32 = (10 + 20) / two!();
48+
49+
// Do not lint if a literal is not present
50+
let _ = (f + 5.0) / (1.0 + 1.0);
51+
52+
// Do not lint on signed integer types
53+
let i: i32 = 10;
54+
let _ = (i + 5) / 2;
55+
56+
// Do not lint on (x+1)/2 or (1+x)/2 as this looks more like a `div_ceil()` operation
57+
let _ = (i + 1) / 2;
58+
let _ = (1 + i) / 2;
59+
60+
// But if we see (x+1.0)/2.0 or (x+1.0)/2.0, it is probably a midpoint operation
61+
let _ = f32::midpoint(f, 1.0); //~ ERROR: manual implementation of `midpoint`
62+
let _ = f32::midpoint(1.0, f); //~ ERROR: manual implementation of `midpoint`
63+
}

‎tests/ui/manual_midpoint.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#![warn(clippy::manual_midpoint)]
2+
3+
macro_rules! mac {
4+
($a: expr, $b: expr) => {
5+
($a + $b) / 2
6+
};
7+
}
8+
9+
macro_rules! add {
10+
($a: expr, $b: expr) => {
11+
($a + $b)
12+
};
13+
}
14+
15+
macro_rules! two {
16+
() => {
17+
2
18+
};
19+
}
20+
21+
#[clippy::msrv = "1.84"]
22+
fn older_msrv() {
23+
let a: u32 = 10;
24+
let _ = (a + 5) / 2;
25+
}
26+
27+
#[clippy::msrv = "1.85"]
28+
fn main() {
29+
let a: u32 = 10;
30+
let _ = (a + 5) / 2; //~ ERROR: manual implementation of `midpoint`
31+
32+
let f: f32 = 10.0;
33+
let _ = (f + 5.0) / 2.0; //~ ERROR: manual implementation of `midpoint`
34+
35+
let _: u32 = 5 + (8 + 8) / 2 + 2; //~ ERROR: manual implementation of `midpoint`
36+
let _: u32 = const { (8 + 8) / 2 }; //~ ERROR: manual implementation of `midpoint`
37+
let _: f64 = const { (8.0f64 + 8.) / 2. }; //~ ERROR: manual implementation of `midpoint`
38+
let _: u32 = (u32::default() + u32::default()) / 2; //~ ERROR: manual implementation of `midpoint`
39+
let _: u32 = (two!() + two!()) / 2; //~ ERROR: manual implementation of `midpoint`
40+
41+
// Do not lint in presence of an addition with more than 2 operands
42+
let _: u32 = (10 + 20 + 30) / 2;
43+
44+
// Do not lint if whole or part is coming from a macro
45+
let _ = mac!(10, 20);
46+
let _: u32 = add!(10u32, 20u32) / 2;
47+
let _: u32 = (10 + 20) / two!();
48+
49+
// Do not lint if a literal is not present
50+
let _ = (f + 5.0) / (1.0 + 1.0);
51+
52+
// Do not lint on signed integer types
53+
let i: i32 = 10;
54+
let _ = (i + 5) / 2;
55+
56+
// Do not lint on (x+1)/2 or (1+x)/2 as this looks more like a `div_ceil()` operation
57+
let _ = (i + 1) / 2;
58+
let _ = (1 + i) / 2;
59+
60+
// But if we see (x+1.0)/2.0 or (x+1.0)/2.0, it is probably a midpoint operation
61+
let _ = (f + 1.0) / 2.0; //~ ERROR: manual implementation of `midpoint`
62+
let _ = (1.0 + f) / 2.0; //~ ERROR: manual implementation of `midpoint`
63+
}

‎tests/ui/manual_midpoint.stderr

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
error: manual implementation of `midpoint` which can overflow
2+
--> tests/ui/manual_midpoint.rs:30:13
3+
|
4+
LL | let _ = (a + 5) / 2;
5+
| ^^^^^^^^^^^ help: use `u32::midpoint` instead: `u32::midpoint(a, 5)`
6+
|
7+
= note: `-D clippy::manual-midpoint` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::manual_midpoint)]`
9+
10+
error: manual implementation of `midpoint` which can overflow
11+
--> tests/ui/manual_midpoint.rs:33:13
12+
|
13+
LL | let _ = (f + 5.0) / 2.0;
14+
| ^^^^^^^^^^^^^^^ help: use `f32::midpoint` instead: `f32::midpoint(f, 5.0)`
15+
16+
error: manual implementation of `midpoint` which can overflow
17+
--> tests/ui/manual_midpoint.rs:35:22
18+
|
19+
LL | let _: u32 = 5 + (8 + 8) / 2 + 2;
20+
| ^^^^^^^^^^^ help: use `u32::midpoint` instead: `u32::midpoint(8, 8)`
21+
22+
error: manual implementation of `midpoint` which can overflow
23+
--> tests/ui/manual_midpoint.rs:36:26
24+
|
25+
LL | let _: u32 = const { (8 + 8) / 2 };
26+
| ^^^^^^^^^^^ help: use `u32::midpoint` instead: `u32::midpoint(8, 8)`
27+
28+
error: manual implementation of `midpoint` which can overflow
29+
--> tests/ui/manual_midpoint.rs:37:26
30+
|
31+
LL | let _: f64 = const { (8.0f64 + 8.) / 2. };
32+
| ^^^^^^^^^^^^^^^^^^ help: use `f64::midpoint` instead: `f64::midpoint(8.0f64, 8.)`
33+
34+
error: manual implementation of `midpoint` which can overflow
35+
--> tests/ui/manual_midpoint.rs:38:18
36+
|
37+
LL | let _: u32 = (u32::default() + u32::default()) / 2;
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `u32::midpoint` instead: `u32::midpoint(u32::default(), u32::default())`
39+
40+
error: manual implementation of `midpoint` which can overflow
41+
--> tests/ui/manual_midpoint.rs:39:18
42+
|
43+
LL | let _: u32 = (two!() + two!()) / 2;
44+
| ^^^^^^^^^^^^^^^^^^^^^ help: use `u32::midpoint` instead: `u32::midpoint(two!(), two!())`
45+
46+
error: manual implementation of `midpoint` which can overflow
47+
--> tests/ui/manual_midpoint.rs:61:13
48+
|
49+
LL | let _ = (f + 1.0) / 2.0;
50+
| ^^^^^^^^^^^^^^^ help: use `f32::midpoint` instead: `f32::midpoint(f, 1.0)`
51+
52+
error: manual implementation of `midpoint` which can overflow
53+
--> tests/ui/manual_midpoint.rs:62:13
54+
|
55+
LL | let _ = (1.0 + f) / 2.0;
56+
| ^^^^^^^^^^^^^^^ help: use `f32::midpoint` instead: `f32::midpoint(1.0, f)`
57+
58+
error: aborting due to 9 previous errors
59+

‎tests/ui/option_if_let_else.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
clippy::equatable_if_let,
55
clippy::let_unit_value,
66
clippy::redundant_locals,
7+
clippy::manual_midpoint,
78
clippy::manual_unwrap_or_default,
89
clippy::manual_unwrap_or
910
)]

‎tests/ui/option_if_let_else.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
clippy::equatable_if_let,
55
clippy::let_unit_value,
66
clippy::redundant_locals,
7+
clippy::manual_midpoint,
78
clippy::manual_unwrap_or_default,
89
clippy::manual_unwrap_or
910
)]

‎tests/ui/option_if_let_else.stderr

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: use Option::map_or instead of an if let/else
2-
--> tests/ui/option_if_let_else.rs:12:5
2+
--> tests/ui/option_if_let_else.rs:13:5
33
|
44
LL | / if let Some(x) = string {
55
LL | |
@@ -13,19 +13,19 @@ LL | | }
1313
= help: to override `-D warnings` add `#[allow(clippy::option_if_let_else)]`
1414

1515
error: use Option::map_or instead of an if let/else
16-
--> tests/ui/option_if_let_else.rs:31:13
16+
--> tests/ui/option_if_let_else.rs:32:13
1717
|
1818
LL | let _ = if let Some(s) = *string { s.len() } else { 0 };
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `string.map_or(0, |s| s.len())`
2020

2121
error: use Option::map_or instead of an if let/else
22-
--> tests/ui/option_if_let_else.rs:33:13
22+
--> tests/ui/option_if_let_else.rs:34:13
2323
|
2424
LL | let _ = if let Some(s) = &num { s } else { &0 };
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `num.as_ref().map_or(&0, |s| s)`
2626

2727
error: use Option::map_or instead of an if let/else
28-
--> tests/ui/option_if_let_else.rs:35:13
28+
--> tests/ui/option_if_let_else.rs:36:13
2929
|
3030
LL | let _ = if let Some(s) = &mut num {
3131
| _____________^
@@ -47,13 +47,13 @@ LL ~ });
4747
|
4848

4949
error: use Option::map_or instead of an if let/else
50-
--> tests/ui/option_if_let_else.rs:42:13
50+
--> tests/ui/option_if_let_else.rs:43:13
5151
|
5252
LL | let _ = if let Some(ref s) = num { s } else { &0 };
5353
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `num.as_ref().map_or(&0, |s| s)`
5454

5555
error: use Option::map_or instead of an if let/else
56-
--> tests/ui/option_if_let_else.rs:44:13
56+
--> tests/ui/option_if_let_else.rs:45:13
5757
|
5858
LL | let _ = if let Some(mut s) = num {
5959
| _____________^
@@ -75,7 +75,7 @@ LL ~ });
7575
|
7676

7777
error: use Option::map_or instead of an if let/else
78-
--> tests/ui/option_if_let_else.rs:51:13
78+
--> tests/ui/option_if_let_else.rs:52:13
7979
|
8080
LL | let _ = if let Some(ref mut s) = num {
8181
| _____________^
@@ -97,7 +97,7 @@ LL ~ });
9797
|
9898

9999
error: use Option::map_or instead of an if let/else
100-
--> tests/ui/option_if_let_else.rs:61:5
100+
--> tests/ui/option_if_let_else.rs:62:5
101101
|
102102
LL | / if let Some(x) = arg {
103103
LL | |
@@ -118,7 +118,7 @@ LL + })
118118
|
119119

120120
error: use Option::map_or_else instead of an if let/else
121-
--> tests/ui/option_if_let_else.rs:75:13
121+
--> tests/ui/option_if_let_else.rs:76:13
122122
|
123123
LL | let _ = if let Some(x) = arg {
124124
| _____________^
@@ -131,7 +131,7 @@ LL | | };
131131
| |_____^ help: try: `arg.map_or_else(side_effect, |x| x)`
132132

133133
error: use Option::map_or_else instead of an if let/else
134-
--> tests/ui/option_if_let_else.rs:85:13
134+
--> tests/ui/option_if_let_else.rs:86:13
135135
|
136136
LL | let _ = if let Some(x) = arg {
137137
| _____________^
@@ -154,7 +154,7 @@ LL ~ }, |x| x * x * x * x);
154154
|
155155

156156
error: use Option::map_or_else instead of an if let/else
157-
--> tests/ui/option_if_let_else.rs:119:13
157+
--> tests/ui/option_if_let_else.rs:120:13
158158
|
159159
LL | / if let Some(idx) = s.find('.') {
160160
LL | |
@@ -165,7 +165,7 @@ LL | | }
165165
| |_____________^ help: try: `s.find('.').map_or_else(|| vec![s.to_string()], |idx| vec![s[..idx].to_string(), s[idx..].to_string()])`
166166

167167
error: use Option::map_or_else instead of an if let/else
168-
--> tests/ui/option_if_let_else.rs:131:5
168+
--> tests/ui/option_if_let_else.rs:132:5
169169
|
170170
LL | / if let Ok(binding) = variable {
171171
LL | |
@@ -189,13 +189,13 @@ LL + })
189189
|
190190

191191
error: use Option::map_or instead of an if let/else
192-
--> tests/ui/option_if_let_else.rs:156:13
192+
--> tests/ui/option_if_let_else.rs:157:13
193193
|
194194
LL | let _ = if let Some(x) = optional { x + 2 } else { 5 };
195195
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `optional.map_or(5, |x| x + 2)`
196196

197197
error: use Option::map_or instead of an if let/else
198-
--> tests/ui/option_if_let_else.rs:167:13
198+
--> tests/ui/option_if_let_else.rs:168:13
199199
|
200200
LL | let _ = if let Some(x) = Some(0) {
201201
| _____________^
@@ -217,13 +217,13 @@ LL ~ });
217217
|
218218

219219
error: use Option::map_or instead of an if let/else
220-
--> tests/ui/option_if_let_else.rs:196:13
220+
--> tests/ui/option_if_let_else.rs:197:13
221221
|
222222
LL | let _ = if let Some(x) = Some(0) { s.len() + x } else { s.len() };
223223
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(0).map_or(s.len(), |x| s.len() + x)`
224224

225225
error: use Option::map_or instead of an if let/else
226-
--> tests/ui/option_if_let_else.rs:201:13
226+
--> tests/ui/option_if_let_else.rs:202:13
227227
|
228228
LL | let _ = if let Some(x) = Some(0) {
229229
| _____________^
@@ -245,7 +245,7 @@ LL ~ });
245245
|
246246

247247
error: use Option::map_or instead of an if let/else
248-
--> tests/ui/option_if_let_else.rs:241:13
248+
--> tests/ui/option_if_let_else.rs:242:13
249249
|
250250
LL | let _ = match s {
251251
| _____________^
@@ -256,7 +256,7 @@ LL | | };
256256
| |_____^ help: try: `s.map_or(1, |string| string.len())`
257257

258258
error: use Option::map_or instead of an if let/else
259-
--> tests/ui/option_if_let_else.rs:246:13
259+
--> tests/ui/option_if_let_else.rs:247:13
260260
|
261261
LL | let _ = match Some(10) {
262262
| _____________^
@@ -267,7 +267,7 @@ LL | | };
267267
| |_____^ help: try: `Some(10).map_or(5, |a| a + 1)`
268268

269269
error: use Option::map_or instead of an if let/else
270-
--> tests/ui/option_if_let_else.rs:253:13
270+
--> tests/ui/option_if_let_else.rs:254:13
271271
|
272272
LL | let _ = match res {
273273
| _____________^
@@ -278,7 +278,7 @@ LL | | };
278278
| |_____^ help: try: `res.map_or(1, |a| a + 1)`
279279

280280
error: use Option::map_or instead of an if let/else
281-
--> tests/ui/option_if_let_else.rs:258:13
281+
--> tests/ui/option_if_let_else.rs:259:13
282282
|
283283
LL | let _ = match res {
284284
| _____________^
@@ -289,13 +289,13 @@ LL | | };
289289
| |_____^ help: try: `res.map_or(1, |a| a + 1)`
290290

291291
error: use Option::map_or instead of an if let/else
292-
--> tests/ui/option_if_let_else.rs:263:13
292+
--> tests/ui/option_if_let_else.rs:264:13
293293
|
294294
LL | let _ = if let Ok(a) = res { a + 1 } else { 5 };
295295
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `res.map_or(5, |a| a + 1)`
296296

297297
error: use Option::map_or instead of an if let/else
298-
--> tests/ui/option_if_let_else.rs:281:17
298+
--> tests/ui/option_if_let_else.rs:282:17
299299
|
300300
LL | let _ = match initial {
301301
| _________________^
@@ -306,7 +306,7 @@ LL | | };
306306
| |_________^ help: try: `initial.as_ref().map_or(42, |value| do_something(value))`
307307

308308
error: use Option::map_or instead of an if let/else
309-
--> tests/ui/option_if_let_else.rs:289:17
309+
--> tests/ui/option_if_let_else.rs:290:17
310310
|
311311
LL | let _ = match initial {
312312
| _________________^
@@ -317,7 +317,7 @@ LL | | };
317317
| |_________^ help: try: `initial.as_mut().map_or(42, |value| do_something2(value))`
318318

319319
error: use Option::map_or_else instead of an if let/else
320-
--> tests/ui/option_if_let_else.rs:313:24
320+
--> tests/ui/option_if_let_else.rs:314:24
321321
|
322322
LL | let mut _hashmap = if let Some(hm) = &opt {
323323
| ________________________^
@@ -329,7 +329,7 @@ LL | | };
329329
| |_____^ help: try: `opt.as_ref().map_or_else(HashMap::new, |hm| hm.clone())`
330330

331331
error: use Option::map_or_else instead of an if let/else
332-
--> tests/ui/option_if_let_else.rs:320:19
332+
--> tests/ui/option_if_let_else.rs:321:19
333333
|
334334
LL | let mut _hm = if let Some(hm) = &opt { hm.clone() } else { new_map!() };
335335
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.as_ref().map_or_else(|| new_map!(), |hm| hm.clone())`

‎tests/ui/suspicious_operation_groupings.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@compile-flags: -Zdeduplicate-diagnostics=yes
22

33
#![warn(clippy::suspicious_operation_groupings)]
4-
#![allow(dead_code, unused_parens, clippy::eq_op)]
4+
#![allow(dead_code, unused_parens, clippy::eq_op, clippy::manual_midpoint)]
55

66
struct Vec3 {
77
x: f64,

‎tests/ui/suspicious_operation_groupings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@compile-flags: -Zdeduplicate-diagnostics=yes
22

33
#![warn(clippy::suspicious_operation_groupings)]
4-
#![allow(dead_code, unused_parens, clippy::eq_op)]
4+
#![allow(dead_code, unused_parens, clippy::eq_op, clippy::manual_midpoint)]
55

66
struct Vec3 {
77
x: f64,

0 commit comments

Comments
 (0)
Please sign in to comment.