Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain newlines when reordering imports #3814

Open
ghost opened this issue Sep 27, 2019 · 2 comments
Open

Retain newlines when reordering imports #3814

ghost opened this issue Sep 27, 2019 · 2 comments

Comments

@ghost
Copy link

ghost commented Sep 27, 2019

cargo-fmt did this:

diff --git a/async-await-echo/src/main.rs b/async-await-echo/src/main.rs
index 1531b22..a654e92 100644
--- a/async-await-echo/src/main.rs
+++ b/async-await-echo/src/main.rs
@@ -1,16 +1,4 @@
 use {
-    hyper::{
-        // Miscellaneous types from Hyper for working with HTTP.
-        Body, Client, Request, Response, Server, Uri,
-
-        // This function turns a closure which returns a future into an
-        // implementation of the the Hyper `Service` trait, which is an
-        // asynchronous function from a generic `Request` to a `Response`.
-        service::service_fn,
-
-        // A function which runs a future to completion using the Hyper runtime.
-        rt::run,
-    },
     futures::{
         // Extension trait for futures 0.1 futures, adding the `.compat()` method
         // which allows us to use `.await` on 0.1 futures.
@@ -20,6 +8,22 @@ use {
         // `TryFutureExt` adds methods to futures that return `Result` types.
         future::{FutureExt, TryFutureExt},
     },
+    hyper::{
+        // A function which runs a future to completion using the Hyper runtime.
+        rt::run,
+        // This function turns a closure which returns a future into an
+        // implementation of the the Hyper `Service` trait, which is an
+        // asynchronous function from a generic `Request` to a `Response`.
+        service::service_fn,
+
+        // Miscellaneous types from Hyper for working with HTTP.
+        Body,
+        Client,
+        Request,
+        Response,
+        Server,
+        Uri,
+    },
     std::net::SocketAddr,
 };
 

that rt::run, line could use an extra new line, me thinks.

rustfmt 1.4.8-nightly (6b0a447 2019-09-20)

code is from: https://rust-lang.github.io/async-book/01_getting_started/05_http_server_example.html

Thanks.

PS: someone could probably title this better to describe this case?

@ayazhafiz
Copy link
Contributor

I think this should require a configuration option, because it would be very hard to do in an expected manner with reordered imports. For example

use {
  // c and d
  C, D,
  
  // a and b
  A, B,
}

Now it's not so clear if this should be reformatted with newlines or not (the "shape" of the original import statement could be used, but I think this would lead to poor formatting for imports that are not already in an almost-formatted state).

Thoughts?

@ytmimi
Copy link
Contributor

ytmimi commented Jul 21, 2022

Confirming I can reproduce this with rustfmt 1.5.1-nightly (f2c31ba0 2022-07-19). The issue is that reorder_imports=true is the default and it would seem that we don't retain all whitespace when reordering.

Input

 use {
    hyper::{
        // Miscellaneous types from Hyper for working with HTTP.
        Body, Client, Request, Response, Server, Uri,

        // This function turns a closure which returns a future into an
        // implementation of the the Hyper `Service` trait, which is an
        // asynchronous function from a generic `Request` to a `Response`.
        service::service_fn,

        // A function which runs a future to completion using the Hyper runtime.
        rt::run,
    }
};

Output with reorder_imports=true (the default)

use hyper::{
    // A function which runs a future to completion using the Hyper runtime.
    rt::run,
    // This function turns a closure which returns a future into an
    // implementation of the the Hyper `Service` trait, which is an
    // asynchronous function from a generic `Request` to a `Response`.
    service::service_fn,

    // Miscellaneous types from Hyper for working with HTTP.
    Body,
    Client,
    Request,
    Response,
    Server,
    Uri,
};

Output with reorder_imports=false

use {
    hyper::{
        // Miscellaneous types from Hyper for working with HTTP.
        Body,
        Client,
        Request,
        Response,
        Server,
        Uri,

        // This function turns a closure which returns a future into an
        // implementation of the the Hyper `Service` trait, which is an
        // asynchronous function from a generic `Request` to a `Response`.
        service::service_fn,

        // A function which runs a future to completion using the Hyper runtime.
        rt::run,
    },
};

@ytmimi ytmimi changed the title extra new line would be good, in this case Retain newlines when reordering imports Jul 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants