Open
Description
Describe the bug
rustfmt
does not differentiate between local imports not prefixed by self::
and external crates. The behavior is as expected when prefixed by self::
.
To Reproduce
Using group_imports = "StdExternalCrate"
in rustfmt.toml
, the following code is considered formatted.
mod foo {
struct Foo;
}
use foo::Foo;
use rand::random;
Expected behavior
rustfmt
recognizes that foo
is a local module, not an external crate.
mod foo {
struct Foo;
}
use rand::random;
use foo::Foo;
Meta
- rustfmt version: rustfmt 1.4.30-stable (8c6769d 2021-01-18)
- From where did you install rustfmt?: rustup
- How do you run rustfmt:
cargo fmt
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
jhpratt commentedon Feb 20, 2021
@rustbot modify labels to +a-imports +only-with-option
rustbot commentedon Feb 20, 2021
Error: This repository is not enabled to use triagebot.
Add a
triagebot.toml
in the root of the master branch to enable it.Please let
@rust-lang/release
know if you're having trouble with this bot.jhpratt commentedon Feb 20, 2021
Also,
proc_macro
should probably be grouped withstd
,alloc
, andcore
.MattX commentedon Feb 20, 2021
Hm, when we added this feature I thought there was no good way to differentiate between external and local imports if the latter were not explicitly prefixed, but taking a look at the code again, we might be able to use the (recently added?) file_mod_map field in the context. I'll experiment with this today.
Good point. Is there a list of standard crates somewhere? It would be nice to make sure we're not missing any others.
calebcartwright commentedon Feb 24, 2021
Removing the bug label, as there's definitely not one here though perhaps some minor updates to the documentation for the variant could be helpful to explain the limitations.
As discussed in various other issues/pulls, including the recent #4693, I'm still highly skeptical that it will be possible to do more. Would be thrilled to be proven wrong as I know folks would appreciate these types of enhancements from rustfmt, but to reiterate a few points:
jhpratt commentedon Feb 24, 2021
Of course the definition of "local" is up for debate, but under the definition currently used, wouldn't it be possible to just see if there is a
mod foo
in the file, treatingfoo::bar
as local? It wouldn't catch all cases (you could douse crate::foo;
anduse foo::bar;
, for example), but it would cover a large portion of what I consider good style.jplatte commentedon Feb 25, 2021
Also found that we had some files in
tests/
in https://github.com/ruma/ruma where the imports from the crate being tested were grouped like crate-local imports in theStdExternCrate
grouping. I wonder how others feel about that.group_imports
#508313 remaining items