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

fix: Correctly check for parentheses redundancy in remove_parentheses assist #13764

Merged
merged 2 commits into from
Dec 20, 2022

Conversation

WaffleLapkin
Copy link
Member

This is quite a bunch of code and some hacks, but I think this time it's correct.

I've added a lot of tests, most of which fail with the assist impl from #13733 :')

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 13, 2022
ast::StmtList(_) => self.needs_parens_in_stmt(None),
ast::ArgList(_) => false,
ast::MatchArm(_) => false,
_ => unimplemented!()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this should just be false? unimplmented seems wrong since other nodes don't make sense here right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, right. This was left from the time I was testing things out and haven't figured out possible parents.

fixed :)

Comment on lines +229 to +254
return contains_exterior_struct_lit_inner(self).is_some();

fn contains_exterior_struct_lit_inner(expr: &Expr) -> Option<()> {
use Expr::*;

match expr {
RecordExpr(..) => Some(()),

// X { y: 1 } + X { y: 2 }
BinExpr(e) => e
.lhs()
.as_ref()
.and_then(contains_exterior_struct_lit_inner)
.or_else(|| e.rhs().as_ref().and_then(contains_exterior_struct_lit_inner)),

// `&X { y: 1 }`, `X { y: 1 }.y`, `X { y: 1 }.bar(...)`, etc
IndexExpr(e) => contains_exterior_struct_lit_inner(&e.base()?),
AwaitExpr(e) => contains_exterior_struct_lit_inner(&e.expr()?),
PrefixExpr(e) => contains_exterior_struct_lit_inner(&e.expr()?),
CastExpr(e) => contains_exterior_struct_lit_inner(&e.expr()?),
FieldExpr(e) => contains_exterior_struct_lit_inner(&e.expr()?),
MethodCallExpr(e) => contains_exterior_struct_lit_inner(&e.receiver()?),

_ => None,
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could use a poor-mans try block aka immediately called closure

Suggested change
return contains_exterior_struct_lit_inner(self).is_some();
fn contains_exterior_struct_lit_inner(expr: &Expr) -> Option<()> {
use Expr::*;
match expr {
RecordExpr(..) => Some(()),
// X { y: 1 } + X { y: 2 }
BinExpr(e) => e
.lhs()
.as_ref()
.and_then(contains_exterior_struct_lit_inner)
.or_else(|| e.rhs().as_ref().and_then(contains_exterior_struct_lit_inner)),
// `&X { y: 1 }`, `X { y: 1 }.y`, `X { y: 1 }.bar(...)`, etc
IndexExpr(e) => contains_exterior_struct_lit_inner(&e.base()?),
AwaitExpr(e) => contains_exterior_struct_lit_inner(&e.expr()?),
PrefixExpr(e) => contains_exterior_struct_lit_inner(&e.expr()?),
CastExpr(e) => contains_exterior_struct_lit_inner(&e.expr()?),
FieldExpr(e) => contains_exterior_struct_lit_inner(&e.expr()?),
MethodCallExpr(e) => contains_exterior_struct_lit_inner(&e.receiver()?),
_ => None,
}
}
(|| {
use Expr::*;
match expr {
RecordExpr(..) => Some(()),
// X { y: 1 } + X { y: 2 }
BinExpr(e) => e
.lhs()
.as_ref()
.and_then(contains_exterior_struct_lit_inner)
.or_else(|| e.rhs().as_ref().and_then(contains_exterior_struct_lit_inner)),
// `&X { y: 1 }`, `X { y: 1 }.y`, `X { y: 1 }.bar(...)`, etc
IndexExpr(e) => contains_exterior_struct_lit_inner(&e.base()?),
AwaitExpr(e) => contains_exterior_struct_lit_inner(&e.expr()?),
PrefixExpr(e) => contains_exterior_struct_lit_inner(&e.expr()?),
CastExpr(e) => contains_exterior_struct_lit_inner(&e.expr()?),
FieldExpr(e) => contains_exterior_struct_lit_inner(&e.expr()?),
MethodCallExpr(e) => contains_exterior_struct_lit_inner(&e.receiver()?),
_ => None,
}
})().is_some()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It kind of makes the code worse, since the recursive calls return bool that needs to be converted back to Option...

    fn contains_exterior_struct_lit(&self) -> bool {
        (|| {
            use Expr::*;
            (match self {
                RecordExpr(..) => true,
                // X { y: 1 } + X { y: 2 }
                BinExpr(e) => {
                    return e
                        .lhs()
                        .as_ref()
                        .filter(|e| e.contains_exterior_struct_lit())
                        .map(drop)
                        .or_else(|| {
                            e.rhs().as_ref().filter(|e| e.contains_exterior_struct_lit()).map(drop)
                        })
                }
                // `&X { y: 1 }`, `X { y: 1 }.y`, `X { y: 1 }.bar(...)`, etc
                IndexExpr(e) => e.base()?.contains_exterior_struct_lit(),
                AwaitExpr(e) => e.expr()?.contains_exterior_struct_lit(),
                PrefixExpr(e) => e.expr()?.contains_exterior_struct_lit(),
                CastExpr(e) => e.expr()?.contains_exterior_struct_lit(),
                FieldExpr(e) => e.expr()?.contains_exterior_struct_lit(),
                MethodCallExpr(e) => e.receiver()?.contains_exterior_struct_lit(),
                _ => return None,
            })
            .then(|| ())
        })()
        .is_some()
    }

Copy link
Member

@Veykril Veykril Dec 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh thats a good point, nvm

@Veykril
Copy link
Member

Veykril commented Dec 20, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Dec 20, 2022

📌 Commit babd4c7 has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Dec 20, 2022

⌛ Testing commit babd4c7 with merge 927d56a...

@bors
Copy link
Contributor

bors commented Dec 20, 2022

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing 927d56a to master...

@bors bors merged commit 927d56a into rust-lang:master Dec 20, 2022
@WaffleLapkin WaffleLapkin deleted the badassexprs branch December 20, 2022 19:00
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