cargo fmt -- -v
Formatting src/app.rs
Formatting src/central_panel.rs
Formatting src/lib.rs
Spent 0.001 secs in the parsing phase, and 0.005 secs in the formatting phase
Formatting src/main.rs
Spent 0.000 secs in the parsing phase, and 0.000 secs in the formatting phase
But I have other file like "src/central_panel.rs" which contains non formatted code
When I run manually rustfmt
rustfmt src/central_panel.rs -v
Formatting src/central_panel.rs
Spent 0.001 secs in the parsing phase, and 0.004 secs in the formatting phase
When I add a big trailing space in central_panel.rs I have
error[internal]: left behind trailing whitespace
--> src/central_panel.rs:181:181:1
|
181 |
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
warning: rustfmt has failed to format. See previous 1 errors.
rustfmt will silently skip formatting a section of code if it fails to find a way to format it that is under the maximum line length. The most common culprit is long string literals, but deeply nested code can also be a problem.
Please show us the contents of central_panel.rs so we can evaluate if that is the case.
I can't help you diagnose the problem without seeing the exact text of the file. But it sounds like you found it by shortening it.
Do you mean, why did it not show an error? I am not familiar with rustfmt’s original design, but I think the idea is that, in cases that it cannot satisfy its formatting rules, it tries to leave formatting of the problematic code to you and format the remainder, so you get partial formatting help rather than no formatting help at all.
I do agree that it would be nice if it showed at least a warning about the line length — but someone would have to put in the work to do that. rustfmt doesn’t have very many people working on it, and it’s a hard problem to improve it without breaking any of its prior formatting stability guarantees. Help is wanted.
rustfmt will never modify the code you wrote in such a way that the AST is different. Every change will only be of the non-semantic formatting (effectively, whitespace). This means that if you have to extract an fn argument to be a binding, rustfmt is not the tool that would do that for you.
I'm just saying that, by making my fn as a closure variable, the rustfmt is suddenly working
-> Why ?
Okay thanks, that's also a working option
Seems still super weird to me to do that (since this will format part of my code with this new width)
I tried to have a MRE and I think it's because of a string
resp.on_hover_text(format!(
"You cannot use this button right now because the char is {ascii_char} and this char is not allowed"
));
And my string is ending at col: 109
After checking, if I have size_of_line > max_width rustfmt just quit (with no error reported🙁 )
New questions:
Is there a setting in rustfmt to tell be verbose and the user "I don't work with line width more than X" ? (the default verbose -v don't do that)
Is there a setting in rustfmt more like allow_max_width to allow rustfmt to format/accept more than X max_width but still format with the default max_width
You can break such a string into multiple lines like this:
resp.on_hover_text(format!(
"You cannot use this button right now because the char is \
{ascii_char} and this char is not allowed"
));
\ at the end of a line eats the newline and all following whitespace, so this literal has the same contents. (The built-in macro concat!() is also an option, if you prefer.)