Skip to content

import reordering options #2185

Closed
Closed
@nrc

Description

@nrc

I would like to make reorder_imports on by default and merge all five import ordering options into a single one (part of #1974):

reorder_extern_crates,
reorder_extern_crates_in_group,
reorder_imports,
reorder_imports_in_group,
reorder_imported_names,

(currently the third and forth are off by default)

However, we should decide on the precise behaviour and iron out any bugs (current behviour is at least a bit suprising, try changing the defaults and running on chains.rs, for example).

Tasks:

  • decide on behaviour around groups
  • decide on behaviour around comments
  • fix bugs with reording imports, comments, and doc comments.
  • turn the default for reorder_imports and reorder_imports_in_group to true, fixup tests and re-format source code (it might be easier to merge these two options before other stuff because it allows to deal with fewer cases).
  • merge the five options into reorder_imports
  • nested imports

The format RFCs only state that imports should be ordered alphabetically, but don't specify beyond that (I thought we went into some detail about grouping, but I can't find that discussion).

The grouping thing is that people often group imports using newlines:

use foo::B;
use foo::A;
use bar::D;

use alpha::B;
use alpha::A;

Respecting grouping means reordering to:

use bar::D;
use foo::A;
use foo::B;

use alpha::A;
use alpha::B;

ignoring grouping would give:

use alpha::A;
use alpha::B;
use bar::D;
use foo::A;
use foo::B;

There have also been suggestions that we should actively do some grouping - perhaps separating imports from deps from imports from std and/or imports from the current crate.

I think we should probably respect grouping by newlines, remove more that one newline and not actively group stuff.

What if there are imports lower down in the header? Should they be moved? That should probably be a separate option, but ordering should respect that. I guess at first we should not move (until the option exists).

Should comments group imports? Probably not, but maybe if the comment is more than one line? In any case, we should probably move comments as if they were attached to an import directly below, but not if there are blank lines (in which case there is a group, so we would not move imports above or below). I think this solves the comment/import ordering question.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions