rust-lang / rust-lang/rust-analyzer
Extract function: Sort arguments by order of declaration, instead of usage?
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.9k
- Forks
- 2.2k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 72
Description
Consider this function:
fn foo(big_picture: Vec<i32>, middle_picture: usize) -> bool {
let small_picture = 42;
small_picture + big_picture[middle_picture] > 0
}
Today (Release 2021-10-18), extracting the second line as a function yields the following code, with arguments of the extracted function sorted in order of usage:
fn foo(big_picture: Vec<i32>, middle_picture: usize) -> bool {
let small_picture = 42;
bar(small_picture, big_picture, middle_picture)
}
fn bar(small_picture: i32, big_picture: Vec<i32>, middle_picture: usize) -> bool {
small_picture + big_picture[middle_picture] > 0
}
However, it could be argued that ordering by declaration, i.e. fn bar(big_picture: Vec<i32>, middle_picture: usize, small_picture: i32) -> bool, can be more logical here. This is indeed a contrived example, but there are cases where the effect is more pronounced, like when extracting functions from multiple match arms that share the same environment.
Is there a reason why this isn't the way it's done today, or are there any scenarios where this can be harmful?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating rust-analyzer's extract-function implementation and its existing tests, then inspect how captured arguments are collected and ordered. Compare declaration order with usage order across the example and shared match-arm environments. Done means the desired ordering is specified, implemented if appropriate, and covered by tests for the relevant cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100