Skip to content

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

Merged
merged 2 commits into from
Jul 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 cannot span multiple lines.
pub(crate) fn is_line_comment(&self) -> bool {
match *self {
matches!(
self,
CommentStyle::DoubleSlash
| CommentStyle::TripleSlash
| CommentStyle::Doc
| CommentStyle::Custom(_) => true,
_ => false,
}
| CommentStyle::TripleSlash
| CommentStyle::Doc
| CommentStyle::Custom(_)
)
Comment on lines +63 to +69
Copy link
Member

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

Copy link
Contributor Author

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.

}

/// Returns `true` if the commenting style can span over multiple lines.
/// Returns `true` if the commenting style can span multiple lines.
pub(crate) fn is_block_comment(&self) -> bool {
match *self {
CommentStyle::SingleBullet | CommentStyle::DoubleBullet | CommentStyle::Exclamation => {
true
}
_ => false,
}
matches!(
self,
CommentStyle::SingleBullet | CommentStyle::DoubleBullet | CommentStyle::Exclamation
)
}

/// Returns `true` if the commenting style is for documentation.
Expand Down