-
Notifications
You must be signed in to change notification settings - Fork 933
Use matches!() macro to improve readability #5830
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
Conversation
1. Use `matches!()` macro in `is_line_comment` and `is_block_comment` to improve readability. 2. Very sightly improve the wording of the doc comment for these two functions.
src/comment.rs
Outdated
@@ -58,25 +58,23 @@ fn custom_opener(s: &str) -> &str { | |||
} | |||
|
|||
impl<'a> CommentStyle<'a> { | |||
/// Returns `true` if the commenting style covers a line only. | |||
/// Returns `true` if the commenting style is a single line comment. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not to be a pedant but I think there's some important subtly that may be lost. Naturally it's possible to have a single-line, block style comment and my reading of the proposed changes loses that distinction
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I appreciate subtlety, so thanks for the comment.
Good point, that subtlety should be captured. The original wording also confused me in a similar fashion.
Two options:
/// Returns `true` if the commenting style cannot span multiple lines.
(added to PR as my choice)- Revert to as it is.
Happy to go either way.
matches!( | ||
self, | ||
CommentStyle::DoubleSlash | ||
| CommentStyle::TripleSlash | ||
| CommentStyle::Doc | ||
| CommentStyle::Custom(_) => true, | ||
_ => false, | ||
} | ||
| CommentStyle::TripleSlash | ||
| CommentStyle::Doc | ||
| CommentStyle::Custom(_) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have any concerns with this, though I will share for awareness that there are cases where we've intentionally not converted to matches!
because of how rustfmt currently formats them (we don't have special casing so the args are treated as standard macro args and thus get formatted as bin expressions instead of a pattern. So in some cases, including this one, switching to the macro has some negative impacts on readability as it adds rightward drift
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the tip, I agree that readability is never as simple as always use X over Y, even if X is generally regarded as the better thing. It's always case by case.
In this case, I think the removal of the false branch structure is worth the rightward drift.
matches!()
macro inis_line_comment
andis_block_comment
to improve readability.I came across these small refactoring/improvements while reading the code in pursuit of
fixing a different bug.
Note to reviewer: This change should produce no difference in functionality; it is a pure refactor.