Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Jan 8, 2025

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

@rustbot

This comment was marked as resolved.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 8, 2025
@rustbot

This comment was marked as resolved.

@oli-obk
Copy link
Contributor Author

oli-obk commented Jan 8, 2025

r? @ytmimi

@rustbot rustbot assigned ytmimi and unassigned Mark-Simulacrum Jan 8, 2025
Comment on lines 50 to 51
ast::ExprKind::ConstBlock(_) => false,
ast::ExprKind::Path(..) => false,
Copy link
Contributor

@ytmimi ytmimi Jan 8, 2025

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:

Suggested 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
}

@ytmimi
Copy link
Contributor

ytmimi commented Jan 8, 2025

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

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 style editions <= 2024, then I think we should be good.

@oli-obk oli-obk force-pushed the push-lmpyvvyrtplk branch from a8e00b0 to be92ac3 Compare January 9, 2025 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants