diff options
| author | Lukas Wirth <lukastw97@gmail.com> | 2025-02-28 09:45:24 +0100 |
|---|---|---|
| committer | Lukas Wirth <lukastw97@gmail.com> | 2025-02-28 10:18:10 +0100 |
| commit | 637dbe56fa651c8ce1f37fb8c78c959a55742e46 (patch) | |
| tree | 01a13e664f3b32df4dcc735fd382410e44bdcfad | |
| parent | d4b92f4b682eb7d856f09a3f58adc6338b5f5e9c (diff) | |
| download | rust-637dbe56fa651c8ce1f37fb8c78c959a55742e46.tar.gz rust-637dbe56fa651c8ce1f37fb8c78c959a55742e46.zip | |
Fix prefix adjustment hints unnecessarily introducing parens
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide/src/inlay_hints/adjustment.rs | 14 | ||||
| -rw-r--r-- | src/tools/rust-analyzer/crates/syntax/src/ast/prec.rs | 19 |
2 files changed, 22 insertions, 11 deletions
diff --git a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/adjustment.rs b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/adjustment.rs index d3b95750f7e..40156ace26b 100644 --- a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/adjustment.rs +++ b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/adjustment.rs @@ -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)] diff --git a/src/tools/rust-analyzer/crates/syntax/src/ast/prec.rs b/src/tools/rust-analyzer/crates/syntax/src/ast/prec.rs index 5d33f132ac1..a7f1a3788c2 100644 --- a/src/tools/rust-analyzer/crates/syntax/src/ast/prec.rs +++ b/src/tools/rust-analyzer/crates/syntax/src/ast/prec.rs @@ -35,7 +35,9 @@ pub enum ExprPrecedence { Cast, // unary - * ! & &mut Prefix, - // paths, loops, function calls, array indexing, field expressions, method calls + // function calls, array indexing, field expressions, method calls + Postfix, + // paths, loops, Unambiguous, } @@ -57,6 +59,7 @@ pub fn precedence(expr: &ast::Expr) -> ExprPrecedence { }, Expr::BreakExpr(_) + | Expr::BecomeExpr(_) | Expr::ContinueExpr(_) | Expr::ReturnExpr(_) | Expr::YeetExpr(_) @@ -89,27 +92,27 @@ pub fn precedence(expr: &ast::Expr) -> ExprPrecedence { Expr::LetExpr(_) | Expr::PrefixExpr(_) | Expr::RefExpr(_) => ExprPrecedence::Prefix, + Expr::AwaitExpr(_) + | Expr::CallExpr(_) + | Expr::FieldExpr(_) + | Expr::IndexExpr(_) + | Expr::MethodCallExpr(_) + | Expr::TryExpr(_) => ExprPrecedence::Postfix, + Expr::ArrayExpr(_) | Expr::AsmExpr(_) - | Expr::AwaitExpr(_) - | Expr::BecomeExpr(_) | Expr::BlockExpr(_) - | Expr::CallExpr(_) - | Expr::FieldExpr(_) | Expr::ForExpr(_) | Expr::FormatArgsExpr(_) | Expr::IfExpr(_) - | Expr::IndexExpr(_) | Expr::Literal(_) | Expr::LoopExpr(_) | Expr::MacroExpr(_) | Expr::MatchExpr(_) - | Expr::MethodCallExpr(_) | Expr::OffsetOfExpr(_) | Expr::ParenExpr(_) | Expr::PathExpr(_) | Expr::RecordExpr(_) - | Expr::TryExpr(_) | Expr::TupleExpr(_) | Expr::UnderscoreExpr(_) | Expr::WhileExpr(_) => ExprPrecedence::Unambiguous, |
