Skip to content

prefer core-alloc-std to alloc-core-std when reordering standard libraries imports #6290

Open
@lo48576

Description

@lo48576

Summary

Currently alloc, core, and std imports are always reordered lexicographically (when they will be reordered).
However, at least when a reordered group only contains these standard library crates, it feels semantically more natural to use the order core-alloc-std (or std-alloc-core), because they have inclusion relationships (core::*alloc::*std::*).

For example, if alloc is unavailable then std is always unavailable, and if std is available then core and alloc is always available. alloc-core-std ordering looks somewhat unintuitive because it does not reflect such obvious transitive relations.

I'm not sure exact condition of when these "semantic" order should be chosen over the lexicographic order, but I strongly feel the semantic order is preferred in some situations (for example group_imports = "StdExternalCrate" is set).

Example

rustfmt.toml:

group_imports = "StdExternalCrate"

(and maybe more settings other than StdExternalCrate or group_imports as well...)

Input (random order):

extern crate alloc;

use std::io::File;
use core::num::NonZero;
use alloc::borrow::Cow;

Expected (semantic: core-alloc-std):

extern crate alloc;

use core::num::NonZero;
use alloc::borrow::Cow;
use std::io::File;

Got (lexicographic: alloc-core-std):

extern crate alloc;

use alloc::borrow::Cow;
use core::num::NonZero;
use std::io::File;

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions