Skip to content

Commit

Permalink
Add for_each_function helper to sema
Browse files Browse the repository at this point in the history
Summary: This removes a bit of duplication and it ensures the `file_id` is always check (missing it is a common mistake).

Reviewed By: alanz

Differential Revision: D56067206

fbshipit-source-id: 9b782263237871b69e12af4b05a8bad5133c1b55
  • Loading branch information
robertoaloi authored and facebook-github-bot committed Apr 15, 2024
1 parent d809f50 commit bc169cf
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 126 deletions.
11 changes: 11 additions & 0 deletions crates/hir/src/sema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,17 @@ impl<'db> Semantic<'db> {
_ => false,
}
}

pub fn for_each_function<F>(&self, file_id: FileId, mut f: F)
where
F: FnMut(&FunctionDef),
{
self.def_map(file_id).get_functions().for_each(|(_, def)| {
if def.file.file_id == file_id {
f(def)
}
});
}
}

pub type FunctionAnyCallBack<'a, T> = &'a mut dyn FnMut(T, ClauseId, AnyCallBackCtx) -> T;
Expand Down
6 changes: 2 additions & 4 deletions crates/ide/src/diagnostics/deprecated_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,8 @@ fn deprecated_function(diagnostics: &mut Vec<Diagnostic>, sema: &Semantic, file_
.iter()
.map(|(m, d)| (m, d.clone()))
.collect::<Vec<_>>();
sema.def_map(file_id).get_functions().for_each(|(_, def)| {
if def.file.file_id == file_id {
check_function(diagnostics, sema, def, &matches)
}
sema.for_each_function(file_id, |def| {
check_function(diagnostics, sema, def, &matches)
});
}

Expand Down
46 changes: 22 additions & 24 deletions crates/ide/src/diagnostics/effect_free_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,31 @@ pub(crate) static DESCRIPTOR: DiagnosticDescriptor = DiagnosticDescriptor {
};

fn effect_free_statement(diags: &mut Vec<Diagnostic>, sema: &Semantic, file_id: FileId) {
sema.def_map(file_id).get_functions().for_each(|(_, def)| {
if def.file.file_id == file_id {
let source_file = sema.parse(file_id);

let def_fb = def.in_function_body(sema, def);
def_fb.fold_function(
Strategy::InvisibleMacros,
(),
&mut |_acc, clause_id, ctx| {
if let AnyExprId::Expr(expr_id) = ctx.item_id {
let body_map = def_fb.get_body_map(clause_id);
let in_clause = def_fb.in_clause(clause_id);
if let Some(in_file_ast_ptr) = body_map.expr(expr_id) {
if let Some(expr_ast) = in_file_ast_ptr.to_node(&source_file) {
if is_statement(&expr_ast)
&& !is_macro_usage(&expr_ast)
&& has_no_effect(in_clause, &expr_id)
&& is_followed_by(SyntaxKind::ANON_COMMA, &expr_ast)
{
diags.push(make_diagnostic(file_id, &expr_ast));
}
sema.for_each_function(file_id, |def| {
let source_file = sema.parse(file_id);

let def_fb = def.in_function_body(sema, def);
def_fb.fold_function(
Strategy::InvisibleMacros,
(),
&mut |_acc, clause_id, ctx| {
if let AnyExprId::Expr(expr_id) = ctx.item_id {
let body_map = def_fb.get_body_map(clause_id);
let in_clause = def_fb.in_clause(clause_id);
if let Some(in_file_ast_ptr) = body_map.expr(expr_id) {
if let Some(expr_ast) = in_file_ast_ptr.to_node(&source_file) {
if is_statement(&expr_ast)
&& !is_macro_usage(&expr_ast)
&& has_no_effect(in_clause, &expr_id)
&& is_followed_by(SyntaxKind::ANON_COMMA, &expr_ast)
{
diags.push(make_diagnostic(file_id, &expr_ast));
}
}
}
},
);
}
}
},
);
});
}

Expand Down
6 changes: 1 addition & 5 deletions crates/ide/src/diagnostics/redundant_assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ fn redundant_assignment(diags: &mut Vec<Diagnostic>, sema: &Semantic, file_id: F
// No point asking for changes to generated files
return;
}
sema.def_map(file_id).get_functions().for_each(|(_, def)| {
if def.file.file_id == file_id {
process_matches(diags, sema, def)
}
});
sema.for_each_function(file_id, |def| process_matches(diags, sema, def));
}

fn process_matches(diags: &mut Vec<Diagnostic>, sema: &Semantic, def: &FunctionDef) {
Expand Down
177 changes: 84 additions & 93 deletions crates/ide/src/diagnostics/replace_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,48 +73,46 @@ pub fn replace_call_site_if_args_match(
sema: &Semantic,
file_id: FileId,
) {
sema.def_map(file_id).get_functions().for_each(|(_, def)| {
if def.file.file_id == file_id {
find_call_in_function(
acc,
sema,
def,
&[(fm, ())],
&args_match,
&move |MakeDiagCtx {
sema,
def_fb,
target,
args,
extra,
range,
}: MakeDiagCtx<'_, (String, String)>| {
let mfa = MFA::from_call_target(
target,
args.len() as u32,
sema,
&def_fb.body(),
file_id,
)?;
let mfa_str = mfa.label();

let diag = diagnostic_builder(&mfa, &extra.0, range)?;

if let Some(edit) =
replace_call(replacement, sema, def_fb, file_id, args, target, &range)
{
Some(diag.with_fixes(Some(vec![fix(
"replace_call_site",
&format!("Replace call to '{:?}' {}", &mfa_str, extra.1),
SourceChange::from_text_edit(file_id, edit),
range,
)])))
} else {
Some(diag)
}
},
);
}
sema.for_each_function(file_id, |def| {
find_call_in_function(
acc,
sema,
def,
&[(fm, ())],
&args_match,
&move |MakeDiagCtx {
sema,
def_fb,
target,
args,
extra,
range,
}: MakeDiagCtx<'_, (String, String)>| {
let mfa = MFA::from_call_target(
target,
args.len() as u32,
sema,
&def_fb.body(),
file_id,
)?;
let mfa_str = mfa.label();

let diag = diagnostic_builder(&mfa, &extra.0, range)?;

if let Some(edit) =
replace_call(replacement, sema, def_fb, file_id, args, target, &range)
{
Some(diag.with_fixes(Some(vec![fix(
"replace_call_site",
&format!("Replace call to '{:?}' {}", &mfa_str, extra.1),
SourceChange::from_text_edit(file_id, edit),
range,
)])))
} else {
Some(diag)
}
},
);
});
}

Expand Down Expand Up @@ -254,58 +252,51 @@ pub fn remove_fun_ref_from_list(
) {
let mfas = vec![(fm, ())];
let matcher = FunctionMatcher::new(&mfas);
sema.def_map(file_id).get_functions().for_each(|(_, def)| {
if def.file.file_id == file_id {
let def_fb = def.in_function_body(sema, def);
let source_file = sema.parse(file_id);
def_fb.clone().fold_function(
Strategy::InvisibleMacros,
(),
&mut |_acc, clause_id, ctx| {
let body_map = def_fb.get_body_map(clause_id);
let in_clause = def_fb.in_clause(clause_id);
match ctx.item_id {
AnyExprId::Expr(expr_id) => {
let matches = match_fun_ref_in_list_in_call_arg(
&matcher, sema, in_clause, &expr_id,
);
matches
.iter()
.for_each(|(matched_funref_id, target, arity)| {
|| -> Option<()> {
let in_file_ast_ptr = body_map.expr(*matched_funref_id)?;
let list_elem_ast =
in_file_ast_ptr.to_node(&source_file)?;
let statement_removal = remove_statement(&list_elem_ast)?;
let mfa = MFA::from_call_target(
target,
*arity,
sema,
&def_fb.body(clause_id),
file_id,
)?;
let range = def_fb
.clone()
.range_for_expr(clause_id, *matched_funref_id)?;
let diag = diagnostic_builder(&mfa, "", range)?;
diags.push(diag.with_fixes(Some(vec![fix(
"remove_fun_ref_from_list",
"Remove noop fun ref from list",
SourceChange::from_text_edit(
file_id,
statement_removal,
),
range,
)])));
Some(())
}();
});
}
_ => {}
sema.for_each_function(file_id, |def| {
let def_fb = def.in_function_body(sema, def);
let source_file = sema.parse(file_id);
def_fb.clone().fold_function(
Strategy::InvisibleMacros,
(),
&mut |_acc, clause_id, ctx| {
let body_map = def_fb.get_body_map(clause_id);
let in_clause = def_fb.in_clause(clause_id);
match ctx.item_id {
AnyExprId::Expr(expr_id) => {
let matches =
match_fun_ref_in_list_in_call_arg(&matcher, sema, in_clause, &expr_id);
matches
.iter()
.for_each(|(matched_funref_id, target, arity)| {
|| -> Option<()> {
let in_file_ast_ptr = body_map.expr(*matched_funref_id)?;
let list_elem_ast = in_file_ast_ptr.to_node(&source_file)?;
let statement_removal = remove_statement(&list_elem_ast)?;
let mfa = MFA::from_call_target(
target,
*arity,
sema,
&def_fb.body(clause_id),
file_id,
)?;
let range = def_fb
.clone()
.range_for_expr(clause_id, *matched_funref_id)?;
let diag = diagnostic_builder(&mfa, "", range)?;
diags.push(diag.with_fixes(Some(vec![fix(
"remove_fun_ref_from_list",
"Remove noop fun ref from list",
SourceChange::from_text_edit(file_id, statement_removal),
range,
)])));
Some(())
}();
});
}
},
);
}
_ => {}
}
},
);
})
}

Expand Down

0 comments on commit bc169cf

Please sign in to comment.