rust-lang / rust-lang/rust-analyzer

Replace named generic with impl assist when the type variable occurs more than once

Open
#15,735 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

The "Replace named generic with impl trait" assist can change the semantics of the code if the type variable being replaced occurs more than once. For example, the following code fails to compile after applying the assist (in other cases it can be a silent change):

fn common_count<T: Eq>(slice: &[(T, T)]) -> usize {
    slice.iter().filter(|(a, b)| a == b).count()
}

Either the assist should not apply in such cases or at least this behavior should be documented.

rust-analyzer version: 0.3.1689-standalone

rustc version: rustc 1.73.0 (cc66ad468 2023-10-03)

relevant settings: None

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the implementation and tests for the "Replace named generic with impl trait" assist. Reproduce the transformation with the provided common_count example, then determine whether repeated uses of the type variable should prevent the assist or be documented; done means the semantic regression is covered by the assist's tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.