Skip to content
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

fix to extract comments to stop internal error #3857

Merged
merged 6 commits into from
Oct 27, 2019

Conversation

rchaser53
Copy link
Contributor

fix: #3851

@@ -1892,6 +1904,7 @@ fn choose_rhs<R: Rewrite>(
shape: Shape,
orig_rhs: Option<String>,
rhs_tactics: RhsTactics,
has_rhs_comment: bool,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps there is a better way than adding a flag. But I can't think of a good idea ...

Copy link
Contributor

Choose a reason for hiding this comment

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

What about inspecting the content of orig_rhs and see if it starts with the comment? Something like the following:

let has_rhs_comment = orig_rhs.map_or(false, |s| starts_with_comment(s.trim_left()));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried your proposal, but it's failed. The orig_rhs doesn't have the comments. The lhs has the comment and indent insteadof orig_rhs. below is an example.

fn main() {
    let after =
    // after_comment
    if true {
        1.0
    };
}
// &lhs = "let after =\n    // after_comment\n    "
// &orig_rhs = Some("if true {\n        1.0\n    }",)

lhs is this

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, thank you for your explanation. In that case, is it possible to use the lhs to detect comments instead? Something like the following:

let has_rhs_comment = !lhs.trim_right().ends_with_comment();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Absolutely. It's a good idea. I fixed it.

@rchaser53
Copy link
Contributor Author

I reverted the commit.

@rchaser53 rchaser53 force-pushed the issue-3851 branch 2 times, most recently from 369eefe to b57ded9 Compare October 26, 2019 14:10
 to remove has_rhs_comment
@topecongiro topecongiro merged commit 031e029 into rust-lang:master Oct 27, 2019
@rchaser53 rchaser53 deleted the issue-3851 branch October 27, 2019 10:36
ytmimi pushed a commit to ytmimi/rustfmt that referenced this pull request Mar 28, 2022
@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
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.

rustfmt fails when encountering comment (+ trailing whitespace)
4 participants