Skip to content

Commit 4eb44d1

Browse files
committed
manual_midpoint: support signed integer types as well
`midpoint()` for integer types has been stabilized in Rust 1.87.
1 parent 9846ab5 commit 4eb44d1

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

clippy_lints/src/operators/manual_midpoint.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_ast::BinOpKind;
66
use rustc_errors::Applicability;
77
use rustc_hir::{Expr, ExprKind};
88
use rustc_lint::LateContext;
9-
use rustc_middle::ty;
9+
use rustc_middle::ty::{self, Ty};
1010

1111
use super::MANUAL_MIDPOINT;
1212

@@ -18,8 +18,7 @@ pub(super) fn check<'tcx>(
1818
right: &'tcx Expr<'_>,
1919
msrv: &Msrv,
2020
) {
21-
if msrv.meets(msrvs::UINT_FLOAT_MIDPOINT)
22-
&& !left.span.from_expansion()
21+
if !left.span.from_expansion()
2322
&& !right.span.from_expansion()
2423
&& op == BinOpKind::Div
2524
&& (is_integer_literal(right, 2) || is_float_literal(right, 2.0))
@@ -30,8 +29,7 @@ pub(super) fn check<'tcx>(
3029
&& left_ty == right_ty
3130
// Do not lint on `(_+1)/2` and `(1+_)/2`, it is likely a `div_ceil()` operation
3231
&& !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(_))
32+
&& is_midpoint_implemented(left_ty, msrv)
3533
{
3634
let mut app = Applicability::MachineApplicable;
3735
let left_sugg = Sugg::hir_with_context(cx, ll_expr, expr.span.ctxt(), "..", &mut app);
@@ -56,3 +54,11 @@ fn add_operands<'e, 'tcx>(expr: &'e Expr<'tcx>) -> Option<(&'e Expr<'tcx>, &'e E
5654
_ => None,
5755
}
5856
}
57+
58+
fn is_midpoint_implemented(ty: Ty<'_>, msrv: &Msrv) -> bool {
59+
match ty.kind() {
60+
ty::Uint(_) | ty::Float(_) => msrv.meets(msrvs::UINT_FLOAT_MIDPOINT),
61+
ty::Int(_) => msrv.meets(msrvs::INT_MIDPOINT),
62+
_ => false,
63+
}
64+
}

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,87,0 { INT_MIDPOINT }
2223
1,85,0 { UINT_FLOAT_MIDPOINT }
2324
1,84,0 { CONST_OPTION_AS_SLICE }
2425
1,83,0 { CONST_EXTERN_FN, CONST_FLOAT_BITS_CONV, CONST_FLOAT_CLASSIFY, CONST_MUT_REFS, CONST_UNWRAP }

tests/ui/manual_midpoint.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,14 @@ fn main() {
6161
let _ = (f + 1.0) / 2.0; //~ ERROR: manual implementation of `midpoint`
6262
let _ = (1.0 + f) / 2.0; //~ ERROR: manual implementation of `midpoint`
6363
}
64+
65+
#[clippy::msrv = "1.86"]
66+
fn older_signed_midpoint(i: i32) {
67+
// Do not lint
68+
let _ = (i + 10) / 2;
69+
}
70+
71+
#[clippy::msrv = "1.87"]
72+
fn signed_midpoint(i: i32) {
73+
let _ = (i + 10) / 2; //~ ERROR: manual implementation of `midpoint`
74+
}

0 commit comments

Comments
 (0)