rust-lang/rustfmt

Exclude block-style comments in wrap_str/filter_normal_code check

Open

#4,668 建立於 2021年1月24日

在 GitHub 查看
 (6 留言) (0 反應) (0 負責人)Rust (4,893 star) (760 fork)batch import
1x-backport:pendingA-commentsE-help-wantedP-mediumgood first issue

描述

There's a utility function, wrap_str, used in various locations throughout the rustfmt codebase which does some checks against a string of formatted code, including whether each line in the string will fit within the width constraints.

The width check is supposed to exclude comments, and does so successfully for line-style comments (e.g. // foo bar). However, there is an issue where it does not exclude block-style comments (e.g. /* foo bar */).

The code that handles sorting out comments can be found in the comments.rs file: https://github.com/rust-lang/rustfmt/blob/79c36960af85fa52ae4dc60ec1f83ec7f9b2733f/src/formatting/comment.rs#L1613-L1629

and this would need to be extended to exclude block-style comments as well. Remember that multi-line block style comments are a possibility, so that needs to be accounted for as well, though happy to accept improvements that cover single-line block style comments.

The changes should include tests, for example additional unit tests like the existing ones for filter_normal_code: https://github.com/rust-lang/rustfmt/blob/79c36960af85fa52ae4dc60ec1f83ec7f9b2733f/src/formatting/comment.rs#L1984-L1999

Additionally, it would be beneficial to include the standard system/idempotence tests with an input file (tests/source/...) that contains snippets that result in the above code paths getting hit, such as a chain that contains a block-style comment that exceeds max_width, along with a target/output file (tests/target/...) that shows the resultant formatting.

貢獻者指南

Exclude block-style comments in wrap_str/filter_normal_code check · rust-lang/rustfmt#4668 | Good First Issue