-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Only treat plain literal patterns as short #135251
base: master
Are you sure you want to change the base?
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
r? @ytmimi |
src/tools/rustfmt/src/patterns.rs
Outdated
ast::ExprKind::ConstBlock(_) => false, | ||
ast::ExprKind::Path(..) => false, |
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.
As you mentioned in #134228 (comment), ConstBlock
and Path
patterns snuck into rustfmt unexpectedly. Let me know if I'm misunderstanding anything, but assuming that's the case then we'll need to gate these changes on the style edition:
Accessing the style_edition requires us to add some additional context to is_short_pattern
and is_short_pattern_inner
. We can either pass the entire &RewriteContext<'_>
, or just the &Config
(context.config). The &RewriteContext<'_>
is available at the call site so this shouldn't be too difficult.
/// - `[small, ntp]`
/// - unary tuple constructor `([small, ntp])`
/// - `&[small]`
-pub(crate) fn is_short_pattern(pat: &ast::Pat, pat_str: &str) -> bool {
+pub(crate) fn is_short_pattern(context: &RewriteContext<'_>, pat: &ast::Pat, pat_str: &str) -> bool {
// We also require that the pattern is reasonably 'small' with its literal width.
- pat_str.len() <= 20 && !pat_str.contains('\n') && is_short_pattern_inner(pat)
+ pat_str.len() <= 20 && !pat_str.contains('\n') && is_short_pattern_inner(context, pat)
}
-fn is_short_pattern_inner(pat: &ast::Pat) -> bool {
+fn is_short_pattern_inner(context: &RewriteContext<'_>, pat: &ast::Pat) -> bool {
Then we can gate the change:
ast::ExprKind::ConstBlock(_) => false, | |
ast::ExprKind::Path(..) => false, | |
ast::ExprKind::ConstBlock(_) | ast::ExprKind::Path(..) => { | |
// Let's leave a comment explaining why this needs to be gated | |
context.config.style_edition() <= StyleEdition::Edition2024 | |
} |
Yeah, I think this will be tougher to test. Especially because I think we need to gate this change on the style edition as to not change any behavior with stable formatting. Assuming we don't change any behavior for |
a8e00b0
to
be92ac3
Compare
See #134228 (comment) and #134228 (comment) for context. We never wanted to treat const blocks and paths as short, only plain literals.
I don't know how to write a test for this, it.s not clear to me how the short pattern check actually affects the formatting