rust-lang / rust-lang/rustfmt

Assignment LHS to RHS Comments handling when rewrite_assign_rhs is used

Open
#4,667 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-comments I-poor-formatting P-medium
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

The places where rewrite_assign_rhsh()/_with() are used, and the comments between lhs and rhs are not handled, were identified in #4626. I submitted PRs to use rewrite_assign_rhs_with_comments() for the rewrite_assignment and format_trait cases.

For the following cases, it is not clear if and what should be done. There seem to be three options:

  1. Do nothing, as it is not expected that comments will be added between the lhs (... =) and rhs in these cases.
  2. Enhance the code to use rewrite_assign_rhs_with_comments instead of rewrite_assign_rhs....
  3. Only check whether all comments are included in the formatted code and if not use the original code (using recover_comment_removed()).

Following are the relevant cases with code examples when I was able to find an example:

  1. format_variant()
enum Foo {
    Bar,
    Baz = /* Block comment */ 123,
    Quux = // Line comment
	124,
}
  1. format_trait_alias()
#![feature(trait_alias)]
trait Foo =/*comment*/std::fmt::Debug + Send;
trait Bar =/*comment*/Foo + Sync;
  1. rewrite_type()
type Kilometers =/*comment*/i32;
  1. rewrite_struct_field()
    Could not find an example. Per this code requires new line inside of field type.

  2. rewrite_opaque_impl_type()
    Could not find an example.

  3. Static ast::ForeignItem
    Could not find an example.

  4. Macro format_lazy_static()
    Maybe this case is not relevant currently, as format_lazy_static() is not called if the lazy_static! macro contains a comment - see here.

  5. ast::WherePredicate::BoundPredicate
    Could not find an example.

  6. ast::WherePredicate::EqPredicate
    Could not find an example.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the listed rewrite_assign_rhs call sites in src/formatting/items.rs, src/formatting/macros.rs, and src/formatting/types.rs, along with PR #4626. Run rustfmt on the provided examples and investigate the cases without examples. Done means each relevant case has an agreed handling strategy and comments are preserved or safely recovered.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools, tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.