Fix prefix adjustment hints unnecessarily introducing parens

This commit is contained in:
Lukas Wirth 2025-02-28 09:45:24 +01:00
parent 505b52da5f
commit f655062cb0
2 changed files with 23 additions and 12 deletions

View file

@ -260,7 +260,7 @@ fn needs_parens_for_adjustment_hints(expr: &ast::Expr, postfix: bool) -> (bool,
if postfix {
// postfix ops have higher precedence than any other operator, so we need to wrap
// any inner expression that is below (except for jumps if they don't have a value)
let needs_inner_parens = prec < ExprPrecedence::Unambiguous && {
let needs_inner_parens = prec < ExprPrecedence::Postfix && {
prec != ExprPrecedence::Jump || !expr.is_ret_like_with_no_value()
};
// given we are the higher precedence, no parent expression will have stronger requirements
@ -276,9 +276,12 @@ fn needs_parens_for_adjustment_hints(expr: &ast::Expr, postfix: bool) -> (bool,
// if we are already wrapped, great, no need to wrap again
.filter(|it| !matches!(it, ast::Expr::ParenExpr(_)))
.map(|it| it.precedence());
// if we have no parent, we don't need outer parens to disambiguate
// otherwise anything with higher precedence than what we insert needs to wrap us
let needs_outer_parens = parent.is_some_and(|prec| prec > ExprPrecedence::Prefix);
// that means only postfix ops
let needs_outer_parens =
parent.is_some_and(|parent_prec| parent_prec == ExprPrecedence::Postfix);
(needs_outer_parens, needs_inner_parens)
}
}
@ -291,7 +294,7 @@ mod tests {
};
#[test]
fn adjustment_hints() {
fn adjustment_hints_prefix() {
check_with_config(
InlayHintsConfig { adjustment_hints: AdjustmentHints::Always, ..DISABLED_CONFIG },
r#"
@ -381,6 +384,8 @@ fn main() {
&mut Struct[0];
//^^^^^^(&mut $
//^^^^^^)
let _: (&mut (),) = (&mut (),);
//^^^^^^^&mut *
}
#[derive(Copy, Clone)]
@ -472,6 +477,9 @@ fn main() {
//^^^^^^.&
&mut Struct[0];
//^^^^^^.&mut
let _: (&mut (),) = (&mut (),);
//^^^^^^^(
//^^^^^^^).*.&mut
}
#[derive(Copy, Clone)]