Skip to content

Indentation of assignment RHS after one-line comment #4502

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

Closed
davidBar-On opened this issue Oct 29, 2020 · 5 comments · Fixed by #4550
Closed

Indentation of assignment RHS after one-line comment #4502

davidBar-On opened this issue Oct 29, 2020 · 5 comments · Fixed by #4550
Assignees

Comments

@davidBar-On
Copy link
Contributor

Output
Current expected formatting output from tests/target/issue-3851.rs

    let after =
    // after_comment
    if true {
        1.0
    };

Expected output
To be compatible with other formatting, the desired output may be with the if statement indented to the right:

    let after =
    // after_comment
        if true {
            1.0
        };

For example, following is current formatting output of similar expression:

    let aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =
        if cccccccccccccccccccccccccccccccccccccccccccccccccc {
            1.0
        };
@calebcartwright
Copy link
Member

calebcartwright commented Nov 1, 2020

Generally agreed, though note that the comment should be correctly indented as well:

    let after =
        // after_comment
        if true {
            1.0
        };

@davidBar-On
Copy link
Contributor Author

davidBar-On commented Nov 23, 2020

Following is another related test case (from #4518) when using rustfmt 2.0.0-rc.2-nightly (30bda450 2020-11-18). Note the extra space before the 777:

fn main() {
    let x =
       // Comment
       777;
}

is formatted as:

fn main() {
    let x =
    // Comment
     777;
}

@calebcartwright
Copy link
Member

is formatted as:

Is that a description current behavior, or a suggested expected/target formatted output? If the latter, is the indentation shown above correct? Here's how that's showing in the GH UI

image

@davidBar-On
Copy link
Contributor Author

This is the current format output. An extra space is added before the 777. The two problems described in this issue are supposed to be fixed by PR #4550. As I noted in the PR, I through that these two issues (indentation after one-line comment and extra space in the following line) are related, but it turned out that they are not. However, since the fix for each is simple (assuming what I did is correct), I submitted both fixes in one PR.

@calebcartwright
Copy link
Member

Gotcha, thanks for the clarification

calebcartwright pushed a commit that referenced this issue Dec 14, 2020
* Fix #4502 issue - assignment indentation after one line comment

* Enhancement by using rewrite_assign_rhs_with_comments()

* Changes per comments received
ytmimi pushed a commit to ytmimi/rustfmt that referenced this issue 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 pushed a commit that referenced this issue 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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants