Skip to content

Commit

Permalink
manual_midpoint: support signed integer types as well
Browse files Browse the repository at this point in the history
`midpoint()` for integer types has been stabilized in Rust 1.87.
  • Loading branch information
samueltardieu committed Feb 21, 2025
1 parent 9846ab5 commit 4eb44d1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
16 changes: 11 additions & 5 deletions clippy_lints/src/operators/manual_midpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_ast::BinOpKind;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::LateContext;
use rustc_middle::ty;
use rustc_middle::ty::{self, Ty};

use super::MANUAL_MIDPOINT;

Expand All @@ -18,8 +18,7 @@ pub(super) fn check<'tcx>(
right: &'tcx Expr<'_>,
msrv: &Msrv,
) {
if msrv.meets(msrvs::UINT_FLOAT_MIDPOINT)
&& !left.span.from_expansion()
if !left.span.from_expansion()
&& !right.span.from_expansion()
&& op == BinOpKind::Div
&& (is_integer_literal(right, 2) || is_float_literal(right, 2.0))
Expand All @@ -30,8 +29,7 @@ pub(super) fn check<'tcx>(
&& left_ty == right_ty
// Do not lint on `(_+1)/2` and `(1+_)/2`, it is likely a `div_ceil()` operation
&& !is_integer_literal(ll_expr, 1) && !is_integer_literal(lr_expr, 1)
// FIXME: Also lint on signed integers when rust-lang/rust#134340 is merged
&& matches!(left_ty.kind(), ty::Uint(_) | ty::Float(_))
&& is_midpoint_implemented(left_ty, msrv)
{
let mut app = Applicability::MachineApplicable;
let left_sugg = Sugg::hir_with_context(cx, ll_expr, expr.span.ctxt(), "..", &mut app);
Expand All @@ -56,3 +54,11 @@ fn add_operands<'e, 'tcx>(expr: &'e Expr<'tcx>) -> Option<(&'e Expr<'tcx>, &'e E
_ => None,
}
}

fn is_midpoint_implemented(ty: Ty<'_>, msrv: &Msrv) -> bool {
match ty.kind() {
ty::Uint(_) | ty::Float(_) => msrv.meets(msrvs::UINT_FLOAT_MIDPOINT),
ty::Int(_) => msrv.meets(msrvs::INT_MIDPOINT),
_ => false,
}
}
1 change: 1 addition & 0 deletions clippy_utils/src/msrvs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ macro_rules! msrv_aliases {

// names may refer to stabilized feature flags or library items
msrv_aliases! {
1,87,0 { INT_MIDPOINT }
1,85,0 { UINT_FLOAT_MIDPOINT }
1,84,0 { CONST_OPTION_AS_SLICE }
1,83,0 { CONST_EXTERN_FN, CONST_FLOAT_BITS_CONV, CONST_FLOAT_CLASSIFY, CONST_MUT_REFS, CONST_UNWRAP }
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/manual_midpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,14 @@ fn main() {
let _ = (f + 1.0) / 2.0; //~ ERROR: manual implementation of `midpoint`
let _ = (1.0 + f) / 2.0; //~ ERROR: manual implementation of `midpoint`
}

#[clippy::msrv = "1.86"]
fn older_signed_midpoint(i: i32) {
// Do not lint
let _ = (i + 10) / 2;
}

#[clippy::msrv = "1.87"]
fn signed_midpoint(i: i32) {
let _ = (i + 10) / 2; //~ ERROR: manual implementation of `midpoint`
}

0 comments on commit 4eb44d1

Please sign in to comment.