Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions clippy_lints/src/manual_div_ceil.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::snippet_with_context;
use clippy_utils::sugg::{Sugg, has_enclosing_paren};
use clippy_utils::{SpanlessEq, sym};
use rustc_ast::{BinOpKind, LitIntType, LitKind, UnOp};
Expand Down Expand Up @@ -165,6 +164,7 @@ fn build_suggestion(
applicability: &mut Applicability,
) {
let dividend_sugg = Sugg::hir_with_applicability(cx, lhs, "..", applicability).maybe_paren();
let rhs_ty = cx.typeck_results().expr_ty(rhs);
let type_suffix = if cx.typeck_results().expr_ty(lhs).is_numeric()
&& matches!(
lhs.kind,
Expand All @@ -182,7 +182,7 @@ fn build_suggestion(
}
)
) {
format!("_{}", cx.typeck_results().expr_ty(rhs))
format!("_{rhs_ty}")
} else {
String::new()
};
Expand All @@ -199,17 +199,20 @@ fn build_suggestion(
} else {
format!("{dividend_sugg_str}{type_suffix}")
};
let divisor_snippet = snippet_with_context(cx, rhs.span, expr.span.ctxt(), "..", applicability);

let sugg = format!("{suggestion_before_div_ceil}.div_ceil({})", divisor_snippet.0);
// Dereference the RHS if it is a reference type
let divisor_snippet = match Sugg::hir_with_context(cx, rhs, expr.span.ctxt(), "_", applicability) {
sugg if rhs_ty.is_ref() => sugg.deref(),
sugg => sugg,
};

span_lint_and_sugg(
cx,
MANUAL_DIV_CEIL,
expr.span,
"manually reimplementing `div_ceil`",
"consider using `.div_ceil()`",
sugg,
format!("{suggestion_before_div_ceil}.div_ceil({divisor_snippet})"),
*applicability,
);
}
5 changes: 5 additions & 0 deletions tests/ui/manual_div_ceil.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,8 @@ fn issue_13950() {
let _ = (-8 + y) / -7;
let _ = (y - 8) / -7;
}

fn issue_15705(size: u64, c: &u64) {
let _ = size.div_ceil(*c);
//~^ manual_div_ceil
}
5 changes: 5 additions & 0 deletions tests/ui/manual_div_ceil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,8 @@ fn issue_13950() {
let _ = (-8 + y) / -7;
let _ = (y - 8) / -7;
}

fn issue_15705(size: u64, c: &u64) {
let _ = (size + c - 1) / c;
//~^ manual_div_ceil
}
8 changes: 7 additions & 1 deletion tests/ui/manual_div_ceil.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,11 @@ error: manually reimplementing `div_ceil`
LL | let _ = (7 + x) / 8;
| ^^^^^^^^^^^ help: consider using `.div_ceil()`: `x.div_ceil(8)`

error: aborting due to 19 previous errors
error: manually reimplementing `div_ceil`
--> tests/ui/manual_div_ceil.rs:105:13
|
LL | let _ = (size + c - 1) / c;
| ^^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `size.div_ceil(*c)`

error: aborting due to 20 previous errors