diff options
author | Florian Diebold <[email protected]> | 2019-01-05 23:33:58 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2019-01-05 23:38:08 +0000 |
commit | e5a6cf815372150ad40dee995b7b89f29e701427 (patch) | |
tree | 68d63fbccd0f5b6c4432aadf98566f428e6fba9f | |
parent | 8e3e5ab2c81f238ea4e731f55eac79b74d9d84c3 (diff) |
Various small code review improvements
-rw-r--r-- | crates/ra_hir/src/expr.rs | 31 | ||||
-rw-r--r-- | crates/ra_hir/src/function/scope.rs | 12 | ||||
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 10 |
3 files changed, 27 insertions, 26 deletions
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 @@ | |||
1 | use std::ops::Index; | ||
1 | use std::sync::Arc; | 2 | use std::sync::Arc; |
2 | 3 | ||
3 | use rustc_hash::FxHashMap; | 4 | use rustc_hash::FxHashMap; |
@@ -44,14 +45,6 @@ pub struct BodySyntaxMapping { | |||
44 | } | 45 | } |
45 | 46 | ||
46 | impl Body { | 47 | impl Body { |
47 | pub fn expr(&self, expr: ExprId) -> &Expr { | ||
48 | &self.exprs[expr] | ||
49 | } | ||
50 | |||
51 | pub fn pat(&self, pat: PatId) -> &Pat { | ||
52 | &self.pats[pat] | ||
53 | } | ||
54 | |||
55 | pub fn args(&self) -> &[PatId] { | 48 | pub fn args(&self) -> &[PatId] { |
56 | &self.args | 49 | &self.args |
57 | } | 50 | } |
@@ -61,6 +54,22 @@ impl Body { | |||
61 | } | 54 | } |
62 | } | 55 | } |
63 | 56 | ||
57 | impl Index<ExprId> for Body { | ||
58 | type Output = Expr; | ||
59 | |||
60 | fn index(&self, expr: ExprId) -> &Expr { | ||
61 | &self.exprs[expr] | ||
62 | } | ||
63 | } | ||
64 | |||
65 | impl Index<PatId> for Body { | ||
66 | type Output = Pat; | ||
67 | |||
68 | fn index(&self, pat: PatId) -> &Pat { | ||
69 | &self.pats[pat] | ||
70 | } | ||
71 | } | ||
72 | |||
64 | impl BodySyntaxMapping { | 73 | impl BodySyntaxMapping { |
65 | pub fn expr_syntax(&self, expr: ExprId) -> Option<LocalSyntaxPtr> { | 74 | pub fn expr_syntax(&self, expr: ExprId) -> Option<LocalSyntaxPtr> { |
66 | self.expr_syntax_mapping_back.get(&expr).cloned() | 75 | self.expr_syntax_mapping_back.get(&expr).cloned() |
@@ -377,11 +386,7 @@ impl ExprCollector { | |||
377 | syntax_ptr, | 386 | syntax_ptr, |
378 | ) | 387 | ) |
379 | } else { | 388 | } else { |
380 | let condition = if let Some(condition) = e.condition() { | 389 | let condition = self.collect_expr_opt(e.condition().and_then(|c| c.expr())); |
381 | self.collect_expr_opt(condition.expr()) | ||
382 | } else { | ||
383 | self.exprs.alloc(Expr::Missing) | ||
384 | }; | ||
385 | let then_branch = self.collect_block_opt(e.then_branch()); | 390 | let then_branch = self.collect_block_opt(e.then_branch()); |
386 | let else_branch = e.else_branch().map(|e| self.collect_block(e)); | 391 | let else_branch = e.else_branch().map(|e| self.collect_block(e)); |
387 | self.alloc_expr( | 392 | 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 { | |||
66 | .scope_chain_for(context_expr) | 66 | .scope_chain_for(context_expr) |
67 | .flat_map(|scope| self.entries(scope).iter()) | 67 | .flat_map(|scope| self.entries(scope).iter()) |
68 | .filter(|entry| shadowed.insert(entry.name())) | 68 | .filter(|entry| shadowed.insert(entry.name())) |
69 | .filter(|entry| entry.name() == &name) | 69 | .find(|entry| entry.name() == &name); |
70 | .nth(0); | ||
71 | ret | 70 | ret |
72 | } | 71 | } |
73 | 72 | ||
@@ -84,7 +83,7 @@ impl FnScopes { | |||
84 | }) | 83 | }) |
85 | } | 84 | } |
86 | fn add_bindings(&mut self, body: &Body, scope: ScopeId, pat: PatId) { | 85 | fn add_bindings(&mut self, body: &Body, scope: ScopeId, pat: PatId) { |
87 | match body.pat(pat) { | 86 | match &body[pat] { |
88 | Pat::Bind { name } => self.scopes[scope].entries.push(ScopeEntry { | 87 | Pat::Bind { name } => self.scopes[scope].entries.push(ScopeEntry { |
89 | name: name.clone(), | 88 | name: name.clone(), |
90 | pat, | 89 | pat, |
@@ -96,7 +95,7 @@ impl FnScopes { | |||
96 | let body = Arc::clone(&self.body); | 95 | let body = Arc::clone(&self.body); |
97 | params | 96 | params |
98 | .into_iter() | 97 | .into_iter() |
99 | .for_each(|it| self.add_bindings(&body, scope, *it)); | 98 | .for_each(|pat| self.add_bindings(&body, scope, *pat)); |
100 | } | 99 | } |
101 | fn set_scope(&mut self, node: ExprId, scope: ScopeId) { | 100 | fn set_scope(&mut self, node: ExprId, scope: ScopeId) { |
102 | self.scope_for.insert(node, scope); | 101 | self.scope_for.insert(node, scope); |
@@ -218,8 +217,7 @@ impl ScopesWithSyntaxMapping { | |||
218 | node.ancestors() | 217 | node.ancestors() |
219 | .map(LocalSyntaxPtr::new) | 218 | .map(LocalSyntaxPtr::new) |
220 | .filter_map(|ptr| self.syntax_mapping.syntax_expr(ptr)) | 219 | .filter_map(|ptr| self.syntax_mapping.syntax_expr(ptr)) |
221 | .filter_map(|it| self.scopes.scope_for(it)) | 220 | .find_map(|it| self.scopes.scope_for(it)) |
222 | .next() | ||
223 | } | 221 | } |
224 | } | 222 | } |
225 | 223 | ||
@@ -264,7 +262,7 @@ fn compute_block_scopes( | |||
264 | 262 | ||
265 | fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut FnScopes, scope: ScopeId) { | 263 | fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut FnScopes, scope: ScopeId) { |
266 | scopes.set_scope(expr, scope); | 264 | scopes.set_scope(expr, scope); |
267 | match body.expr(expr) { | 265 | match &body[expr] { |
268 | Expr::Block { statements, tail } => { | 266 | Expr::Block { statements, tail } => { |
269 | compute_block_scopes(&statements, *tail, body, scopes, scope); | 267 | compute_block_scopes(&statements, *tail, body, scopes, scope); |
270 | } | 268 | } |
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( | |||
92 | position: FilePosition, | 92 | position: FilePosition, |
93 | ) -> Cancelable<Option<Function>> { | 93 | ) -> Cancelable<Option<Function>> { |
94 | let file = db.source_file(position.file_id); | 94 | let file = db.source_file(position.file_id); |
95 | let fn_def = if let Some(f) = find_node_at_offset::<ast::FnDef>(file.syntax(), position.offset) | 95 | let fn_def = ctry!(find_node_at_offset::<ast::FnDef>( |
96 | { | 96 | file.syntax(), |
97 | f | 97 | position.offset |
98 | } else { | 98 | )); |
99 | return Ok(None); | ||
100 | }; | ||
101 | function_from_source(db, position.file_id, fn_def) | 99 | function_from_source(db, position.file_id, fn_def) |
102 | } | 100 | } |
103 | 101 | ||