-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[manual_div_ceil
]: init
#12987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[manual_div_ceil
]: init
#12987
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
use clippy_config::msrvs::{self, Msrv}; | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use clippy_utils::sugg::Sugg; | ||
use clippy_utils::SpanlessEq; | ||
use rustc_ast::{BinOpKind, LitKind}; | ||
use rustc_data_structures::packed::Pu128; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_middle::ty::{self}; | ||
use rustc_session::impl_lint_pass; | ||
use rustc_span::symbol::Symbol; | ||
|
||
use clippy_config::Conf; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for an expression like `(x + (y - 1)) / y` which is a common manual reimplementation | ||
/// of `x.div_ceil(y)`. | ||
/// | ||
/// ### Why is this bad? | ||
/// It's simpler, clearer and more readable. | ||
/// | ||
/// ### Example | ||
/// ```no_run | ||
/// let x: i32 = 7; | ||
/// let y: i32 = 4; | ||
/// let div = (x + (y - 1)) / y; | ||
/// ``` | ||
/// Use instead: | ||
/// ```no_run | ||
/// #![feature(int_roundings)] | ||
/// let x: i32 = 7; | ||
/// let y: i32 = 4; | ||
/// let div = x.div_ceil(y); | ||
/// ``` | ||
#[clippy::version = "1.81.0"] | ||
pub MANUAL_DIV_CEIL, | ||
complexity, | ||
"manually reimplementing `div_ceil`" | ||
} | ||
|
||
pub struct ManualDivCeil { | ||
msrv: Msrv, | ||
} | ||
|
||
impl ManualDivCeil { | ||
#[must_use] | ||
pub fn new(conf: &'static Conf) -> Self { | ||
Self { | ||
msrv: conf.msrv.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl_lint_pass!(ManualDivCeil => [MANUAL_DIV_CEIL]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for ManualDivCeil { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) { | ||
if !self.msrv.meets(msrvs::MANUAL_DIV_CEIL) { | ||
return; | ||
} | ||
|
||
let mut applicability = Applicability::MachineApplicable; | ||
|
||
if let ExprKind::Binary(div_op, div_lhs, div_rhs) = expr.kind | ||
&& div_op.node == BinOpKind::Div | ||
&& check_int_ty_and_feature(cx, div_lhs) | ||
&& check_int_ty_and_feature(cx, div_rhs) | ||
&& let ExprKind::Binary(inner_op, inner_lhs, inner_rhs) = div_lhs.kind | ||
{ | ||
// (x + (y - 1)) / y | ||
if let ExprKind::Binary(sub_op, sub_lhs, sub_rhs) = inner_rhs.kind | ||
&& inner_op.node == BinOpKind::Add | ||
&& sub_op.node == BinOpKind::Sub | ||
&& check_literal(sub_rhs) | ||
&& check_eq_expr(cx, sub_lhs, div_rhs) | ||
{ | ||
build_suggestion(cx, expr, inner_lhs, div_rhs, &mut applicability); | ||
return; | ||
} | ||
|
||
// ((y - 1) + x) / y | ||
if let ExprKind::Binary(sub_op, sub_lhs, sub_rhs) = inner_lhs.kind | ||
&& inner_op.node == BinOpKind::Add | ||
&& sub_op.node == BinOpKind::Sub | ||
&& check_literal(sub_rhs) | ||
&& check_eq_expr(cx, sub_lhs, div_rhs) | ||
{ | ||
build_suggestion(cx, expr, inner_rhs, div_rhs, &mut applicability); | ||
return; | ||
} | ||
|
||
// (x + y - 1) / y | ||
if let ExprKind::Binary(add_op, add_lhs, add_rhs) = inner_lhs.kind | ||
&& inner_op.node == BinOpKind::Sub | ||
&& add_op.node == BinOpKind::Add | ||
&& check_literal(inner_rhs) | ||
&& check_eq_expr(cx, add_rhs, div_rhs) | ||
{ | ||
build_suggestion(cx, expr, add_lhs, div_rhs, &mut applicability); | ||
} | ||
} | ||
} | ||
|
||
extract_msrv_attr!(LateContext); | ||
} | ||
|
||
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() { | ||
ty::Uint(_) => true, | ||
ty::Int(_) => cx | ||
.tcx | ||
.features() | ||
.declared_features | ||
.contains(&Symbol::intern("int_roundings")), | ||
|
||
_ => false, | ||
} | ||
} | ||
|
||
fn check_literal(expr: &Expr<'_>) -> bool { | ||
if let ExprKind::Lit(lit) = expr.kind | ||
&& let LitKind::Int(Pu128(1), _) = lit.node | ||
{ | ||
return true; | ||
} | ||
false | ||
} | ||
|
||
fn check_eq_expr(cx: &LateContext<'_>, lhs: &Expr<'_>, rhs: &Expr<'_>) -> bool { | ||
SpanlessEq::new(cx).eq_expr(lhs, rhs) | ||
} | ||
|
||
fn build_suggestion( | ||
cx: &LateContext<'_>, | ||
expr: &Expr<'_>, | ||
lhs: &Expr<'_>, | ||
rhs: &Expr<'_>, | ||
applicability: &mut Applicability, | ||
) { | ||
let dividend_sugg = Sugg::hir_with_applicability(cx, lhs, "..", applicability).maybe_par(); | ||
let divisor_snippet = snippet_with_applicability(cx, rhs.span.source_callsite(), "..", applicability); | ||
|
||
let sugg = format!("{dividend_sugg}.div_ceil({divisor_snippet})"); | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
MANUAL_DIV_CEIL, | ||
expr.span, | ||
"manually reimplementing `div_ceil`", | ||
"consider using `.div_ceil()`", | ||
sugg, | ||
*applicability, | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#![warn(clippy::manual_div_ceil)] | ||
|
||
fn main() { | ||
let x = 7_u32; | ||
let y = 4_u32; | ||
let z = 11_u32; | ||
|
||
// Lint | ||
let _ = x.div_ceil(y); //~ ERROR: manually reimplementing `div_ceil` | ||
let _ = x.div_ceil(y); //~ ERROR: manually reimplementing `div_ceil` | ||
let _ = x.div_ceil(y); //~ ERROR: manually reimplementing `div_ceil` | ||
|
||
let _ = 7_u32.div_ceil(4); //~ ERROR: manually reimplementing `div_ceil` | ||
let _ = (7_i32 as u32).div_ceil(4); //~ ERROR: manually reimplementing `div_ceil` | ||
|
||
// No lint | ||
let _ = (x + (y - 2)) / y; | ||
let _ = (x + (y + 1)) / y; | ||
|
||
let _ = (x + (y - 1)) / z; | ||
|
||
let x_i = 7_i32; | ||
let y_i = 4_i32; | ||
let z_i = 11_i32; | ||
|
||
// No lint because `int_roundings` feature is not enabled. | ||
let _ = (z as i32 + (y_i - 1)) / y_i; | ||
let _ = (7_u32 as i32 + (y_i - 1)) / y_i; | ||
let _ = (7_u32 as i32 + (4 - 1)) / 4; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#![warn(clippy::manual_div_ceil)] | ||
|
||
fn main() { | ||
let x = 7_u32; | ||
let y = 4_u32; | ||
let z = 11_u32; | ||
|
||
// Lint | ||
let _ = (x + (y - 1)) / y; //~ ERROR: manually reimplementing `div_ceil` | ||
let _ = ((y - 1) + x) / y; //~ ERROR: manually reimplementing `div_ceil` | ||
let _ = (x + y - 1) / y; //~ ERROR: manually reimplementing `div_ceil` | ||
|
||
let _ = (7_u32 + (4 - 1)) / 4; //~ ERROR: manually reimplementing `div_ceil` | ||
let _ = (7_i32 as u32 + (4 - 1)) / 4; //~ ERROR: manually reimplementing `div_ceil` | ||
|
||
// No lint | ||
let _ = (x + (y - 2)) / y; | ||
let _ = (x + (y + 1)) / y; | ||
|
||
let _ = (x + (y - 1)) / z; | ||
|
||
let x_i = 7_i32; | ||
let y_i = 4_i32; | ||
let z_i = 11_i32; | ||
|
||
// No lint because `int_roundings` feature is not enabled. | ||
let _ = (z as i32 + (y_i - 1)) / y_i; | ||
let _ = (7_u32 as i32 + (y_i - 1)) / y_i; | ||
let _ = (7_u32 as i32 + (4 - 1)) / 4; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
error: manually reimplementing `div_ceil` | ||
--> tests/ui/manual_div_ceil.rs:9:13 | ||
| | ||
LL | let _ = (x + (y - 1)) / y; | ||
| ^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `x.div_ceil(y)` | ||
| | ||
= note: `-D clippy::manual-div-ceil` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::manual_div_ceil)]` | ||
|
||
error: manually reimplementing `div_ceil` | ||
--> tests/ui/manual_div_ceil.rs:10:13 | ||
| | ||
LL | let _ = ((y - 1) + x) / y; | ||
| ^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `x.div_ceil(y)` | ||
|
||
error: manually reimplementing `div_ceil` | ||
--> tests/ui/manual_div_ceil.rs:11:13 | ||
| | ||
LL | let _ = (x + y - 1) / y; | ||
| ^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `x.div_ceil(y)` | ||
|
||
error: manually reimplementing `div_ceil` | ||
--> tests/ui/manual_div_ceil.rs:13:13 | ||
| | ||
LL | let _ = (7_u32 + (4 - 1)) / 4; | ||
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `7_u32.div_ceil(4)` | ||
|
||
error: manually reimplementing `div_ceil` | ||
--> tests/ui/manual_div_ceil.rs:14:13 | ||
| | ||
LL | let _ = (7_i32 as u32 + (4 - 1)) / 4; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `(7_i32 as u32).div_ceil(4)` | ||
|
||
error: aborting due to 5 previous errors | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#![warn(clippy::manual_div_ceil)] | ||
#![feature(int_roundings)] | ||
|
||
fn main() { | ||
let x = 7_i32; | ||
let y = 4_i32; | ||
let z = 3_i32; | ||
let z_u: u32 = 11; | ||
|
||
// Lint. | ||
let _ = x.div_ceil(y); | ||
let _ = x.div_ceil(y); | ||
let _ = x.div_ceil(y); | ||
|
||
let _ = 7_i32.div_ceil(4); | ||
let _ = (7_i32 as u32).div_ceil(4); | ||
let _ = (7_u32 as i32).div_ceil(4); | ||
let _ = z_u.div_ceil(4); | ||
|
||
// No lint. | ||
let _ = (x + (y - 2)) / y; | ||
let _ = (x + (y + 1)) / y; | ||
|
||
let _ = (x + (y - 1)) / z; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#![warn(clippy::manual_div_ceil)] | ||
#![feature(int_roundings)] | ||
|
||
fn main() { | ||
let x = 7_i32; | ||
let y = 4_i32; | ||
let z = 3_i32; | ||
let z_u: u32 = 11; | ||
|
||
// Lint. | ||
let _ = (x + (y - 1)) / y; | ||
let _ = ((y - 1) + x) / y; | ||
let _ = (x + y - 1) / y; | ||
|
||
let _ = (7_i32 + (4 - 1)) / 4; | ||
let _ = (7_i32 as u32 + (4 - 1)) / 4; | ||
let _ = (7_u32 as i32 + (4 - 1)) / 4; | ||
let _ = (z_u + (4 - 1)) / 4; | ||
|
||
// No lint. | ||
let _ = (x + (y - 2)) / y; | ||
let _ = (x + (y + 1)) / y; | ||
|
||
let _ = (x + (y - 1)) / z; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some drive-by thoughts 😄 :
It seems like
div_ceil
for signed integers are still unstable, so if this was put incomplexity
group, we might ended up suggesting something that the users cannot fix. (not to mention that it have an applicability ofMachineApplicable
)As for unsigned integers, this function was stabilized for 1.73.0 and onward, so a
msrv
configuration would be very nice~There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your input!
I'm struggling to find a proper way to separate lint for stable and unstable (i.e. unsigned and signed integers)
Can you please point me in a direction that would be helpful for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, when a binary operand is cast expr, the suggestion might be broken, something like:
this suggests
z as i32.div_ceil(y)
, you'll need a pair of parentheses to wrap the caller (seemaybe_par
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah yes, it appears that you already have a function to check if it's
Int
orUint
right?Then you should be able to separate it into two scenarios, one is where the type is
ty::Int
, in this case, don't offer a fixable suggestion yet... unless the user already have that required feature enabled, meaningcx.tcx.features().declared_features.contains(&Symbol::inter("int_roundings"))
is true.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot!
This
declared_features
hashset is very insightful, I'd never find it on my own