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

Implement remove_nested_blocks option #6489

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

karolzwolak
Copy link
Contributor

@karolzwolak karolzwolak commented Feb 26, 2025

Implements new feature that removes nested blocks #5616.
The feature is similar to remove_nested_parens. Blocks with any sort of comments or attributes, unsafe and const blocks will not be removed. This however will not remove all nested blocks, instead it will leave the innermost block (similar to remove_nested_parens). That behavior differs from suggested in the issue, and I'm not sure which one is better.

I'm not fully satisfied with my implementation, so any suggestions are appreciated.
I also was not sure what do do if nested blocks are of different kind (nested const and/or unsafe blocks), so I decided to only remove 'naked' blocks, and just never remove const and unsafe blocks. There is some merit to removing unsafe or const blocks when only blocks of one kind are nested, but for the sake of consistency I decided to not do that. An example of such problematic situation:

// rustfmt-remove_nested_blocks: true
fn foo() {}
fn one_kind() {
    { // <- this block would be removed if not for this comment
        const { const {} }
    }
}
fn mixed() {
    unsafe {
        unsafe {
            { // <- this block would be removed if not for this comment
                const {
                    const {
                        { // this block will not be removed regardless of the comment
                            foo();
                        }
                    }
                }
            }
        }
    }
}

As I said, for now both mixed, and one_kind functions would only remove the blocks annotated by comments.
I'm open for suggestions regarding the behavior, since the feature in the original issue was very loosely defined (also blocks are more tricky than parentheses)

inner_label,
true,
context,
shape,
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'm not sure if this is correct, it does seem to work, but I'm confused what the shape is even for.

@karolzwolak karolzwolak changed the title add tests and config spec for new option remove_nested_blocks Implement remove_nested_blocks option Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants