-
Notifications
You must be signed in to change notification settings - Fork 898
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
ssue #2896 - trim extra whitespaces with comment at end of line #4391
Conversation
… comment moved to new line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is worth adding test cases for the issue as well.
@@ -1604,7 +1604,15 @@ pub(crate) fn recover_comment_removed( | |||
let snippet = context.snippet(span); | |||
let includes_comment = contains_comment(snippet); | |||
if snippet != new && includes_comment && changed_comment_content(snippet, &new) { | |||
Some(snippet.to_owned()) | |||
/* Trim white spaces at end of lines */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/* Trim white spaces at end of lines */ | |
/* Trim whitespace at end of lines */ |
/* Trim white spaces at end of lines */ | ||
let mut c = String::from(""); | ||
for line in snippet.to_owned().split('\n') { | ||
if c != "" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if c != "" { | |
if !c.is_empty() { |
}; | ||
c.push_str(line.trim_end()); | ||
} | ||
Some(c.to_owned()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c is already a String, is to_owned needed?
Relevant test cases were added to file ".../tests/source/issue-2896.rs" |
Thank you for the PR @davidBar-On, though I'm not yet convinced this is the best approach. In cases like these, rustfmt is failing to format and simply bailing since it's detected there's a comment that was lost, and emitting the original formatting. Since the original formatting had the trailing space, that is then triggering the warning/error. Instead of trying to address a symptom of the failed formatting as proposed with the changes in this PR, we'd ideally be able to find the root cause and address there. Specifically, the problem here is the formatting of the cast expression where comments between the subexpression and the type are getting dropped. You can see this reproduced with a simpler example like the below: fn main() {
let x = 0f64 /* x */ as i32;
} There's a very similar issue with formatting of binary expressions that have comments between the operator and operands: fn main() {
let x = 2 /* x */ * /* y */ 4;
} The formatting logic for both of these expression kinds leverage rustfmt/src/formatting/pairs.rs Lines 162 to 170 in f7ba2db
rustfmt/src/formatting/expr.rs Lines 53 to 58 in f7ba2db
|
@calebcartwright, the issue handled by the PR is when a comment is moved to a new line and whitespace is left at the end of first line, not to a missing comment. Can't this case happen even if all other comments related issues are solved, e.g. when the comment is to long to remain in the line? If it can, then this PR may be of value. Regarding the missing comment examples you gave. For the first I tried adding to Is the above approach for changes to |
Sorry, but that's not what is happening with the snippets in the PR description nor the referenced issue. As I detailed above and in Discord, the problem is the failed formatting of the cast expression. rustfmt tries really hard to not be destructive and remove code, so when it detects that the updated formatting would be destructive, it bails and revers to the original snippet. rustfmt failed to format the cast expression in this case because rustfmt detected that it was going to lose the inner comments, so reverts back to the original snippet for the cast expr. Because the original snippets all had trailing spaces (if you look closely you'll see them), that then triggers the format failed warnings.
Nope. Trailing whitespace will be trimmed even on long comments in cases where rustfmt formats successfully. The proposed changes here are would only obfuscate cases where rustfmt failed to format. We do not want to have rustfmt fail to format an expression, have to revert to the original formatting, and then try to retroactively process and tweak that formatting.
Sorry but it's not entirely clear to me what the approach/thinking is. We definitely do not want to try to calculate spans within Please also note that, at least in the case of a cast expression, there are two distinct "between" spans; not just one between the pair. There's the span between the subexpression and let x = 1 /* foo */ as i32;
let x = 1 /* foo */ as /* bar */ i32;
let x = 1 /* foo */as/* bar */ i32; I see two potential approaches to address this:
|
@calebcartwright thanks for the detailed response. I hope it will prove useful and not a waste of your time. Regarding the potential approaches I would like to continue evaluating the second (which also seem easier with my current understanding of the code and is also somewhat is the direction I thought to take)):
For Another similar issue is that |
That is correct. There's a lot of utility functions within rustfmt for dealing with spans, comments, and more, and you'd want to utilize those here. I believe something like the below would do the trick: let before_span = mk_sp(
subexpr.span.hi(),
context.snippet_provider.span_before(expr.span, "as"),
);
let after_span = mk_sp(
context.snippet_provider.span_after(expr.span, "as"),
ty.span.lo(),
);
That's why we have the util functions 😄 This is already handled correctly with the util functions I showed above.
The AST is defined by rustc, not rustfmt, and keywords like
I want to reiterate my suggestion to focus on one issue at a time. PRs should be as small and targeted as possible. This makes them easier to review and increases the odds of being merged. Please don't try to address multiple, separate things within a single PR. I'd really encourage you to focus on the cast expressions, and only the cast expressions, and get that working first before moving on to others. |
Should resolve issue #2896 when trailing whitespaces remain because a comment at the end of the line was moved to a new line.
Example resolved: