mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 04:19:13 +00:00
Various small code review improvements
This commit is contained in:
parent
8e3e5ab2c8
commit
e5a6cf8153
3 changed files with 27 additions and 26 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
use std::ops::Index;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
@ -44,14 +45,6 @@ pub struct BodySyntaxMapping {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Body {
|
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] {
|
pub fn args(&self) -> &[PatId] {
|
||||||
&self.args
|
&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 {
|
impl BodySyntaxMapping {
|
||||||
pub fn expr_syntax(&self, expr: ExprId) -> Option<LocalSyntaxPtr> {
|
pub fn expr_syntax(&self, expr: ExprId) -> Option<LocalSyntaxPtr> {
|
||||||
self.expr_syntax_mapping_back.get(&expr).cloned()
|
self.expr_syntax_mapping_back.get(&expr).cloned()
|
||||||
|
@ -377,11 +386,7 @@ impl ExprCollector {
|
||||||
syntax_ptr,
|
syntax_ptr,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
let condition = if let Some(condition) = e.condition() {
|
let condition = self.collect_expr_opt(e.condition().and_then(|c| c.expr()));
|
||||||
self.collect_expr_opt(condition.expr())
|
|
||||||
} else {
|
|
||||||
self.exprs.alloc(Expr::Missing)
|
|
||||||
};
|
|
||||||
let then_branch = self.collect_block_opt(e.then_branch());
|
let then_branch = self.collect_block_opt(e.then_branch());
|
||||||
let else_branch = e.else_branch().map(|e| self.collect_block(e));
|
let else_branch = e.else_branch().map(|e| self.collect_block(e));
|
||||||
self.alloc_expr(
|
self.alloc_expr(
|
||||||
|
|
|
@ -66,8 +66,7 @@ impl FnScopes {
|
||||||
.scope_chain_for(context_expr)
|
.scope_chain_for(context_expr)
|
||||||
.flat_map(|scope| self.entries(scope).iter())
|
.flat_map(|scope| self.entries(scope).iter())
|
||||||
.filter(|entry| shadowed.insert(entry.name()))
|
.filter(|entry| shadowed.insert(entry.name()))
|
||||||
.filter(|entry| entry.name() == &name)
|
.find(|entry| entry.name() == &name);
|
||||||
.nth(0);
|
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,7 +83,7 @@ impl FnScopes {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
fn add_bindings(&mut self, body: &Body, scope: ScopeId, pat: PatId) {
|
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 {
|
Pat::Bind { name } => self.scopes[scope].entries.push(ScopeEntry {
|
||||||
name: name.clone(),
|
name: name.clone(),
|
||||||
pat,
|
pat,
|
||||||
|
@ -96,7 +95,7 @@ impl FnScopes {
|
||||||
let body = Arc::clone(&self.body);
|
let body = Arc::clone(&self.body);
|
||||||
params
|
params
|
||||||
.into_iter()
|
.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) {
|
fn set_scope(&mut self, node: ExprId, scope: ScopeId) {
|
||||||
self.scope_for.insert(node, scope);
|
self.scope_for.insert(node, scope);
|
||||||
|
@ -218,8 +217,7 @@ impl ScopesWithSyntaxMapping {
|
||||||
node.ancestors()
|
node.ancestors()
|
||||||
.map(LocalSyntaxPtr::new)
|
.map(LocalSyntaxPtr::new)
|
||||||
.filter_map(|ptr| self.syntax_mapping.syntax_expr(ptr))
|
.filter_map(|ptr| self.syntax_mapping.syntax_expr(ptr))
|
||||||
.filter_map(|it| self.scopes.scope_for(it))
|
.find_map(|it| self.scopes.scope_for(it))
|
||||||
.next()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -264,7 +262,7 @@ fn compute_block_scopes(
|
||||||
|
|
||||||
fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut FnScopes, scope: ScopeId) {
|
fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut FnScopes, scope: ScopeId) {
|
||||||
scopes.set_scope(expr, scope);
|
scopes.set_scope(expr, scope);
|
||||||
match body.expr(expr) {
|
match &body[expr] {
|
||||||
Expr::Block { statements, tail } => {
|
Expr::Block { statements, tail } => {
|
||||||
compute_block_scopes(&statements, *tail, body, scopes, scope);
|
compute_block_scopes(&statements, *tail, body, scopes, scope);
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,12 +92,10 @@ pub fn function_from_position(
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
) -> Cancelable<Option<Function>> {
|
) -> Cancelable<Option<Function>> {
|
||||||
let file = db.source_file(position.file_id);
|
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)
|
let fn_def = ctry!(find_node_at_offset::<ast::FnDef>(
|
||||||
{
|
file.syntax(),
|
||||||
f
|
position.offset
|
||||||
} else {
|
));
|
||||||
return Ok(None);
|
|
||||||
};
|
|
||||||
function_from_source(db, position.file_id, fn_def)
|
function_from_source(db, position.file_id, fn_def)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue