Skip to content

Commit fd1f2e3

Browse files
authored
Merge pull request rust-lang#19249 from Veykril/push-noosrywrsuvn
Fix prefix adjustment hints unnecessarily introducing parens
2 parents bd8b58d + 637dbe5 commit fd1f2e3

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

src/tools/rust-analyzer/crates/ide/src/inlay_hints/adjustment.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ fn needs_parens_for_adjustment_hints(expr: &ast::Expr, postfix: bool) -> (bool,
260260
if postfix {
261261
// postfix ops have higher precedence than any other operator, so we need to wrap
262262
// any inner expression that is below (except for jumps if they don't have a value)
263-
let needs_inner_parens = prec < ExprPrecedence::Unambiguous && {
263+
let needs_inner_parens = prec < ExprPrecedence::Postfix && {
264264
prec != ExprPrecedence::Jump || !expr.is_ret_like_with_no_value()
265265
};
266266
// 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,
276276
// if we are already wrapped, great, no need to wrap again
277277
.filter(|it| !matches!(it, ast::Expr::ParenExpr(_)))
278278
.map(|it| it.precedence());
279+
279280
// if we have no parent, we don't need outer parens to disambiguate
280281
// otherwise anything with higher precedence than what we insert needs to wrap us
281-
let needs_outer_parens = parent.is_some_and(|prec| prec > ExprPrecedence::Prefix);
282+
// that means only postfix ops
283+
let needs_outer_parens =
284+
parent.is_some_and(|parent_prec| parent_prec == ExprPrecedence::Postfix);
282285
(needs_outer_parens, needs_inner_parens)
283286
}
284287
}
@@ -291,7 +294,7 @@ mod tests {
291294
};
292295

293296
#[test]
294-
fn adjustment_hints() {
297+
fn adjustment_hints_prefix() {
295298
check_with_config(
296299
InlayHintsConfig { adjustment_hints: AdjustmentHints::Always, ..DISABLED_CONFIG },
297300
r#"
@@ -381,6 +384,8 @@ fn main() {
381384
&mut Struct[0];
382385
//^^^^^^(&mut $
383386
//^^^^^^)
387+
let _: (&mut (),) = (&mut (),);
388+
//^^^^^^^&mut *
384389
}
385390
386391
#[derive(Copy, Clone)]
@@ -472,6 +477,9 @@ fn main() {
472477
//^^^^^^.&
473478
&mut Struct[0];
474479
//^^^^^^.&mut
480+
let _: (&mut (),) = (&mut (),);
481+
//^^^^^^^(
482+
//^^^^^^^).*.&mut
475483
}
476484
477485
#[derive(Copy, Clone)]

src/tools/rust-analyzer/crates/syntax/src/ast/prec.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ pub enum ExprPrecedence {
3535
Cast,
3636
// unary - * ! & &mut
3737
Prefix,
38-
// paths, loops, function calls, array indexing, field expressions, method calls
38+
// function calls, array indexing, field expressions, method calls
39+
Postfix,
40+
// paths, loops,
3941
Unambiguous,
4042
}
4143

@@ -57,6 +59,7 @@ pub fn precedence(expr: &ast::Expr) -> ExprPrecedence {
5759
},
5860

5961
Expr::BreakExpr(_)
62+
| Expr::BecomeExpr(_)
6063
| Expr::ContinueExpr(_)
6164
| Expr::ReturnExpr(_)
6265
| Expr::YeetExpr(_)
@@ -89,27 +92,27 @@ pub fn precedence(expr: &ast::Expr) -> ExprPrecedence {
8992

9093
Expr::LetExpr(_) | Expr::PrefixExpr(_) | Expr::RefExpr(_) => ExprPrecedence::Prefix,
9194

95+
Expr::AwaitExpr(_)
96+
| Expr::CallExpr(_)
97+
| Expr::FieldExpr(_)
98+
| Expr::IndexExpr(_)
99+
| Expr::MethodCallExpr(_)
100+
| Expr::TryExpr(_) => ExprPrecedence::Postfix,
101+
92102
Expr::ArrayExpr(_)
93103
| Expr::AsmExpr(_)
94-
| Expr::AwaitExpr(_)
95-
| Expr::BecomeExpr(_)
96104
| Expr::BlockExpr(_)
97-
| Expr::CallExpr(_)
98-
| Expr::FieldExpr(_)
99105
| Expr::ForExpr(_)
100106
| Expr::FormatArgsExpr(_)
101107
| Expr::IfExpr(_)
102-
| Expr::IndexExpr(_)
103108
| Expr::Literal(_)
104109
| Expr::LoopExpr(_)
105110
| Expr::MacroExpr(_)
106111
| Expr::MatchExpr(_)
107-
| Expr::MethodCallExpr(_)
108112
| Expr::OffsetOfExpr(_)
109113
| Expr::ParenExpr(_)
110114
| Expr::PathExpr(_)
111115
| Expr::RecordExpr(_)
112-
| Expr::TryExpr(_)
113116
| Expr::TupleExpr(_)
114117
| Expr::UnderscoreExpr(_)
115118
| Expr::WhileExpr(_) => ExprPrecedence::Unambiguous,

0 commit comments

Comments
 (0)