-
Notifications
You must be signed in to change notification settings - Fork 899
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
Remove each comment's leading whitespace in comment::rewrite_comment
#5392
base: master
Are you sure you want to change the base?
Conversation
Fixes 5391 Previously, leading whitespace at the start of custom comments (`CommentStyle::Custom`) would lead `consume_same_line_comments` to not return the correct byte position for the end of the comment. This issue cascaded into a situation where an extra indented newline was inserted into rustfmts output causing a `"left behind trailing whitespace"` error. Removing leading whitespace helps to properly identify the end of the comment and properly rewrite the comment.
comment::rewrite_comment
Just want to elaborate a little more on the issue that we're experiencing here, and why I think this is the approach we should take. For reference here's the code for Lines 269 to 408 in 3de1a09
Taking the example from the original issue When hitting the match on line 321 we enter the first match arm because we just computed the Now lets zoom in on the for loop in Lines 305 to 317 in 3de1a09
Now on line 364 we split the original string at 0, which gives us When we finally get to the last conditional statement at the end of In the case where the comment is the last item in a block this additional indented newline conflicts with the indented newline added on line 314 of Lines 311 to 323 in 2c8b3be
I suspect something similar happens in the case where the comment isn't the last item in a block, but didn't look too deeply into it since removing leading whitespace up front resolves both cases. In conclusion, by trimming the comment up front we save ourselves from doing additional work that would lead us to recursively call |
Confirmed this PR works for me on Gentoo with rust 1.78.0 AND with locally built diff --git a/src/main.rs b/src/main.rs
index 316bc87..65dfb1a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,7 @@
mod my_error_things {
//const CUSTOM_ERROR_MSG_BUFFER_SIZE: usize = 6;
pub const CUSTOM_ERROR_MSG_BUFFER_SIZE: usize = 4096; // one kernel page?!
- //^ must be pub due to being used in a macro!
+ //^ must be pub due to being used in a macro!
}
fn main() {
println!("Hello, world!"); Also it passes Thanks! Footnotes
|
Fixes #5391
Previously, leading whitespace at the start of custom comments (
CommentStyle::Custom
) would leadconsume_same_line_comments
to not return the correct byte position for the end of the comment.This issue cascaded into a situation where an extra indented newline was inserted into rustfmts output causing a
"left behind trailing whitespace"
error.Removing leading whitespace helps to properly identify the end of the comment and properly rewrite the comment.