From e5a6cf815372150ad40dee995b7b89f29e701427 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 6 Jan 2019 00:33:58 +0100 Subject: Various small code review improvements --- crates/ra_hir/src/expr.rs | 31 ++++++++++++++++++------------- crates/ra_hir/src/function/scope.rs | 12 +++++------- crates/ra_hir/src/source_binder.rs | 10 ++++------ 3 files changed, 27 insertions(+), 26 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 5cf0f5e3f..6866fc2ac 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -1,3 +1,4 @@ +use std::ops::Index; use std::sync::Arc; use rustc_hash::FxHashMap; @@ -44,14 +45,6 @@ pub struct BodySyntaxMapping { } impl Body { - pub fn expr(&self, expr: ExprId) -> &Expr { - &self.exprs[expr] - } - - pub fn pat(&self, pat: PatId) -> &Pat { - &self.pats[pat] - } - pub fn args(&self) -> &[PatId] { &self.args } @@ -61,6 +54,22 @@ impl Body { } } +impl Index for Body { + type Output = Expr; + + fn index(&self, expr: ExprId) -> &Expr { + &self.exprs[expr] + } +} + +impl Index for Body { + type Output = Pat; + + fn index(&self, pat: PatId) -> &Pat { + &self.pats[pat] + } +} + impl BodySyntaxMapping { pub fn expr_syntax(&self, expr: ExprId) -> Option { self.expr_syntax_mapping_back.get(&expr).cloned() @@ -377,11 +386,7 @@ impl ExprCollector { syntax_ptr, ) } else { - let condition = if let Some(condition) = e.condition() { - self.collect_expr_opt(condition.expr()) - } else { - self.exprs.alloc(Expr::Missing) - }; + let condition = self.collect_expr_opt(e.condition().and_then(|c| c.expr())); let then_branch = self.collect_block_opt(e.then_branch()); let else_branch = e.else_branch().map(|e| self.collect_block(e)); self.alloc_expr( diff --git a/crates/ra_hir/src/function/scope.rs b/crates/ra_hir/src/function/scope.rs index 0607a99cb..0a12f0b35 100644 --- a/crates/ra_hir/src/function/scope.rs +++ b/crates/ra_hir/src/function/scope.rs @@ -66,8 +66,7 @@ impl FnScopes { .scope_chain_for(context_expr) .flat_map(|scope| self.entries(scope).iter()) .filter(|entry| shadowed.insert(entry.name())) - .filter(|entry| entry.name() == &name) - .nth(0); + .find(|entry| entry.name() == &name); ret } @@ -84,7 +83,7 @@ impl FnScopes { }) } fn add_bindings(&mut self, body: &Body, scope: ScopeId, pat: PatId) { - match body.pat(pat) { + match &body[pat] { Pat::Bind { name } => self.scopes[scope].entries.push(ScopeEntry { name: name.clone(), pat, @@ -96,7 +95,7 @@ impl FnScopes { let body = Arc::clone(&self.body); params .into_iter() - .for_each(|it| self.add_bindings(&body, scope, *it)); + .for_each(|pat| self.add_bindings(&body, scope, *pat)); } fn set_scope(&mut self, node: ExprId, scope: ScopeId) { self.scope_for.insert(node, scope); @@ -218,8 +217,7 @@ impl ScopesWithSyntaxMapping { node.ancestors() .map(LocalSyntaxPtr::new) .filter_map(|ptr| self.syntax_mapping.syntax_expr(ptr)) - .filter_map(|it| self.scopes.scope_for(it)) - .next() + .find_map(|it| self.scopes.scope_for(it)) } } @@ -264,7 +262,7 @@ fn compute_block_scopes( fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut FnScopes, scope: ScopeId) { scopes.set_scope(expr, scope); - match body.expr(expr) { + match &body[expr] { Expr::Block { statements, tail } => { compute_block_scopes(&statements, *tail, body, scopes, scope); } diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 551f44d4e..29a3960e9 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -92,12 +92,10 @@ pub fn function_from_position( position: FilePosition, ) -> Cancelable> { let file = db.source_file(position.file_id); - let fn_def = if let Some(f) = find_node_at_offset::(file.syntax(), position.offset) - { - f - } else { - return Ok(None); - }; + let fn_def = ctry!(find_node_at_offset::( + file.syntax(), + position.offset + )); function_from_source(db, position.file_id, fn_def) } -- cgit v1.2.3