diff --git a/crates/ide-assists/src/handlers/extract_variable.rs b/crates/ide-assists/src/handlers/extract_variable.rs index ecf9feb0e5519..71fa3ac334953 100644 --- a/crates/ide-assists/src/handlers/extract_variable.rs +++ b/crates/ide-assists/src/handlers/extract_variable.rs @@ -943,8 +943,8 @@ struct S { } fn foo(s: &mut S) { - let $0var_name = &mut s.vec; - var_name.push(0); + let $0vec = &mut s.vec; + vec.push(0); }"#, ); } @@ -979,8 +979,8 @@ struct S { } fn foo(f: &mut Y) { - let $0var_name = &mut f.field.field.vec; - var_name.push(0); + let $0vec = &mut f.field.field.vec; + vec.push(0); }"#, ); } diff --git a/crates/ide-assists/src/utils/suggest_name.rs b/crates/ide-assists/src/utils/suggest_name.rs index 5b79a7495f7de..411e5b25b7310 100644 --- a/crates/ide-assists/src/utils/suggest_name.rs +++ b/crates/ide-assists/src/utils/suggest_name.rs @@ -94,7 +94,8 @@ pub(crate) fn for_variable(expr: &ast::Expr, sema: &Semantics<'_, RootDatabase>) let mut next_expr = Some(expr.clone()); while let Some(expr) = next_expr { - let name = from_call(&expr).or_else(|| from_type(&expr, sema)); + let name = + from_call(&expr).or_else(|| from_type(&expr, sema)).or_else(|| from_field_name(&expr)); if let Some(name) = name { return name; } @@ -263,6 +264,15 @@ fn trait_name(trait_: &hir::Trait, db: &RootDatabase) -> Option { Some(name) } +fn from_field_name(expr: &ast::Expr) -> Option { + let field = match expr { + ast::Expr::FieldExpr(field) => field, + _ => return None, + }; + let ident = field.name_ref()?.ident_token()?; + normalize(ident.text()) +} + #[cfg(test)] mod tests { use ide_db::base_db::{fixture::WithFixture, FileRange}; @@ -734,4 +744,17 @@ fn foo() { $0function.name().as_ref().unwrap().to_string()$0 } "name", ); } + + #[test] + fn struct_field_name() { + check( + r#" +struct S { + some_field: T; +} +fn foo(some_struct: S) { $0some_struct.some_field$0 } +"#, + "some_field", + ); + } }