rust-lang / rust-lang/rust-analyzer

Extract function: Sort arguments by order of declaration, instead of usage?

Open
#10,590 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics C-enhancement
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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.