Skip to content

Assignment indentation after one line comment #4550

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

Merged
merged 4 commits into from
Dec 14, 2020

Conversation

davidBar-On
Copy link
Contributor

@davidBar-On davidBar-On commented Nov 24, 2020

Changes to fix #4502 issue - proper indentation when there is one-line comment after the =.

Actually #4502 is about two issues - indentation after one-line comment and extra space in the following line. These two issues seemed to be related but it turned out that they are not. I still submitted the fix for both in this PR, as each is relatively simple.

Note that although the expected output of #3851 test cases are modified, as discussed in #4502, no config option was added, assuming that the the previous indentation was a bug and not a feature.

@calebcartwright
Copy link
Member

Thanks for the PR! I gather that this achieves the goal of resolving the referenced issues, but it's another case where we're just trying to triage symptoms instead of the root cause of the core lhs/rhs utility functions not handling comments well.

A fairly short time ago, we introduced a new utility function rewrite_assign_rhs_with_comments that continues to support the paradigm of the lhs contents inclusively running up to the assignment operator, but actually supporting comments between the assignment operator and the start of the rhs.

The usage of that function was intentionally kept fairly minimal when it was added to keep that PR reasonable, but I think this is a great opportunity to use that "new" function here to address the root cause instead of continuing to battle side effects.

It's a preferable solution both from a code base perspective (encapsulation, reduced duplication) and from a runtime/execution perspective as it avoids first making formatting changes and then having to scan back through our own formatted result to try to figure out what to do next.

Would you be willing to try to implement this approach? I believe it should only require making a few tweaks to the rewrite function for locals to use rewrite_assign_rhs_with_comments instead of whatever rewrite-* and string concat is in there now

@davidBar-On
Copy link
Contributor Author

Made the change for using rewrite_assign_rhs_with_comments(). Seem to work o.k.

Had to create a span variable with initial value let mut comment_after_assign_span = base_span for the non = case - hope base_span is the right value but should be verified.

Copy link
Member

@calebcartwright calebcartwright left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates! Feedback left inline below

Comment on lines 2068 to 2074
// If last lhs line is only indetation then no need to add a space before rhs
let result_prefix = if trimmed_last_line_width(lhs) == 0 {
""
} else {
" "
};
Some(format!("{}{}", result_prefix, new_str))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are unneeded with the change to use rewrite_assign_rhs_with_comments, so should be reverted (this is the type of post processing we should strive to avoid)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Submitted. Reverted the changes, as all tests pass. However, note that rewrite_pat_expr() is still using choose_rhs() directly and performs format!("{}{}", lhs, rhs), so in principle the removed change could still be relevant until rewrite_pat_expr() will use rewrite_assign_rhs_with_comments()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so in principle the removed change could still be relevant until rewrite_pat_expr() will use rewrite_assign_rhs_with_comments()

Thanks. We wouldn't want to introduce something like this to the codebase where we already know it's tech debt that would need to be removed. It also wouldn't just apply to pattern expressions, but every single invocation of these functions including the others where it'd be exceptionally duplicative and unnecessary

@calebcartwright
Copy link
Member

Thanks for the updates!

@calebcartwright calebcartwright merged commit 81ad114 into rust-lang:master Dec 14, 2020
ytmimi pushed a commit to ytmimi/rustfmt that referenced this pull request Mar 28, 2022
* Fix rust-lang#4502 issue - assignment indentation after one line comment

* Enhancement by using rewrite_assign_rhs_with_comments()

* Changes per comments received
@ytmimi ytmimi added 1x-backport:pending Fixed/resolved in source but not yet backported to a 1x branch and release and removed backport-triage labels Mar 30, 2022
ytmimi pushed a commit that referenced this pull request Jul 20, 2022
* Fix #4502 issue - assignment indentation after one line comment

* Enhancement by using rewrite_assign_rhs_with_comments()

* Changes per comments received
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1x-backport:pending Fixed/resolved in source but not yet backported to a 1x branch and release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Indentation of assignment RHS after one-line comment
4 participants