Skip to content

Rustfmt fails to format code with a long string inside struct #5986

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
sterliakov opened this issue Dec 22, 2023 · 1 comment
Closed

Rustfmt fails to format code with a long string inside struct #5986

sterliakov opened this issue Dec 22, 2023 · 1 comment
Labels
a-strings String literals e-max width error[internal]: line formatted, but exceeded maximum width

Comments

@sterliakov
Copy link

This terrible input (reduced from some larger struct with 2 levels of nesting) is left as-is.

fn test_fetch_existing_receipt() {
    let expected =
    Event {
                            topics: 1,
                            data: Bytes::copy_from_slice(&b"vtho-usd\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x84\x17\x19\x1a\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0e\x7f\x8eJ"[..])
                        };
}

Applies to both current nightly and stable:

$ cargo fmt -- -V
rustfmt 1.6.0-stable (a28077b 2023-12-04)
$ cargo +nightly fmt -- -V
rustfmt 1.7.0-nightly (3f28fe1 2023-12-18)

I wanted to just split a struct copied from terminal output, but ended up doing it manually:(

@ytmimi
Copy link
Contributor

ytmimi commented Dec 22, 2023

Thanks for reaching out.

Originally I thought this was a duplicate of #4992 #4800. Both of those issues are fixed when using version=Two.
Unfortunately the call to Bytes::copy_from_slice is what makes this a different issues. Because the string literal is way longer than the default of max_width rustfmt fails to format Bytes::copy_from_slice, which prevents rustfmt from formatting the Event struct.

I think your best option here is to refactor your code:

Option 3:

input

fn test_fetch_existing_receipt() {
    let data = Bytes::copy_from_slice(&b"vtho-usd\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x84\x17\x19\x1a\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0e\x7f\x8eJ"[..]);
    let expected = 
    Event {
                            topics: 1,
                            data
                        };
}

Output

fn test_fetch_existing_receipt() {
    let data = Bytes::copy_from_slice(&b"vtho-usd\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x84\x17\x19\x1a\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0e\x7f\x8eJ"[..]);
    let expected = Event { topics: 1, data };
}

@ytmimi ytmimi closed this as not planned Won't fix, can't repro, duplicate, stale Dec 22, 2023
@ytmimi ytmimi added a-strings String literals e-max width error[internal]: line formatted, but exceeded maximum width labels Dec 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-strings String literals e-max width error[internal]: line formatted, but exceeded maximum width
Projects
None yet
Development

No branches or pull requests

2 participants