rust-lang / rust-lang/rustfmt

Should group_imports apply to function bodies?

Open
#6,704 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-discussion UO-group_imports
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

In Rust, you can write imports inside of function bodies. This is usually used for one or a small few imports that are relevant to that function. There is a clear distinction between these two kinds of places in the Rust AST (items vs. statements) so I think it may be reasonable to have different rules in how they are handled.

My concern is that, since the number of imports inside of function bodes is usually small, group_imports will often result in separating two or three imports with blank lines, and this will feel noisy.

(Contrived) example:

fn my_function(thing: Bar) {
    use std::collections::hash_map::Entry::*;

    use foo::Bar::*;

    match thing {
        Sally(..) => ..,
        Sammy(..) => ..,

vs.

fn my_function(thing: Bar) {
    use std::collections::hash_map::Entry::*;
    use foo::Bar::*;

    match thing {
        Sally(..) => ..,
        Sammy(..) => ..,

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 group_imports implementation and existing tests for imports in function bodies. Compare the issue's two examples and add coverage for the desired behavior; done means the behavior is captured by tests and group_imports no longer produces noisy separation there.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 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.