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

Conversation

jmqd
Copy link
Contributor

@jmqd jmqd commented Jul 13, 2023

  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.

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.

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.
@jmqd jmqd changed the title Use matches!() macro to improve readability in comment Use matches!() macro to improve readability Jul 13, 2023
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.
Copy link
Member

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

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 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:

  1. /// Returns `true` if the commenting style cannot span multiple lines. (added to PR as my choice)
  2. Revert to as it is.

Happy to go either way.

Comment on lines +63 to +69
matches!(
self,
CommentStyle::DoubleSlash
| CommentStyle::TripleSlash
| CommentStyle::Doc
| CommentStyle::Custom(_) => true,
_ => false,
}
| CommentStyle::TripleSlash
| CommentStyle::Doc
| CommentStyle::Custom(_)
)
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.

@jmqd jmqd requested a review from calebcartwright July 16, 2023 14:13
@calebcartwright calebcartwright merged commit e0e633e into rust-lang:master Jul 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants