Skip to content
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

Merged
merged 1 commit into from
Sep 5, 2024
Merged

Conversation

belyakov-am
Copy link
Contributor

@belyakov-am belyakov-am commented Jun 23, 2024

Suggest using div_ceil() instead of manual implementation for basic cases.

Partially fixes #12894

changelog: new lint: [manual_div_ceil]

@rustbot
Copy link
Collaborator

rustbot commented Jun 23, 2024

r? @blyxyas

rustbot has assigned @blyxyas.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 23, 2024
@belyakov-am
Copy link
Contributor Author

See #12894 for more details

/// ```
#[clippy::version = "1.81.0"]
pub MANUAL_DIV_CEIL,
complexity,
Copy link
Member

@J-ZhengLi J-ZhengLi Jun 24, 2024

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~

Copy link
Contributor Author

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?

Copy link
Member

@J-ZhengLi J-ZhengLi Jun 27, 2024

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).

Copy link
Member

@J-ZhengLi J-ZhengLi Jun 27, 2024

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.

Copy link
Contributor Author

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

@blyxyas
Copy link
Member

blyxyas commented Jul 1, 2024

Not related, but maybe we should do some benchmarks on (a + (b - 1)) / b vs div_ceil, it seems to be 1 instruction faster

Copy link
Member

@blyxyas blyxyas left a 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 Show resolved Hide resolved
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
Copy link
Member

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)

clippy_lints/src/manual_div_ceil.rs Outdated Show resolved Hide resolved
let z = 11_u32;

// Lint.
let _ = (x + (y - 1)) / y;
Copy link
Member

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 🌈

@bors
Copy link
Collaborator

bors commented Jul 4, 2024

☔ The latest upstream changes (presumably #12873) made this pull request unmergeable. Please resolve the merge conflicts.

Copy link
Member

@blyxyas blyxyas left a 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?

@flip1995
Copy link
Member

Looking at lintcheck, all new lint suggestions are valid, so no FPs there. But the suggestions generation for macros is broken:

warning: manually reimplementing `div_ceil`
   --> target/lintcheck/sources/rand_core-0.6.0/src/impls.rs:59:26
    |
59  |         let chunk_size = (chunk_size_u8 + SIZE - 1) / SIZE;
    |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `chunk_size_u8.div_ceil(fill_via_chunks!(src, dest, u64))`
...
132 |     fill_via_chunks!(src, dest, u64)
    |     -------------------------------- in this macro invocation
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_div_ceil
    = note: this warning originates in the macro `fill_via_chunks` (in Nightly builds, run with -Z macro-backtrace for more info)

@bors
Copy link
Collaborator

bors commented Aug 28, 2024

☔ The latest upstream changes (presumably #11476) made this pull request unmergeable. Please resolve the merge conflicts.

@blyxyas
Copy link
Member

blyxyas commented Aug 28, 2024

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.

@bors
Copy link
Collaborator

bors commented Sep 3, 2024

☔ The latest upstream changes (presumably #13247) made this pull request unmergeable. Please resolve the merge conflicts.

@belyakov-am
Copy link
Contributor Author

Thanks! And sorry for disappearing for so long
I wasn't able to reproduce this one

@flip1995
Copy link
Member

flip1995 commented Sep 5, 2024

I also doesn't show up in the latest lintcheck run anymore. So I guess my concern is resolved.

Copy link
Member

@blyxyas blyxyas left a 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)

@blyxyas
Copy link
Member

blyxyas commented Sep 5, 2024

Just looked at lintcheck, everything looks correct!

@bors r+

@bors
Copy link
Collaborator

bors commented Sep 5, 2024

📌 Commit 9415e6e has been approved by blyxyas

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Sep 5, 2024

⌛ Testing commit 9415e6e with merge 9e9042a...

@bors
Copy link
Collaborator

bors commented Sep 5, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: blyxyas
Pushing 9e9042a to master...

@bors bors merged commit 9e9042a into rust-lang:master Sep 5, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add lints against more manual integer ops where direct methods exist
6 participants