Skip to content

Commit 6ea07fc

Browse files
bors[bot]Veykril
andauthored
Merge #9809
9809: internal: Simplify r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents c5bde08 + c4a119f commit 6ea07fc

File tree

8 files changed

+40
-28
lines changed

8 files changed

+40
-28
lines changed

crates/ide_assists/src/handlers/extract_function.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext) -> Option
100100
body.extracted_function_params(ctx, &container_info, locals_used.iter().copied());
101101

102102
let fun = Function {
103-
name: "fun_name".to_string(),
103+
name: make::name_ref("fun_name"),
104104
self_param,
105105
params,
106106
control_flow,
@@ -170,7 +170,7 @@ fn extraction_target(node: &SyntaxNode, selection_range: TextRange) -> Option<Fu
170170

171171
#[derive(Debug)]
172172
struct Function {
173-
name: String,
173+
name: ast::NameRef,
174174
self_param: Option<ast::SelfParam>,
175175
params: Vec<Param>,
176176
control_flow: ControlFlow,
@@ -1077,11 +1077,12 @@ fn make_call(ctx: &AssistContext, fun: &Function, indent: IndentLevel) -> String
10771077

10781078
let args = fun.params.iter().map(|param| param.to_arg(ctx));
10791079
let args = make::arg_list(args);
1080+
let name = fun.name.clone();
10801081
let call_expr = if fun.self_param.is_some() {
10811082
let self_arg = make::expr_path(make::ext::ident_path("self"));
1082-
make::expr_method_call(self_arg, &fun.name, args)
1083+
make::expr_method_call(self_arg, name, args)
10831084
} else {
1084-
let func = make::expr_path(make::ext::ident_path(&fun.name));
1085+
let func = make::expr_path(make::path_unqualified(make::path_segment(name)));
10851086
make::expr_call(func, args)
10861087
};
10871088

crates/ide_assists/src/handlers/invert_if.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub(crate) fn invert_if(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
3535
}
3636

3737
// This assist should not apply for if-let.
38-
if expr.condition()?.pat().is_some() {
38+
if expr.condition()?.is_pattern_cond() {
3939
return None;
4040
}
4141

crates/ide_assists/src/handlers/move_guard.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContex
115115
return None;
116116
}
117117
// Not support moving if let to arm guard
118-
if cond.pat().is_some() {
118+
if cond.is_pattern_cond() {
119119
return None;
120120
}
121121

crates/ide_assists/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ fn invert_special_case(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Opti
248248
"is_err" => "is_ok",
249249
_ => return None,
250250
};
251-
Some(make::expr_method_call(receiver, method, arg_list))
251+
Some(make::expr_method_call(receiver, make::name_ref(method), arg_list))
252252
}
253253
ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => {
254254
if let ast::Expr::ParenExpr(parexpr) = pe.expr()? {

crates/ide_diagnostics/src/handlers/unlinked_file.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn fixes(ctx: &DiagnosticsContext, file_id: FileId) -> Option<Vec<Assist>> {
3838

3939
let source_root = ctx.sema.db.source_root(ctx.sema.db.file_source_root(file_id));
4040
let our_path = source_root.path_for_file(&file_id)?;
41-
let module_name = our_path.name_and_extension()?.0;
41+
let (module_name, _) = our_path.name_and_extension()?;
4242

4343
// Candidates to look for:
4444
// - `mod.rs` in the same folder
@@ -48,26 +48,23 @@ fn fixes(ctx: &DiagnosticsContext, file_id: FileId) -> Option<Vec<Assist>> {
4848
let mut paths = vec![parent.join("mod.rs")?, parent.join("lib.rs")?, parent.join("main.rs")?];
4949

5050
// `submod/bla.rs` -> `submod.rs`
51-
if let Some(newmod) = (|| {
52-
let name = parent.name_and_extension()?.0;
51+
let parent_mod = (|| {
52+
let (name, _) = parent.name_and_extension()?;
5353
parent.parent()?.join(&format!("{}.rs", name))
54-
})() {
55-
paths.push(newmod);
56-
}
54+
})();
55+
paths.extend(parent_mod);
56+
57+
for &parent_id in paths.iter().filter_map(|path| source_root.file_for_path(path)) {
58+
for &krate in ctx.sema.db.relevant_crates(parent_id).iter() {
59+
let crate_def_map = ctx.sema.db.crate_def_map(krate);
60+
for (_, module) in crate_def_map.modules() {
61+
if module.origin.is_inline() {
62+
// We don't handle inline `mod parent {}`s, they use different paths.
63+
continue;
64+
}
5765

58-
for path in &paths {
59-
if let Some(parent_id) = source_root.file_for_path(path) {
60-
for krate in ctx.sema.db.relevant_crates(*parent_id).iter() {
61-
let crate_def_map = ctx.sema.db.crate_def_map(*krate);
62-
for (_, module) in crate_def_map.modules() {
63-
if module.origin.is_inline() {
64-
// We don't handle inline `mod parent {}`s, they use different paths.
65-
continue;
66-
}
67-
68-
if module.origin.file_id() == Some(*parent_id) {
69-
return make_fixes(ctx.sema.db, *parent_id, module_name, file_id);
70-
}
66+
if module.origin.file_id() == Some(parent_id) {
67+
return make_fixes(ctx.sema.db, parent_id, module_name, file_id);
7168
}
7269
}
7370
}

crates/syntax/src/ast/expr_ext.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl ast::Expr {
4848
}
4949

5050
/// Preorder walk all the expression's child expressions preserving events.
51-
/// If the callback returns true the subtree of the expression will be skipped.
51+
/// If the callback returns true on an [`WalkEvent::Enter`], the subtree of the expression will be skipped.
5252
/// Note that the subtree may already be skipped due to the context analysis this function does.
5353
pub fn preorder(&self, cb: &mut dyn FnMut(WalkEvent<ast::Expr>) -> bool) {
5454
let mut preorder = self.syntax().preorder();

crates/syntax/src/ast/make.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,20 @@ pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr {
304304
pub fn expr_call(f: ast::Expr, arg_list: ast::ArgList) -> ast::Expr {
305305
expr_from_text(&format!("{}{}", f, arg_list))
306306
}
307-
pub fn expr_method_call(receiver: ast::Expr, method: &str, arg_list: ast::ArgList) -> ast::Expr {
307+
pub fn expr_method_call(
308+
receiver: ast::Expr,
309+
method: ast::NameRef,
310+
arg_list: ast::ArgList,
311+
) -> ast::Expr {
308312
expr_from_text(&format!("{}.{}{}", receiver, method, arg_list))
309313
}
310314
pub fn expr_ref(expr: ast::Expr, exclusive: bool) -> ast::Expr {
311315
expr_from_text(&if exclusive { format!("&mut {}", expr) } else { format!("&{}", expr) })
312316
}
317+
pub fn expr_closure(pats: impl IntoIterator<Item = ast::Param>, expr: ast::Expr) -> ast::Expr {
318+
let params = pats.into_iter().join(", ");
319+
expr_from_text(&format!("|{}| {}", params, expr))
320+
}
313321
pub fn expr_paren(expr: ast::Expr) -> ast::Expr {
314322
expr_from_text(&format!("({})", expr))
315323
}

crates/syntax/src/ast/node_ext.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,12 @@ impl ast::Item {
611611
}
612612
}
613613

614+
impl ast::Condition {
615+
pub fn is_pattern_cond(&self) -> bool {
616+
self.let_token().is_some()
617+
}
618+
}
619+
614620
#[derive(Debug, Clone, PartialEq, Eq)]
615621
pub enum FieldKind {
616622
Name(ast::NameRef),

0 commit comments

Comments
 (0)