diff options
| author | Chayim Refael Friedman <chayimfr@gmail.com> | 2024-08-28 23:55:31 +0300 |
|---|---|---|
| committer | Chayim Refael Friedman <chayimfr@gmail.com> | 2024-08-28 23:55:31 +0300 |
| commit | 2a7ec0b0ad0b9d7aea147694c23b5f52b6dc6fa9 (patch) | |
| tree | 1a700acce7a9714b55b7e0d47bb17948100346d6 | |
| parent | 248a55723b5f4761bbce417bd64c5b79ac68d5bd (diff) | |
| download | rust-2a7ec0b0ad0b9d7aea147694c23b5f52b6dc6fa9.tar.gz rust-2a7ec0b0ad0b9d7aea147694c23b5f52b6dc6fa9.zip | |
Consider all expressions that autoderef in "Extract variable", not just method and field accesses.
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_variable.rs | 51 |
1 files changed, 48 insertions, 3 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_variable.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_variable.rs index 0ef71a38661..33cd5252813 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_variable.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_variable.rs @@ -58,9 +58,15 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op } let parent = to_extract.syntax().parent().and_then(ast::Expr::cast); - let needs_adjust = parent - .as_ref() - .map_or(false, |it| matches!(it, ast::Expr::FieldExpr(_) | ast::Expr::MethodCallExpr(_))); + // Any expression that autoderefs may need adjustment. + let needs_adjust = parent.as_ref().map_or(false, |it| match it { + ast::Expr::FieldExpr(_) + | ast::Expr::MethodCallExpr(_) + | ast::Expr::CallExpr(_) + | ast::Expr::AwaitExpr(_) => true, + ast::Expr::IndexExpr(index) if index.base().as_ref() == Some(&to_extract) => true, + _ => false, + }); let anchor = Anchor::from(&to_extract)?; let target = to_extract.syntax().text_range(); @@ -1221,6 +1227,45 @@ fn foo(s: &S) { } #[test] + fn test_extract_var_index_deref() { + check_assist( + extract_variable, + r#" +//- minicore: index +struct X; + +impl std::ops::Index<usize> for X { + type Output = i32; + fn index(&self) -> &Self::Output { 0 } +} + +struct S { + sub: X +} + +fn foo(s: &S) { + $0s.sub$0[0]; +}"#, + r#" +struct X; + +impl std::ops::Index<usize> for X { + type Output = i32; + fn index(&self) -> &Self::Output { 0 } +} + +struct S { + sub: X +} + +fn foo(s: &S) { + let $0sub = &s.sub; + sub[0]; +}"#, + ); + } + + #[test] fn test_extract_var_reference_parameter_deep_nesting() { check_assist( extract_variable, |
