Suggest manual_div_ceil even when right operand is a constant (#13951)
changelog: [`manual_div_ceil`]: lint constants as well Fix #13950
This commit is contained in:
commit
7e83ec57c6
7 changed files with 124 additions and 2 deletions
|
|
@ -102,12 +102,48 @@ impl<'tcx> LateLintPass<'tcx> for ManualDivCeil {
|
|||
{
|
||||
build_suggestion(cx, expr, add_lhs, div_rhs, &mut applicability);
|
||||
}
|
||||
|
||||
// (x + (Y - 1)) / Y
|
||||
if inner_op.node == BinOpKind::Add && differ_by_one(inner_rhs, div_rhs) {
|
||||
build_suggestion(cx, expr, inner_lhs, div_rhs, &mut applicability);
|
||||
}
|
||||
|
||||
// ((Y - 1) + x) / Y
|
||||
if inner_op.node == BinOpKind::Add && differ_by_one(inner_lhs, div_rhs) {
|
||||
build_suggestion(cx, expr, inner_rhs, div_rhs, &mut applicability);
|
||||
}
|
||||
|
||||
// (x - (-Y - 1)) / Y
|
||||
if inner_op.node == BinOpKind::Sub
|
||||
&& let ExprKind::Unary(UnOp::Neg, abs_div_rhs) = div_rhs.kind
|
||||
&& differ_by_one(abs_div_rhs, inner_rhs)
|
||||
{
|
||||
build_suggestion(cx, expr, inner_lhs, div_rhs, &mut applicability);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extract_msrv_attr!(LateContext);
|
||||
}
|
||||
|
||||
/// Checks if two expressions represent non-zero integer literals such that `small_expr + 1 ==
|
||||
/// large_expr`.
|
||||
fn differ_by_one(small_expr: &Expr<'_>, large_expr: &Expr<'_>) -> bool {
|
||||
if let ExprKind::Lit(small) = small_expr.kind
|
||||
&& let ExprKind::Lit(large) = large_expr.kind
|
||||
&& let LitKind::Int(s, _) = small.node
|
||||
&& let LitKind::Int(l, _) = large.node
|
||||
{
|
||||
Some(l.get()) == s.get().checked_add(1)
|
||||
} else if let ExprKind::Unary(UnOp::Neg, small_inner_expr) = small_expr.kind
|
||||
&& let ExprKind::Unary(UnOp::Neg, large_inner_expr) = large_expr.kind
|
||||
{
|
||||
differ_by_one(large_inner_expr, small_inner_expr)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn check_int_ty_and_feature(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
||||
let expr_ty = cx.typeck_results().expr_ty(expr);
|
||||
match expr_ty.peel_refs().kind() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue