Various small code review improvements

This commit is contained in:
Florian Diebold 2019-01-06 00:33:58 +01:00
parent 8e3e5ab2c8
commit e5a6cf8153
3 changed files with 27 additions and 26 deletions

View File

@ -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<ExprId> for Body {
type Output = Expr;
fn index(&self, expr: ExprId) -> &Expr {
&self.exprs[expr]
}
}
impl Index<PatId> for Body {
type Output = Pat;
fn index(&self, pat: PatId) -> &Pat {
&self.pats[pat]
}
}
impl BodySyntaxMapping {
pub fn expr_syntax(&self, expr: ExprId) -> Option<LocalSyntaxPtr> {
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(

View File

@ -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);
}

View File

@ -92,12 +92,10 @@ pub fn function_from_position(
position: FilePosition,
) -> Cancelable<Option<Function>> {
let file = db.source_file(position.file_id);
let fn_def = if let Some(f) = find_node_at_offset::<ast::FnDef>(file.syntax(), position.offset)
{
f
} else {
return Ok(None);
};
let fn_def = ctry!(find_node_at_offset::<ast::FnDef>(
file.syntax(),
position.offset
));
function_from_source(db, position.file_id, fn_def)
}