-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[manual_div_ceil
]: init
#12987
[manual_div_ceil
]: init
#12987
Conversation
See #12894 for more details |
a90e8e6
to
0f32a26
Compare
/// ``` | ||
#[clippy::version = "1.81.0"] | ||
pub MANUAL_DIV_CEIL, | ||
complexity, |
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 in complexity
group, we might ended up suggesting something that the users cannot fix. (not to mention that it have an applicability of MachineApplicable
)
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:
let z: u32 = 7;
let _ = (z as i32 + (y - 1)) / y;
this suggests z as i32.div_ceil(y)
, you'll need a pair of parentheses to wrap the caller (see maybe_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.
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?
ah yes, it appears that you already have a function to check if it's Int
or Uint
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, meaning cx.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
Not related, but maybe we should do some benchmarks on (a + (b - 1)) / b vs div_ceil, it seems to be 1 instruction faster |
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.
Very good for a first iteration on the lint! Some minor improvements, this is really close to the finish line!
clippy_lints/src/manual_div_ceil.rs
Outdated
if let ExprKind::Binary(div_op, div_lhs, div_rhs) = expr.kind | ||
&& check_int_ty_and_feature(cx, div_lhs) | ||
&& check_int_ty_and_feature(cx, div_rhs) | ||
&& div_op.node == BinOpKind::Div |
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.
This check should be before the double check_int_ty_and_feature
, and you could also put the if let ExprKind::Binary(add_op, add_lhs, add_rhs) = div_lhs.kind
here, because it's repeated three times without any reason (more than changing variable name)
tests/ui/manual_div_ceil.rs
Outdated
let z = 11_u32; | ||
|
||
// Lint. | ||
let _ = (x + (y - 1)) / y; |
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.
You can use this notation //~ ERROR
to indicate an error 🌈
☔ The latest upstream changes (presumably #12873) made this pull request unmergeable. Please resolve the merge conflicts. |
876732d
to
b9eb1d2
Compare
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.
LGTM, thanks! ❤️
Sorry for the delay, I've been busy and turns out I didn't actually start the Final Comment Period with the team (which I could've sweared I did, but anyways 😅 ), in a few days I'll merge this PR.
Thanks for your contribution! And sorry again for keeping you waiting
In-between it gets accepted for merge by the team, could you squash the commits into one?
Looking at lintcheck, all new lint suggestions are valid, so no FPs there. But the suggestions generation for macros is broken:
|
b9eb1d2
to
ea83c3c
Compare
☔ The latest upstream changes (presumably #11476) made this pull request unmergeable. Please resolve the merge conflicts. |
ea83c3c
to
cb897d0
Compare
I've done some updating. But I'm not being able to replicate the macro bug that Philipp is talking about, is there any more information? @belyakov-am Can you replicate it? If we're not able to replicate it, we can safely merge this. |
☔ The latest upstream changes (presumably #13247) made this pull request unmergeable. Please resolve the merge conflicts. |
Thanks! And sorry for disappearing for so long |
I also doesn't show up in the latest lintcheck run anymore. So I guess my concern is resolved. |
390f038
to
9415e6e
Compare
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.
LGTM, thanks! ❤️ (I just synced and rebased to get this rerolling, just lib.rs
issues)
Just looked at lintcheck, everything looks correct! @bors r+ |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Suggest using
div_ceil()
instead of manual implementation for basic cases.Partially fixes #12894
changelog: new lint: [
manual_div_ceil
]