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

[fix] [redundant_closure_for_method_calls] Suggest relative paths for local modules #11370

Conversation

modelflat
Copy link
Contributor

Fixes #10854.

Currently, redundant_closure_for_method_calls suggest incorrect paths when a method defined on a struct within inline mod is referenced (see the description in the aforementioned issue for an example; also see this playground link for the current-version output for the test cases added in this PR). It will now try to construct a relative path path to the module and suggest it instead.

changelog: [redundant_closure_for_method_calls] Fix incorrect path suggestions for types within local modules

@rustbot
Copy link
Collaborator

rustbot commented Aug 21, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Centri3 (or someone else) soon.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 21, 2023
@Centri3
Copy link
Member

Centri3 commented Aug 21, 2023

Will review later, but can you extract get_ufcs_type_name to clippy_utils? There are quite a few lints that can benefit from this.

@modelflat
Copy link
Contributor Author

@Centri3 Thanks for the suggestion! I moved get_ucfs_type_name to lib.rs in the utils. Considering the size of lib.rs I'd probably prefer to put it into a separate module, but I'm not sure which one is the most suitable... so I guess lib.rs it is 🙂

// mod_a::sym :: ::
// (looking at mod_c)
EitherOrBoth::Right(r) => {
if let DefPathData::TypeNs(_) = r.data {
Copy link
Member

Choose a reason for hiding this comment

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

This would probably be better as a guard instead (same with the above)

}
// Otherwise ignore
},
EitherOrBoth::Both(l, r) => match (l.data, r.data) {
Copy link
Member

Choose a reason for hiding this comment

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

afaict, this can be simplified by pushing l and r if they're TypeNs.

clippy_utils/src/lib.rs Outdated Show resolved Hide resolved
Comment on lines 2909 to 2914
| rustc_ty::Dynamic(..)
| rustc_ty::Never
| rustc_ty::RawPtr(_)
| rustc_ty::Ref(..)
| rustc_ty::Slice(_)
| rustc_ty::Tuple(_) => format!("<{}>", EarlyBinder::bind(ty).instantiate(tcx, args)),
Copy link
Member

@Centri3 Centri3 Aug 22, 2023

Choose a reason for hiding this comment

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

We need to handle these too, I'm pretty sure. This should recursively go through each nested type and get the relative path for each ADT.

Not an issue for the lint in question, but if somebody uses this on something like (super::A, super::super::B) it'll still be broken afaik.

@bors
Copy link
Collaborator

bors commented Aug 24, 2023

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

@blyxyas
Copy link
Member

blyxyas commented Oct 26, 2023

Seeing as Catherine is pretty busy (judging from her GitHub contribution graph) so I'll take from now on some of her reviews.

r? @blyxyas

@rustbot rustbot assigned blyxyas and unassigned Centri3 Oct 26, 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.

Very good first iteration.

///
/// Returned path can be either absolute (for methods defined non-locally), or relative (for local
/// methods).
pub fn get_ufcs_type_name<'tcx>(
Copy link
Member

Choose a reason for hiding this comment

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

This could have a much more descriptive name.

Suggested change
pub fn get_ufcs_type_name<'tcx>(
pub fn path_from_caller_to_method_type<'tcx>(

Or something like that.

tests/ui/eta.rs Outdated
@@ -399,3 +399,56 @@ fn _closure_with_types() {
let _ = f2(|x: u32| f(x));
let _ = f2(|x| -> u32 { f(x) });
}

// https://github.com/rust-lang/rust-clippy/issues/10854
mod redundant_closure_for_method_calls_resolve_suggested_paths_to_relative {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of having a module name so long, I think it would be best to name it issue_10854 and explain in a doc comment what it is supposed to test .

Comment on lines 2998 to 3000
}

repeat(String::from("super")).take(go_up_by).chain(path).join("::")
Copy link
Member

Choose a reason for hiding this comment

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

Seeing the result, I think that we should maybe cap go_up_by at like 2, AFAIK people prefer having crate::a::b than super::super::super::super::b in their codebase.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I really like this idea, implemented!

@blyxyas blyxyas added the hacktoberfest-accepted For PRs that were accepted for hacktoberfest (and will be merged soon) label Oct 30, 2023
@blyxyas
Copy link
Member

blyxyas commented Nov 28, 2023

@modelflat Are you there?

@modelflat
Copy link
Contributor Author

@blyxyas yep, hopefully I'll get some time to work on it in December

@blyxyas
Copy link
Member

blyxyas commented Jan 26, 2024

@modelflat Would you like for other person to finish this contribution?

@modelflat modelflat force-pushed the suggest-relpath-in-redundant-closure-for-method-calls branch from 0079622 to 7105961 Compare January 27, 2024 20:44
@modelflat
Copy link
Contributor Author

@blyxyas Hi! Sorry I completely forgot about this one. I've got some spare time now so can work on it. Pls take a look at the newest changes when you have time, I resolved conflicts and addressed your comments

@blyxyas blyxyas removed the hacktoberfest-accepted For PRs that were accepted for hacktoberfest (and will be merged soon) label Jan 29, 2024
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! Could you squash these commits? ฅ*•ω•*ฅ♡

tests/ui/eta.rs Outdated Show resolved Hide resolved
@modelflat modelflat force-pushed the suggest-relpath-in-redundant-closure-for-method-calls branch from dc4146e to 0b7dd75 Compare January 29, 2024 11:32
Fixes rust-lang#10854.

Co-authored-by: Alejandra González <blyxyas@gmail.com>
@modelflat modelflat force-pushed the suggest-relpath-in-redundant-closure-for-method-calls branch from 0b7dd75 to b3d5377 Compare January 29, 2024 11:35
@blyxyas
Copy link
Member

blyxyas commented Jan 29, 2024

@bors r+

@bors
Copy link
Collaborator

bors commented Jan 29, 2024

📌 Commit b3d5377 has been approved by blyxyas

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Jan 29, 2024

⌛ Testing commit b3d5377 with merge 85d8082...

bors added a commit that referenced this pull request Jan 29, 2024
…-for-method-calls, r=blyxyas

[fix] [`redundant_closure_for_method_calls`] Suggest relative paths for local modules

Fixes #10854.

Currently, `redundant_closure_for_method_calls` suggest incorrect paths when a method defined on a struct within inline mod is referenced (see the description in the aforementioned issue for an example; also see [this playground link](https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=f7d3c5b2663c9bd3ab7abdb0bd38ee43) for the current-version output for the test cases added in this PR). It will now try to construct a relative path path to the module and suggest it instead.

changelog: [`redundant_closure_for_method_calls`] Fix incorrect path suggestions for types within local modules
@bors
Copy link
Collaborator

bors commented Jan 29, 2024

💔 Test failed - checks-action_test

@blyxyas
Copy link
Member

blyxyas commented Jan 29, 2024

@bors retry

bors added a commit that referenced this pull request Jan 29, 2024
…-for-method-calls, r=blyxyas

[fix] [`redundant_closure_for_method_calls`] Suggest relative paths for local modules

Fixes #10854.

Currently, `redundant_closure_for_method_calls` suggest incorrect paths when a method defined on a struct within inline mod is referenced (see the description in the aforementioned issue for an example; also see [this playground link](https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=f7d3c5b2663c9bd3ab7abdb0bd38ee43) for the current-version output for the test cases added in this PR). It will now try to construct a relative path path to the module and suggest it instead.

changelog: [`redundant_closure_for_method_calls`] Fix incorrect path suggestions for types within local modules
@bors
Copy link
Collaborator

bors commented Jan 29, 2024

⌛ Testing commit b3d5377 with merge 813d672...

@bors
Copy link
Collaborator

bors commented Jan 29, 2024

💥 Test timed out

@blyxyas
Copy link
Member

blyxyas commented Jan 29, 2024

@bors retry

@bors
Copy link
Collaborator

bors commented Jan 29, 2024

⌛ Testing commit b3d5377 with merge 3cd713a...

@bors
Copy link
Collaborator

bors commented Jan 29, 2024

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

1 similar comment
@bors
Copy link
Collaborator

bors commented Jan 29, 2024

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

@bors bors merged commit 3cd713a into rust-lang:master Jan 29, 2024
5 checks passed
@bors
Copy link
Collaborator

bors commented Jan 29, 2024

👀 Test was successful, but fast-forwarding failed: 422 Changes must be made through a pull request.

@modelflat modelflat deleted the suggest-relpath-in-redundant-closure-for-method-calls branch January 29, 2024 17:57
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.

redundant_closure_for_method_calls suggests the wrong path for types across inline modules
5 participants