2024-07-25 11:29:17 -05:00
|
|
|
use clippy_config::Conf;
|
2024-09-22 13:42:10 -05:00
|
|
|
use clippy_config::msrvs::{self, Msrv};
|
2024-08-08 12:13:50 -05:00
|
|
|
use clippy_utils::consts::{ConstEvalCtxt, FullInt};
|
2022-06-30 03:50:09 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2023-03-10 03:53:50 -06:00
|
|
|
use clippy_utils::source::snippet_with_context;
|
2024-08-08 12:13:50 -05:00
|
|
|
use clippy_utils::{is_in_const_context, path_to_local};
|
2022-06-30 03:50:09 -05:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::{BinOpKind, Expr, ExprKind, Node, TyKind};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
|
|
|
use rustc_middle::lint::in_external_macro;
|
2023-12-01 11:21:58 -06:00
|
|
|
use rustc_session::impl_lint_pass;
|
2022-06-30 03:50:09 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
|
|
|
/// Checks for an expression like `((x % 4) + 4) % 4` which is a common manual reimplementation
|
|
|
|
/// of `x.rem_euclid(4)`.
|
|
|
|
///
|
|
|
|
/// ### Why is this bad?
|
|
|
|
/// It's simpler and more readable.
|
|
|
|
///
|
|
|
|
/// ### Example
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2022-06-30 03:50:09 -05:00
|
|
|
/// let x: i32 = 24;
|
|
|
|
/// let rem = ((x % 4) + 4) % 4;
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2022-06-30 03:50:09 -05:00
|
|
|
/// let x: i32 = 24;
|
|
|
|
/// let rem = x.rem_euclid(4);
|
|
|
|
/// ```
|
2022-10-06 02:44:38 -05:00
|
|
|
#[clippy::version = "1.64.0"]
|
2022-06-30 03:50:09 -05:00
|
|
|
pub MANUAL_REM_EUCLID,
|
|
|
|
complexity,
|
|
|
|
"manually reimplementing `rem_euclid`"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ManualRemEuclid {
|
2022-12-01 11:29:38 -06:00
|
|
|
msrv: Msrv,
|
2022-06-30 03:50:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ManualRemEuclid {
|
2024-07-25 11:29:17 -05:00
|
|
|
pub fn new(conf: &'static Conf) -> Self {
|
|
|
|
Self {
|
|
|
|
msrv: conf.msrv.clone(),
|
|
|
|
}
|
2022-06-30 03:50:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(ManualRemEuclid => [MANUAL_REM_EUCLID]);
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2023-03-10 03:53:50 -06:00
|
|
|
// (x % c + c) % c
|
2024-07-11 08:44:03 -05:00
|
|
|
if let ExprKind::Binary(rem_op, rem_lhs, rem_rhs) = expr.kind
|
|
|
|
&& rem_op.node == BinOpKind::Rem
|
|
|
|
&& let ExprKind::Binary(add_op, add_lhs, add_rhs) = rem_lhs.kind
|
|
|
|
&& add_op.node == BinOpKind::Add
|
2023-03-10 03:53:50 -06:00
|
|
|
&& let ctxt = expr.span.ctxt()
|
2024-07-11 08:44:03 -05:00
|
|
|
&& rem_lhs.span.ctxt() == ctxt
|
|
|
|
&& rem_rhs.span.ctxt() == ctxt
|
|
|
|
&& add_lhs.span.ctxt() == ctxt
|
|
|
|
&& add_rhs.span.ctxt() == ctxt
|
|
|
|
&& !in_external_macro(cx.sess(), expr.span)
|
|
|
|
&& self.msrv.meets(msrvs::REM_EUCLID)
|
2024-08-08 12:13:50 -05:00
|
|
|
&& (self.msrv.meets(msrvs::REM_EUCLID_CONST) || !is_in_const_context(cx))
|
2024-07-11 08:44:03 -05:00
|
|
|
&& let Some(const1) = check_for_unsigned_int_constant(cx, rem_rhs)
|
|
|
|
&& let Some((const2, add_other)) = check_for_either_unsigned_int_constant(cx, add_lhs, add_rhs)
|
|
|
|
&& let ExprKind::Binary(rem2_op, rem2_lhs, rem2_rhs) = add_other.kind
|
|
|
|
&& rem2_op.node == BinOpKind::Rem
|
|
|
|
&& const1 == const2
|
|
|
|
&& let Some(hir_id) = path_to_local(rem2_lhs)
|
|
|
|
&& let Some(const3) = check_for_unsigned_int_constant(cx, rem2_rhs)
|
2022-06-30 03:50:09 -05:00
|
|
|
// Also ensures the const is nonzero since zero can't be a divisor
|
2024-07-11 08:44:03 -05:00
|
|
|
&& const2 == const3
|
|
|
|
&& rem2_lhs.span.ctxt() == ctxt
|
|
|
|
&& rem2_rhs.span.ctxt() == ctxt
|
2023-11-02 11:35:56 -05:00
|
|
|
{
|
|
|
|
// Apply only to params or locals with annotated types
|
2024-02-09 14:58:36 -06:00
|
|
|
match cx.tcx.parent_hir_node(hir_id) {
|
|
|
|
Node::Param(..) => (),
|
2024-03-22 12:06:20 -05:00
|
|
|
Node::LetStmt(local) => {
|
2023-11-02 11:35:56 -05:00
|
|
|
let Some(ty) = local.ty else { return };
|
|
|
|
if matches!(ty.kind, TyKind::Infer) {
|
|
|
|
return;
|
2022-06-30 03:50:09 -05:00
|
|
|
}
|
2023-11-02 11:35:56 -05:00
|
|
|
},
|
|
|
|
_ => return,
|
|
|
|
};
|
2022-06-30 03:50:09 -05:00
|
|
|
|
2023-11-02 11:35:56 -05:00
|
|
|
let mut app = Applicability::MachineApplicable;
|
2024-07-11 08:44:03 -05:00
|
|
|
let rem_of = snippet_with_context(cx, rem2_lhs.span, ctxt, "_", &mut app).0;
|
2023-11-02 11:35:56 -05:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
MANUAL_REM_EUCLID,
|
|
|
|
expr.span,
|
|
|
|
"manual `rem_euclid` implementation",
|
|
|
|
"consider using",
|
|
|
|
format!("{rem_of}.rem_euclid({const1})"),
|
|
|
|
app,
|
|
|
|
);
|
2022-06-30 03:50:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extract_msrv_attr!(LateContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if either the left or right expressions can be an unsigned int constant and returns that
|
|
|
|
// constant along with the other expression unchanged if so
|
|
|
|
fn check_for_either_unsigned_int_constant<'a>(
|
|
|
|
cx: &'a LateContext<'_>,
|
|
|
|
left: &'a Expr<'_>,
|
|
|
|
right: &'a Expr<'_>,
|
|
|
|
) -> Option<(u128, &'a Expr<'a>)> {
|
|
|
|
check_for_unsigned_int_constant(cx, left)
|
|
|
|
.map(|int_const| (int_const, right))
|
|
|
|
.or_else(|| check_for_unsigned_int_constant(cx, right).map(|int_const| (int_const, left)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_for_unsigned_int_constant<'a>(cx: &'a LateContext<'_>, expr: &'a Expr<'_>) -> Option<u128> {
|
2024-08-08 12:13:50 -05:00
|
|
|
let int_const = ConstEvalCtxt::new(cx).eval_full_int(expr)?;
|
2022-06-30 03:50:09 -05:00
|
|
|
match int_const {
|
|
|
|
FullInt::S(s) => s.try_into().ok(),
|
|
|
|
FullInt::U(u) => Some(u),
|
|
|
|
}
|
|
|
|
}
|