Skip to content

Formatting stops working with specific code structure #5348

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

Closed
l3utterfly opened this issue May 22, 2022 · 6 comments
Closed

Formatting stops working with specific code structure #5348

l3utterfly opened this issue May 22, 2022 · 6 comments

Comments

@l3utterfly
Copy link

VS Code
rustlang.rust v0.3.1051

The following code does not format (using the format command has no effect).

Removing the println line completely seems to make the code format again.

Is this a bug within rustfmt?

use std::thread;

enum Switch1 {
    Option1,
    Option2,
}

enum MassiveSwitch {
    Cmd1,
}

struct Worker {
    id: i32,
}

impl Worker {
    pub fn start(self) {
        let switch: MassiveSwitch = MassiveSwitch::Cmd1;
        let switch1 = Switch1::Option1;

        let _ = thread::Builder::new()
            .name(format!("Worker {}", self.id))
            .spawn(move || {
                if true {
                    match switch1 {
                        Switch1::Option1 => match switch {
                            MassiveSwitch::Cmd1 => {







                                // TODO: the white lines here should be deleted by rustfmt on save (or format command)
                                // TODO: removing this line causes rustfmt to work again. Commenting it out doesn't work, have to delete the line complete to get rust fmt to work
                                println!("Shard worker {}: received shutdown signal, exiting loop...", 5);

                                let test = "test";
                            }
                        },
                        Switch1::Option2 => todo!(),
                    }
                }
            })
            .expect("Can't spawn");
    }
}

fn main() {
    let worker = Worker { id: 3 };

    worker.start();
}
@ytmimi
Copy link
Contributor

ytmimi commented May 22, 2022

Hi @l3utterfly Thanks for reaching out. If you're on nightly you can try setting version=Two or format_strings=True. I just checked and both options should allow rustmt to format the println! call vertically.

The issue is that the nested println! call exceeds the default max line width of 100. If you're on stable You can try setting the max width to some sufficiently large value like 110, and rustfmt will then be able to reformat the code.

I'm pretty sure this is a duplicate of #3416, and #3863

@ytmimi ytmimi closed this as completed May 22, 2022
@l3utterfly
Copy link
Author

Thank you, this works now

@ytmimi
Copy link
Contributor

ytmimi commented May 23, 2022

I'm glad to hear that! Out of curiosity, which approach did you take to resolve your issue?

@l3utterfly
Copy link
Author

I used both. I changed "max_width" to 200 which is about my editor screen length. But I still have some strings which are even longer than that, so had to add the "format_strings" flag as well.

@l3utterfly
Copy link
Author

l3utterfly commented May 23, 2022

Incidentally, I needed to set version=Two AND format_strings=True to get it to work

@ytmimi
Copy link
Contributor

ytmimi commented May 23, 2022

Got it! I'm glad that we were able to find a solution for your issue!

@ytmimi ytmimi closed this as not planned Won't fix, can't repro, duplicate, stale Jun 28, 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

2 participants