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

Add extra_unused_type_parameters lint #10028

Merged
merged 1 commit into from
Feb 3, 2023

Conversation

mkrasnitski
Copy link
Contributor

@mkrasnitski mkrasnitski commented Dec 3, 2022

Closes #9240. Keeping this as draft for now, because of a bug that I don't know how to fix. It seems that opaque return types are not walked properly, for some unknown reason. As in, the following:

fn used_ret_opaque<A>() -> impl Iterator<Item = A> {
    std::iter::empty()
}

This triggers the lint even though it shouldn't. Discussion on Zulip didn't illuminate any possible reasons why, so PR-ing this now to increase visibility.


changelog: new lint: [extra_unused_type_parameters]
#10028

@rustbot
Copy link
Collaborator

rustbot commented Dec 3, 2022

r? @dswij

(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 Dec 3, 2022
@mkrasnitski
Copy link
Contributor Author

After some digging, it seems the corresponding hir type was added all the way back in rust-lang/rust#54741 (since renamed to TyKind::OpaqueDef), and has only ever contained lifetime bounds on the impl Trait, not any parameter bounds. Does the Item = A count as a parameter bound in this case?

cc-ing @oli-obk

@oli-obk
Copy link
Contributor

oli-obk commented Dec 5, 2022

When you seen an OpaqueDef you basically need to fetch the HIR of the item it references and walk that, too. We're mangling the HIR quite a bit here to make the queries like generics_of and predicates_of simpler. This is in the process of being changed and I don't know what the end result will be. Maybe for now just mark all generic params as used if there's an OpaqueDef?

@mkrasnitski
Copy link
Contributor Author

When you seen an OpaqueDef you basically need to fetch the HIR of the item it references and walk that, too.

Turns out walk_ty already does this by calling visit_nested_item on the id. However, that involves a check that Self::NestedFilter::INTER is set, and I'm using OnlyBodies which keeps it false. That's the reason the walk was stopping short of descending down into the OpaqueDef. Maybe that behavior should be changed, since the filter check seems not to have been intended for this edge-case?

@mkrasnitski mkrasnitski marked this pull request as ready for review December 6, 2022 22:38
@mkrasnitski
Copy link
Contributor Author

This PR is ready for review/merge, I think.

@flip1995
Copy link
Member

flip1995 commented Dec 7, 2022

r? @flip1995

@rustbot rustbot assigned flip1995 and unassigned dswij Dec 7, 2022
@flip1995
Copy link
Member

flip1995 commented Dec 7, 2022

Assigning myself, because we talked about this on Zulip a lot. I will probably be busy with release work in the next week, so review might take a bit.

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

Here's an early review. I still have to look at the tests and think a little bit about all the cases I can come up with. Impl looks already really clean AFAICT from a quick glance.

clippy_lints/src/extra_unused_type_parameters.rs Outdated Show resolved Hide resolved
@mkrasnitski mkrasnitski force-pushed the extra_unused_type_parameters branch from e12dbd7 to 9fdbf4c Compare December 9, 2022 00:43
@mkrasnitski
Copy link
Contributor Author

mkrasnitski commented Dec 9, 2022

I've found an edge case where the lint doesn't behave quite as expected:

fn nested_bounds<A, B, C, D>()
where
    B: Iterator<Item = A>,
    C: Iterator<Item = B>,
    D: Iterator<Item = C>,
{
}

This produces the following lint output:

warning: type parameter goes unused in function definition
  --> tests/ui/extra_unused_type_parameters.rs:61:27
   |
61 | fn nested_bounds<A, B, C, D>()
   |                           ^
   |
   = help: consider removing the parameter
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#extra_unused_type_parameters

Whereas I would expect the following:

warning: type parameters go unused in function definition
  --> tests/ui/extra_unused_type_parameters.rs:61:17
   |
61 | fn nested_bounds<A, B, C, D>()
   |                 ^^^^^^^^^^^^
   |
   = help: consider removing the parameters
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#extra_unused_type_parameters

This is because I mark parameters used if they appear in the right-hand side of a where-clause. What I failed to consider, was if the left side also goes unused, then I shouldn't mark the nested parameter as unused. What is the consensus here, should this be fixed? Subsequent applications of the lint will eventually get rid of all of the parameters, but it would be nice if they were all detected at once. However, I feel like that would require a fair bit of engineering effort.

@bors
Copy link
Contributor

bors commented Dec 9, 2022

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

@mkrasnitski mkrasnitski force-pushed the extra_unused_type_parameters branch 2 times, most recently from edfb1a5 to f3faaeb Compare December 10, 2022 09:18
@mkrasnitski mkrasnitski force-pushed the extra_unused_type_parameters branch from f3faaeb to d5785f0 Compare December 12, 2022 23:20
@mkrasnitski
Copy link
Contributor Author

This is ready for re-review, when you have the time @flip1995

@bors
Copy link
Contributor

bors commented Dec 19, 2022

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

@mkrasnitski mkrasnitski force-pushed the extra_unused_type_parameters branch from d5785f0 to e6f3cfb Compare December 20, 2022 01:16
@bors
Copy link
Contributor

bors commented Dec 20, 2022

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

@mkrasnitski mkrasnitski force-pushed the extra_unused_type_parameters branch from e6f3cfb to e1681e5 Compare December 21, 2022 00:32
@flip1995
Copy link
Member

Sorry for taking so long. Last week before vacation in $day_job is a bit stressful. I'll get back to this during the holidays.

@bors
Copy link
Contributor

bors commented Dec 25, 2022

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

@mkrasnitski mkrasnitski force-pushed the extra_unused_type_parameters branch from e1681e5 to f7ebea1 Compare December 27, 2022 17:42
@mkrasnitski
Copy link
Contributor Author

mkrasnitski commented Jan 12, 2023

Will this still be in time to get merged into 1.67? Unsure, since beta has been cut for a while already.

@flip1995
Copy link
Member

Sadly not. It would've to get merged today to make it in 1.68. I'm so sorry that I didn't get to review this in time.

@bors
Copy link
Contributor

bors commented Jan 19, 2023

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

@mkrasnitski mkrasnitski force-pushed the extra_unused_type_parameters branch 2 times, most recently from 43fdcf5 to 572a014 Compare January 26, 2023 06:42
@bors
Copy link
Contributor

bors commented Jan 31, 2023

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

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

I finally managed to get to this. Looks really good overall. I have some style questions left.

You're concern about those cascading bounds is not an issue we should address. Just applying this lint multiple times is totally fine and pretty much how Clippy works. It's not worth putting in the effort to try and fix this and then potentially introducing FPs or FNs.

clippy_lints/src/extra_unused_type_parameters.rs Outdated Show resolved Hide resolved
clippy_lints/src/extra_unused_type_parameters.rs Outdated Show resolved Hide resolved
self.generics.span.into()
};

span_lint_and_help(self.cx, EXTRA_UNUSED_TYPE_PARAMETERS, span, msg, None, help);
Copy link
Member

Choose a reason for hiding this comment

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

You've put so much effort in crafting this awesome spans and message, I think this should be span_lint_and_sugg. The non-multispan cases can then be tested with // run-rustfix in the test file. You'll have to split up the test files for that though: fixable and unfixable.

@mkrasnitski mkrasnitski force-pushed the extra_unused_type_parameters branch from 572a014 to 08e170e Compare February 2, 2023 15:45
@mkrasnitski
Copy link
Contributor Author

I've pushed some changes that hopefully address your concerns. It may be a few days before I can get to changing to using span_lint_and_sugg, as that would require changes in how I'd form the suggestion, and overall it will take a bit of effort.

Also, I'd like to at some point just squash all these commits before the PR is merged, as I don't think they're super informative on their own.

Let me know what you think of the new changes.

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

Only one comment left.

@flip1995
Copy link
Member

flip1995 commented Feb 2, 2023

It may be a few days before I can get to changing to using span_lint_and_sugg, as that would require changes in how I'd form the suggestion, and overall it will take a bit of effort.

We can also just do this in a follow up PR. This is not blocking for this PR.

Also, I'd like to at some point just squash all these commits before the PR is merged

Sure, I'll wait for that after approving this PR 👍

@mkrasnitski mkrasnitski force-pushed the extra_unused_type_parameters branch from 08e170e to fba16e2 Compare February 3, 2023 00:37
@mkrasnitski
Copy link
Contributor Author

I've gone ahead and squashed everything down. Should be ready for merge now.

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

Awesome work! Easy to understand code, really concise and well commented+documented. And great suggestion building (one of my favorite parts when working on diagnostics).

@flip1995
Copy link
Member

flip1995 commented Feb 3, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Feb 3, 2023

📌 Commit fba16e2 has been approved by flip1995

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Feb 3, 2023

⌛ Testing commit fba16e2 with merge 8a98609...

@bors
Copy link
Contributor

bors commented Feb 3, 2023

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

1 similar comment
@bors
Copy link
Contributor

bors commented Feb 3, 2023

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

@bors bors merged commit 8a98609 into rust-lang:master Feb 3, 2023
@mkrasnitski
Copy link
Contributor Author

@flip1995 Thanks for all your help on this work, I really appreciate it!

@flip1995
Copy link
Member

flip1995 commented Feb 3, 2023

Thank you for sticking with it and patiently waiting for my review!

@mkrasnitski
Copy link
Contributor Author

I now realize it's been pretty much 6 months since I opened the tracking issue for this - I went in with basically zero knowledge about compiler internals, but now I feel a lot better equipped to go off and tackle more work :)

@flip1995
Copy link
Member

flip1995 commented Feb 3, 2023

Sounds great! I would be happy to see more contributions from you to Clippy! But if you want to tackle bigger challenges also have a look at rustc :)

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.

Lint for unused type parameters on functions
6 participants