Rustfmt: Visual indentation of imports

Created on 4 Jun 2018  路  4Comments  路  Source: rust-lang/rustfmt

Hi! rustfmt from 1.26.1 gives me this diff:

-use exonum::api::{Api, ApiError};
-use exonum::blockchain::{self, BlockProof, Blockchain, Transaction, TransactionSet};
-use exonum::crypto::{Hash, PublicKey};
-use exonum::helpers::Height;
-use exonum::node::TransactionSend;
-use exonum::storage::{ListProof, MapProof};
+use exonum::{api::{Api, ApiError},
+             blockchain::{self, BlockProof, Blockchain, Transaction, TransactionSet},
+             crypto::{Hash, PublicKey},
+             helpers::Height,
+             node::TransactionSend,
+             storage::{ListProof, MapProof}};

I though rustfmt is supposed to use block indents everywhere:
https://github.com/nrc/rfcs/blob/style-guide/style-guide/items.md#imports-use-statements

I was expecting something like this:

use exonum::{
    api::{Api, ApiError},
    blockchain::{self, BlockProof, Blockchain, Transaction, TransactionSet},
    crypto::{Hash, PublicKey},
    helpers::Height,
    node::TransactionSend,
    storage::{ListProof, MapProof},
};

Is this a temporary omission or have I misunderstood something?

poor-formatting

Most helpful comment

Yeah, you are right, nightly version uses block indent, just checked it.
But it has another oddity.

https://github.com/nrc/rfcs/blob/style-guide/style-guide/items.md#nested-imports says:

If there are any nested imports in a list import, then use the multi-line form, even if the import fits on one line. Each nested import must be on its own line, but non-nested imports must be grouped on as few lines as possible.

... so I was expecting this:

use exonum::{
    api::{Api, ApiError},
    blockchain::{self, BlockProof, Blockchain, Transaction, TransactionSet},
    crypto::{Hash, PublicKey},
    helpers::Height,
    node::TransactionSend,
    storage::{ListProof, MapProof},
};

but rustfmt 0.8.2-nightly (173ae0d 2018-05-28) gives me this (multiple nested import on a line):

use exonum::{
    api::{Api, ApiError}, blockchain::{self, BlockProof, Blockchain, Transaction, TransactionSet},
    crypto::{Hash, PublicKey}, helpers::Height, node::TransactionSend,
    storage::{ListProof, MapProof},
};

Is this an expected behaviour?

All 4 comments

I believe this is fixed in nightly Rustfmt?

Yeah, you are right, nightly version uses block indent, just checked it.
But it has another oddity.

https://github.com/nrc/rfcs/blob/style-guide/style-guide/items.md#nested-imports says:

If there are any nested imports in a list import, then use the multi-line form, even if the import fits on one line. Each nested import must be on its own line, but non-nested imports must be grouped on as few lines as possible.

... so I was expecting this:

use exonum::{
    api::{Api, ApiError},
    blockchain::{self, BlockProof, Blockchain, Transaction, TransactionSet},
    crypto::{Hash, PublicKey},
    helpers::Height,
    node::TransactionSend,
    storage::{ListProof, MapProof},
};

but rustfmt 0.8.2-nightly (173ae0d 2018-05-28) gives me this (multiple nested import on a line):

use exonum::{
    api::{Api, ApiError}, blockchain::{self, BlockProof, Blockchain, Transaction, TransactionSet},
    crypto::{Hash, PublicKey}, helpers::Height, node::TransactionSend,
    storage::{ListProof, MapProof},
};

Is this an expected behaviour?

It is a formatting bug, rustfmt should put each nested import on its own line.

Wow, that was fast! Thank you, @nrc and @topecongiro :)

Was this page helpful?
0 / 5 - 0 ratings