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

[implied_bounds_in_impls]: include (previously omitted) associated types in suggestion #11459

Merged
merged 3 commits into from
Sep 9, 2023

Conversation

y21
Copy link
Member

@y21 y21 commented Sep 3, 2023

Fixes #11435

It now includes associated types from the implied bound that were omitted in the second bound. Example:

fn f() -> impl Iterator<Item = u8> + ExactSizeIterator> {..}

Suggestion before this change:

- pub fn my_iter() -> impl Iterator<Item = u32> + ExactSizeIterator {
+ pub fn my_iter() -> impl ExactSizeIterator {

It didn't include <Item = u32> on ExactSizeIterator. Now, with this change, it does.

- pub fn my_iter() -> impl Iterator<Item = u32> + ExactSizeIterator {
+ pub fn my_iter() -> impl ExactSizeIterator<Item = u32> {

We also now extend the span to include not just possible + ahead of it, but also behind it (an example for this is in the linked issue as well).
Note: The overall diff is a bit noisy, because building up the suggestion involves quite a bit more logic now and I decided to extract that into its own function. For that reason, I split this PR up into two commits. The first commit contains the actual "logic" changes. Second commit just moves code around.

changelog: [implied_bounds_in_impls]: include (previously omitted) associated types in suggestion
changelog: [implied_bounds_in_impls]: include the + behind bound if it's the last bound

@rustbot
Copy link
Collaborator

rustbot commented Sep 3, 2023

r? @blyxyas

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 3, 2023
@blyxyas blyxyas changed the title [implied_bounds_in_impls]: include omitted associated types in suggestion [implied_bounds_in_impls]: include (previously omitted) associated types in suggestion Sep 5, 2023
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.

Pretty good first revision ❤️ , thanks for being so committed to this one lint 🐱 (really, all current commits affecting implied_bounds_in_impls.rs are by you 🌵 )

clippy_lints/src/implied_bounds_in_impls.rs Outdated Show resolved Hide resolved
clippy_lints/src/implied_bounds_in_impls.rs Outdated Show resolved Hide resolved
clippy_lints/src/implied_bounds_in_impls.rs Show resolved Hide resolved
tests/ui/implied_bounds_in_impls.rs Show resolved Hide resolved
Comment on lines +78 to +86
let implied_span_extended = if let Some(next_bound) = opaque_ty.bounds.get(index + 1) {
poly_trait.span.to(next_bound.span().shrink_to_lo())
} else if index > 0
&& let Some(prev_bound) = opaque_ty.bounds.get(index - 1)
{
prev_bound.span().shrink_to_hi().to(poly_trait.span.shrink_to_hi())
} else {
poly_trait.span
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm kinda worried about this check, it's kinda inconsistent (one checking index and the other not), but it may not matter if we make sure it doesn't fail.

Could you add a test with 3 bounds? Maybe some with generics and some without them?

Copy link
Member Author

@y21 y21 Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why index - 1 has a check and the other doesn't is mainly that - 1 (I assume you're referring to this) can panic with debug assertions due to overflow if it's the first bound, while + 1 in this context can't.
I mean, a slice with usize::MAX elements (which is needed here for an overflow to occur) is not only realistically impossible because nobody has that much ram but also theoretically impossible, since afaik allocations in rust can never be bigger than isize::MAX.

Only if there's neither an element before or after it (i.e. only one bound) will it not extend the span

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 ❤️!

@blyxyas
Copy link
Member

blyxyas commented Sep 9, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Sep 9, 2023

📌 Commit 30846b1 has been approved by blyxyas

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Sep 9, 2023

⌛ Testing commit 30846b1 with merge 8c48b93...

@bors
Copy link
Contributor

bors commented Sep 9, 2023

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

@bors bors merged commit 8c48b93 into rust-lang:master Sep 9, 2023
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.

implied_bounds_in_impls false positive
4 participants