Skip to content

assist: replace generic with impl trait #14626

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

Closed
pickx opened this issue Apr 22, 2023 · 1 comment · Fixed by #14816
Closed

assist: replace generic with impl trait #14626

pickx opened this issue Apr 22, 2023 · 1 comment · Fixed by #14816
Assignees
Labels
A-assists C-feature Category: feature request

Comments

@pickx
Copy link

pickx commented Apr 22, 2023

fn new<P: AsRef<Path>>(location: P) -> Self { }

Placing the cursor on P: AsRef<Path> or on the P of location: P should trigger an assist to change this to

fn new(location: impl AsRef<Path>) -> Self { }

We already have an assist replace impl trait with generic that does the opposite.

@pickx pickx added the C-feature Category: feature request label Apr 22, 2023
@justahero
Copy link
Contributor

@rustbot claim

@bors bors closed this as completed in e963846 May 26, 2023
bors added a commit that referenced this issue Jun 2, 2023
Fix Assist "replace named generic type with impl trait"

This is a follow-up PR to fix the assist "replace named generic type with impl trait" described in #14626 to filter invalid param types. It integrates the feedback given in PR #14816 .

The change updates the logic to determine when a function parameter is safe to replace a type param with its trait implementation. Some parameter definitions are invalid & should not be replaced by their traits, therefore skipping the assist completely.

First, all usages of the generic type under the cursor are determined. These usage references are checked to see if they occur outside the function parameter list. If an outside reference is found, e.g. in body, return type or where clause, the assist is skipped. All remaining usages need to appear only in the function param list. For each usage the param type is further inspected to see if it's valid. The logic to determine if a function parameter is valid, follows a heuristic and may not cover all possible parameter definitions.

With this change the following param types (as given in [this comment](#14816 (comment))) are not replaced & therefore skip the assist.

```rust
fn foo<P: Trait>(
    _: <P as Trait>::Assoc,          // within path type qualifier
    _: <() as OtherTrait<P>>::Assoc, // same as above
    _: P::Assoc,                     // associated type shorthand
    _: impl OtherTrait<P>            // generic arg in impl trait (note that associated type bindings are fine)
    _: &dyn Fn(P)                    // param type and/or return type for Fn* traits
) {}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-assists C-feature Category: feature request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants