Skip to content

Commit 9e9042a

Browse files
committed
Auto merge of #12987 - belyakov-am:lint/manual_div_ceil, r=blyxyas
[`manual_div_ceil`]: init Suggest using `div_ceil()` instead of manual implementation for basic cases. Partially fixes #12894 changelog: new lint: [`manual_div_ceil`]
2 parents c41be9e + 9415e6e commit 9e9042a

12 files changed

+356
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5614,6 +5614,7 @@ Released 2018-09-13
56145614
[`manual_bits`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_bits
56155615
[`manual_c_str_literals`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_c_str_literals
56165616
[`manual_clamp`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_clamp
5617+
[`manual_div_ceil`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_div_ceil
56175618
[`manual_filter`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_filter
56185619
[`manual_filter_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_filter_map
56195620
[`manual_find`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_find

clippy_config/src/conf.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ fn calculate_dimensions(fields: &[&str]) -> (usize, Vec<usize>) {
864864
cmp::max(1, terminal_width / (SEPARATOR_WIDTH + max_field_width))
865865
});
866866

867-
let rows = (fields.len() + (columns - 1)) / columns;
867+
let rows = fields.len().div_ceil(columns);
868868

869869
let column_widths = (0..columns)
870870
.map(|column| {

clippy_config/src/msrvs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ msrv_aliases! {
2121
1,80,0 { BOX_INTO_ITER}
2222
1,77,0 { C_STR_LITERALS }
2323
1,76,0 { PTR_FROM_REF, OPTION_RESULT_INSPECT }
24+
1,73,0 { MANUAL_DIV_CEIL }
2425
1,71,0 { TUPLE_ARRAY_CONVERSIONS, BUILD_HASHER_HASH_ONE }
2526
1,70,0 { OPTION_RESULT_IS_VARIANT_AND, BINARY_HEAP_RETAIN }
2627
1,68,0 { PATH_MAIN_SEPARATOR_STR }

clippy_lints/src/declared_lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
301301
crate::manual_async_fn::MANUAL_ASYNC_FN_INFO,
302302
crate::manual_bits::MANUAL_BITS_INFO,
303303
crate::manual_clamp::MANUAL_CLAMP_INFO,
304+
crate::manual_div_ceil::MANUAL_DIV_CEIL_INFO,
304305
crate::manual_float_methods::MANUAL_IS_FINITE_INFO,
305306
crate::manual_float_methods::MANUAL_IS_INFINITE_INFO,
306307
crate::manual_hash_one::MANUAL_HASH_ONE_INFO,

clippy_lints/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ mod manual_assert;
203203
mod manual_async_fn;
204204
mod manual_bits;
205205
mod manual_clamp;
206+
mod manual_div_ceil;
206207
mod manual_float_methods;
207208
mod manual_hash_one;
208209
mod manual_is_ascii_check;
@@ -936,5 +937,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
936937
store.register_early_pass(|| Box::new(cfg_not_test::CfgNotTest));
937938
store.register_late_pass(|_| Box::new(zombie_processes::ZombieProcesses));
938939
store.register_late_pass(|_| Box::new(pointers_in_nomem_asm_block::PointersInNomemAsmBlock));
940+
store.register_late_pass(move |_| Box::new(manual_div_ceil::ManualDivCeil::new(conf)));
939941
// add lints here, do not remove this comment, it's used in `new_lint`
940942
}

clippy_lints/src/manual_div_ceil.rs

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
use clippy_config::msrvs::{self, Msrv};
2+
use clippy_utils::diagnostics::span_lint_and_sugg;
3+
use clippy_utils::source::snippet_with_applicability;
4+
use clippy_utils::sugg::Sugg;
5+
use clippy_utils::SpanlessEq;
6+
use rustc_ast::{BinOpKind, LitKind};
7+
use rustc_data_structures::packed::Pu128;
8+
use rustc_errors::Applicability;
9+
use rustc_hir::{Expr, ExprKind};
10+
use rustc_lint::{LateContext, LateLintPass};
11+
use rustc_middle::ty::{self};
12+
use rustc_session::impl_lint_pass;
13+
use rustc_span::symbol::Symbol;
14+
15+
use clippy_config::Conf;
16+
17+
declare_clippy_lint! {
18+
/// ### What it does
19+
/// Checks for an expression like `(x + (y - 1)) / y` which is a common manual reimplementation
20+
/// of `x.div_ceil(y)`.
21+
///
22+
/// ### Why is this bad?
23+
/// It's simpler, clearer and more readable.
24+
///
25+
/// ### Example
26+
/// ```no_run
27+
/// let x: i32 = 7;
28+
/// let y: i32 = 4;
29+
/// let div = (x + (y - 1)) / y;
30+
/// ```
31+
/// Use instead:
32+
/// ```no_run
33+
/// #![feature(int_roundings)]
34+
/// let x: i32 = 7;
35+
/// let y: i32 = 4;
36+
/// let div = x.div_ceil(y);
37+
/// ```
38+
#[clippy::version = "1.81.0"]
39+
pub MANUAL_DIV_CEIL,
40+
complexity,
41+
"manually reimplementing `div_ceil`"
42+
}
43+
44+
pub struct ManualDivCeil {
45+
msrv: Msrv,
46+
}
47+
48+
impl ManualDivCeil {
49+
#[must_use]
50+
pub fn new(conf: &'static Conf) -> Self {
51+
Self {
52+
msrv: conf.msrv.clone(),
53+
}
54+
}
55+
}
56+
57+
impl_lint_pass!(ManualDivCeil => [MANUAL_DIV_CEIL]);
58+
59+
impl<'tcx> LateLintPass<'tcx> for ManualDivCeil {
60+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
61+
if !self.msrv.meets(msrvs::MANUAL_DIV_CEIL) {
62+
return;
63+
}
64+
65+
let mut applicability = Applicability::MachineApplicable;
66+
67+
if let ExprKind::Binary(div_op, div_lhs, div_rhs) = expr.kind
68+
&& div_op.node == BinOpKind::Div
69+
&& check_int_ty_and_feature(cx, div_lhs)
70+
&& check_int_ty_and_feature(cx, div_rhs)
71+
&& let ExprKind::Binary(inner_op, inner_lhs, inner_rhs) = div_lhs.kind
72+
{
73+
// (x + (y - 1)) / y
74+
if let ExprKind::Binary(sub_op, sub_lhs, sub_rhs) = inner_rhs.kind
75+
&& inner_op.node == BinOpKind::Add
76+
&& sub_op.node == BinOpKind::Sub
77+
&& check_literal(sub_rhs)
78+
&& check_eq_expr(cx, sub_lhs, div_rhs)
79+
{
80+
build_suggestion(cx, expr, inner_lhs, div_rhs, &mut applicability);
81+
return;
82+
}
83+
84+
// ((y - 1) + x) / y
85+
if let ExprKind::Binary(sub_op, sub_lhs, sub_rhs) = inner_lhs.kind
86+
&& inner_op.node == BinOpKind::Add
87+
&& sub_op.node == BinOpKind::Sub
88+
&& check_literal(sub_rhs)
89+
&& check_eq_expr(cx, sub_lhs, div_rhs)
90+
{
91+
build_suggestion(cx, expr, inner_rhs, div_rhs, &mut applicability);
92+
return;
93+
}
94+
95+
// (x + y - 1) / y
96+
if let ExprKind::Binary(add_op, add_lhs, add_rhs) = inner_lhs.kind
97+
&& inner_op.node == BinOpKind::Sub
98+
&& add_op.node == BinOpKind::Add
99+
&& check_literal(inner_rhs)
100+
&& check_eq_expr(cx, add_rhs, div_rhs)
101+
{
102+
build_suggestion(cx, expr, add_lhs, div_rhs, &mut applicability);
103+
}
104+
}
105+
}
106+
107+
extract_msrv_attr!(LateContext);
108+
}
109+
110+
fn check_int_ty_and_feature(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
111+
let expr_ty = cx.typeck_results().expr_ty(expr);
112+
match expr_ty.peel_refs().kind() {
113+
ty::Uint(_) => true,
114+
ty::Int(_) => cx
115+
.tcx
116+
.features()
117+
.declared_features
118+
.contains(&Symbol::intern("int_roundings")),
119+
120+
_ => false,
121+
}
122+
}
123+
124+
fn check_literal(expr: &Expr<'_>) -> bool {
125+
if let ExprKind::Lit(lit) = expr.kind
126+
&& let LitKind::Int(Pu128(1), _) = lit.node
127+
{
128+
return true;
129+
}
130+
false
131+
}
132+
133+
fn check_eq_expr(cx: &LateContext<'_>, lhs: &Expr<'_>, rhs: &Expr<'_>) -> bool {
134+
SpanlessEq::new(cx).eq_expr(lhs, rhs)
135+
}
136+
137+
fn build_suggestion(
138+
cx: &LateContext<'_>,
139+
expr: &Expr<'_>,
140+
lhs: &Expr<'_>,
141+
rhs: &Expr<'_>,
142+
applicability: &mut Applicability,
143+
) {
144+
let dividend_sugg = Sugg::hir_with_applicability(cx, lhs, "..", applicability).maybe_par();
145+
let divisor_snippet = snippet_with_applicability(cx, rhs.span.source_callsite(), "..", applicability);
146+
147+
let sugg = format!("{dividend_sugg}.div_ceil({divisor_snippet})");
148+
149+
span_lint_and_sugg(
150+
cx,
151+
MANUAL_DIV_CEIL,
152+
expr.span,
153+
"manually reimplementing `div_ceil`",
154+
"consider using `.div_ceil()`",
155+
sugg,
156+
*applicability,
157+
);
158+
}

tests/ui/manual_div_ceil.fixed

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![warn(clippy::manual_div_ceil)]
2+
3+
fn main() {
4+
let x = 7_u32;
5+
let y = 4_u32;
6+
let z = 11_u32;
7+
8+
// Lint
9+
let _ = x.div_ceil(y); //~ ERROR: manually reimplementing `div_ceil`
10+
let _ = x.div_ceil(y); //~ ERROR: manually reimplementing `div_ceil`
11+
let _ = x.div_ceil(y); //~ ERROR: manually reimplementing `div_ceil`
12+
13+
let _ = 7_u32.div_ceil(4); //~ ERROR: manually reimplementing `div_ceil`
14+
let _ = (7_i32 as u32).div_ceil(4); //~ ERROR: manually reimplementing `div_ceil`
15+
16+
// No lint
17+
let _ = (x + (y - 2)) / y;
18+
let _ = (x + (y + 1)) / y;
19+
20+
let _ = (x + (y - 1)) / z;
21+
22+
let x_i = 7_i32;
23+
let y_i = 4_i32;
24+
let z_i = 11_i32;
25+
26+
// No lint because `int_roundings` feature is not enabled.
27+
let _ = (z as i32 + (y_i - 1)) / y_i;
28+
let _ = (7_u32 as i32 + (y_i - 1)) / y_i;
29+
let _ = (7_u32 as i32 + (4 - 1)) / 4;
30+
}

tests/ui/manual_div_ceil.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![warn(clippy::manual_div_ceil)]
2+
3+
fn main() {
4+
let x = 7_u32;
5+
let y = 4_u32;
6+
let z = 11_u32;
7+
8+
// Lint
9+
let _ = (x + (y - 1)) / y; //~ ERROR: manually reimplementing `div_ceil`
10+
let _ = ((y - 1) + x) / y; //~ ERROR: manually reimplementing `div_ceil`
11+
let _ = (x + y - 1) / y; //~ ERROR: manually reimplementing `div_ceil`
12+
13+
let _ = (7_u32 + (4 - 1)) / 4; //~ ERROR: manually reimplementing `div_ceil`
14+
let _ = (7_i32 as u32 + (4 - 1)) / 4; //~ ERROR: manually reimplementing `div_ceil`
15+
16+
// No lint
17+
let _ = (x + (y - 2)) / y;
18+
let _ = (x + (y + 1)) / y;
19+
20+
let _ = (x + (y - 1)) / z;
21+
22+
let x_i = 7_i32;
23+
let y_i = 4_i32;
24+
let z_i = 11_i32;
25+
26+
// No lint because `int_roundings` feature is not enabled.
27+
let _ = (z as i32 + (y_i - 1)) / y_i;
28+
let _ = (7_u32 as i32 + (y_i - 1)) / y_i;
29+
let _ = (7_u32 as i32 + (4 - 1)) / 4;
30+
}

tests/ui/manual_div_ceil.stderr

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error: manually reimplementing `div_ceil`
2+
--> tests/ui/manual_div_ceil.rs:9:13
3+
|
4+
LL | let _ = (x + (y - 1)) / y;
5+
| ^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `x.div_ceil(y)`
6+
|
7+
= note: `-D clippy::manual-div-ceil` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::manual_div_ceil)]`
9+
10+
error: manually reimplementing `div_ceil`
11+
--> tests/ui/manual_div_ceil.rs:10:13
12+
|
13+
LL | let _ = ((y - 1) + x) / y;
14+
| ^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `x.div_ceil(y)`
15+
16+
error: manually reimplementing `div_ceil`
17+
--> tests/ui/manual_div_ceil.rs:11:13
18+
|
19+
LL | let _ = (x + y - 1) / y;
20+
| ^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `x.div_ceil(y)`
21+
22+
error: manually reimplementing `div_ceil`
23+
--> tests/ui/manual_div_ceil.rs:13:13
24+
|
25+
LL | let _ = (7_u32 + (4 - 1)) / 4;
26+
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `7_u32.div_ceil(4)`
27+
28+
error: manually reimplementing `div_ceil`
29+
--> tests/ui/manual_div_ceil.rs:14:13
30+
|
31+
LL | let _ = (7_i32 as u32 + (4 - 1)) / 4;
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `(7_i32 as u32).div_ceil(4)`
33+
34+
error: aborting due to 5 previous errors
35+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#![warn(clippy::manual_div_ceil)]
2+
#![feature(int_roundings)]
3+
4+
fn main() {
5+
let x = 7_i32;
6+
let y = 4_i32;
7+
let z = 3_i32;
8+
let z_u: u32 = 11;
9+
10+
// Lint.
11+
let _ = x.div_ceil(y);
12+
let _ = x.div_ceil(y);
13+
let _ = x.div_ceil(y);
14+
15+
let _ = 7_i32.div_ceil(4);
16+
let _ = (7_i32 as u32).div_ceil(4);
17+
let _ = (7_u32 as i32).div_ceil(4);
18+
let _ = z_u.div_ceil(4);
19+
20+
// No lint.
21+
let _ = (x + (y - 2)) / y;
22+
let _ = (x + (y + 1)) / y;
23+
24+
let _ = (x + (y - 1)) / z;
25+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#![warn(clippy::manual_div_ceil)]
2+
#![feature(int_roundings)]
3+
4+
fn main() {
5+
let x = 7_i32;
6+
let y = 4_i32;
7+
let z = 3_i32;
8+
let z_u: u32 = 11;
9+
10+
// Lint.
11+
let _ = (x + (y - 1)) / y;
12+
let _ = ((y - 1) + x) / y;
13+
let _ = (x + y - 1) / y;
14+
15+
let _ = (7_i32 + (4 - 1)) / 4;
16+
let _ = (7_i32 as u32 + (4 - 1)) / 4;
17+
let _ = (7_u32 as i32 + (4 - 1)) / 4;
18+
let _ = (z_u + (4 - 1)) / 4;
19+
20+
// No lint.
21+
let _ = (x + (y - 2)) / y;
22+
let _ = (x + (y + 1)) / y;
23+
24+
let _ = (x + (y - 1)) / z;
25+
}

0 commit comments

Comments
 (0)