2022-10-21 12:28:59 -05:00
|
|
|
use std::collections::BTreeSet;
|
|
|
|
|
2021-01-01 23:47:35 -06:00
|
|
|
use ast::make;
|
2021-09-26 09:01:54 -05:00
|
|
|
use either::Either;
|
2021-10-07 07:46:49 -05:00
|
|
|
use hir::{db::HirDatabase, PathResolution, Semantics, TypeInfo};
|
2021-09-25 11:39:43 -05:00
|
|
|
use ide_db::{
|
2021-09-26 07:38:58 -05:00
|
|
|
base_db::{FileId, FileRange},
|
|
|
|
defs::Definition,
|
2022-03-06 12:01:30 -06:00
|
|
|
imports::insert_use::remove_path_if_in_use_stmt,
|
2021-09-26 07:38:58 -05:00
|
|
|
path_transform::PathTransform,
|
2023-12-05 08:42:39 -06:00
|
|
|
search::{FileReference, FileReferenceNode, SearchScope},
|
2022-08-23 23:50:12 -05:00
|
|
|
source_change::SourceChangeBuilder,
|
2022-07-25 15:43:25 -05:00
|
|
|
syntax_helpers::{insert_whitespace_into_node::insert_ws_into, node_ext::expr_as_name_ref},
|
2021-09-25 11:39:43 -05:00
|
|
|
RootDatabase,
|
|
|
|
};
|
2021-09-26 09:01:54 -05:00
|
|
|
use itertools::{izip, Itertools};
|
2021-01-01 23:47:35 -06:00
|
|
|
use syntax::{
|
2023-06-18 03:59:11 -05:00
|
|
|
ast::{self, edit::IndentLevel, edit_in_place::Indent, HasArgList, PathExpr},
|
2022-08-18 20:08:59 -05:00
|
|
|
ted, AstNode, NodeOrToken, SyntaxKind,
|
2021-01-01 23:47:35 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
assist_context::{AssistContext, Assists},
|
|
|
|
AssistId, AssistKind,
|
|
|
|
};
|
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
// Assist: inline_into_callers
|
|
|
|
//
|
|
|
|
// Inline a function or method body into all of its callers where possible, creating a `let` statement per parameter
|
|
|
|
// unless the parameter can be inlined. The parameter will be inlined either if it the supplied argument is a simple local
|
|
|
|
// or if the parameter is only accessed inside the function body once.
|
|
|
|
// If all calls can be inlined the function will be removed.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// fn print(_: &str) {}
|
|
|
|
// fn foo$0(word: &str) {
|
|
|
|
// if !word.is_empty() {
|
|
|
|
// print(word);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// fn bar() {
|
|
|
|
// foo("안녕하세요");
|
|
|
|
// foo("여러분");
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// fn print(_: &str) {}
|
|
|
|
//
|
|
|
|
// fn bar() {
|
|
|
|
// {
|
|
|
|
// let word = "안녕하세요";
|
|
|
|
// if !word.is_empty() {
|
|
|
|
// print(word);
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
// {
|
|
|
|
// let word = "여러분";
|
|
|
|
// if !word.is_empty() {
|
|
|
|
// print(word);
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
// }
|
|
|
|
// ```
|
2022-07-20 08:02:08 -05:00
|
|
|
pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
2021-10-13 07:39:37 -05:00
|
|
|
let def_file = ctx.file_id();
|
2021-09-25 11:39:43 -05:00
|
|
|
let name = ctx.find_node_at_offset::<ast::Name>()?;
|
2021-09-26 07:38:58 -05:00
|
|
|
let ast_func = name.syntax().parent().and_then(ast::Fn::cast)?;
|
|
|
|
let func_body = ast_func.body()?;
|
|
|
|
let param_list = ast_func.param_list()?;
|
|
|
|
|
|
|
|
let function = ctx.sema.to_def(&ast_func)?;
|
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
let params = get_fn_params(ctx.sema.db, function, ¶m_list)?;
|
|
|
|
|
2021-11-10 15:02:50 -06:00
|
|
|
let usages = Definition::Function(function).usages(&ctx.sema);
|
2021-09-25 11:39:43 -05:00
|
|
|
if !usages.at_least_one() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-09-26 07:38:58 -05:00
|
|
|
let is_recursive_fn = usages
|
|
|
|
.clone()
|
2023-07-09 16:20:18 -05:00
|
|
|
.in_scope(&SearchScope::file_range(FileRange {
|
2021-09-26 07:38:58 -05:00
|
|
|
file_id: def_file,
|
|
|
|
range: func_body.syntax().text_range(),
|
|
|
|
}))
|
|
|
|
.at_least_one();
|
|
|
|
if is_recursive_fn {
|
|
|
|
cov_mark::hit!(inline_into_callers_recursive);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
acc.add(
|
|
|
|
AssistId("inline_into_callers", AssistKind::RefactorInline),
|
|
|
|
"Inline into all callers",
|
|
|
|
name.syntax().text_range(),
|
|
|
|
|builder| {
|
|
|
|
let mut usages = usages.all();
|
|
|
|
let current_file_usage = usages.references.remove(&def_file);
|
|
|
|
|
2021-09-26 07:38:58 -05:00
|
|
|
let mut remove_def = true;
|
|
|
|
let mut inline_refs_for_file = |file_id, refs: Vec<FileReference>| {
|
2021-09-25 11:39:43 -05:00
|
|
|
builder.edit_file(file_id);
|
|
|
|
let count = refs.len();
|
2021-09-26 09:01:54 -05:00
|
|
|
// The collects are required as we are otherwise iterating while mutating 🙅♀️🙅♂️
|
2022-08-23 23:50:12 -05:00
|
|
|
let (name_refs, name_refs_use) = split_refs_and_uses(builder, refs, Some);
|
2021-09-26 09:01:54 -05:00
|
|
|
let call_infos: Vec<_> = name_refs
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(CallInfo::from_name_ref)
|
2021-09-25 11:39:43 -05:00
|
|
|
.map(|call_info| {
|
2021-09-26 09:01:54 -05:00
|
|
|
let mut_node = builder.make_syntax_mut(call_info.node.syntax().clone());
|
|
|
|
(call_info, mut_node)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let replaced = call_infos
|
|
|
|
.into_iter()
|
|
|
|
.map(|(call_info, mut_node)| {
|
2021-09-25 11:39:43 -05:00
|
|
|
let replacement =
|
|
|
|
inline(&ctx.sema, def_file, function, &func_body, ¶ms, &call_info);
|
2021-09-26 09:01:54 -05:00
|
|
|
ted::replace(mut_node, replacement.syntax());
|
2021-09-25 11:39:43 -05:00
|
|
|
})
|
|
|
|
.count();
|
2021-09-26 09:01:54 -05:00
|
|
|
if replaced + name_refs_use.len() == count {
|
|
|
|
// we replaced all usages in this file, so we can remove the imports
|
2022-08-23 23:50:12 -05:00
|
|
|
name_refs_use.iter().for_each(remove_path_if_in_use_stmt);
|
2021-09-26 09:01:54 -05:00
|
|
|
} else {
|
|
|
|
remove_def = false;
|
|
|
|
}
|
2021-09-25 11:39:43 -05:00
|
|
|
};
|
|
|
|
for (file_id, refs) in usages.into_iter() {
|
2021-09-26 07:38:58 -05:00
|
|
|
inline_refs_for_file(file_id, refs);
|
2021-09-25 11:39:43 -05:00
|
|
|
}
|
2021-10-03 07:53:01 -05:00
|
|
|
match current_file_usage {
|
|
|
|
Some(refs) => inline_refs_for_file(def_file, refs),
|
|
|
|
None => builder.edit_file(def_file),
|
2021-09-25 11:39:43 -05:00
|
|
|
}
|
2021-09-26 07:38:58 -05:00
|
|
|
if remove_def {
|
|
|
|
builder.delete(ast_func.syntax().text_range());
|
2021-09-25 11:39:43 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-23 23:50:12 -05:00
|
|
|
pub(super) fn split_refs_and_uses<T: ast::AstNode>(
|
|
|
|
builder: &mut SourceChangeBuilder,
|
|
|
|
iter: impl IntoIterator<Item = FileReference>,
|
|
|
|
mut map_ref: impl FnMut(ast::NameRef) -> Option<T>,
|
|
|
|
) -> (Vec<T>, Vec<ast::Path>) {
|
|
|
|
iter.into_iter()
|
|
|
|
.filter_map(|file_ref| match file_ref.name {
|
2023-12-05 08:42:39 -06:00
|
|
|
FileReferenceNode::NameRef(name_ref) => Some(name_ref),
|
2022-08-23 23:50:12 -05:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.filter_map(|name_ref| match name_ref.syntax().ancestors().find_map(ast::UseTree::cast) {
|
|
|
|
Some(use_tree) => builder.make_mut(use_tree).path().map(Either::Right),
|
|
|
|
None => map_ref(name_ref).map(Either::Left),
|
|
|
|
})
|
|
|
|
.partition_map(|either| either)
|
|
|
|
}
|
|
|
|
|
2021-07-03 10:13:56 -05:00
|
|
|
// Assist: inline_call
|
2021-07-02 18:33:34 -05:00
|
|
|
//
|
2021-07-05 08:34:01 -05:00
|
|
|
// Inlines a function or method body creating a `let` statement per parameter unless the parameter
|
|
|
|
// can be inlined. The parameter will be inlined either if it the supplied argument is a simple local
|
|
|
|
// or if the parameter is only accessed inside the function body once.
|
2021-01-01 23:47:35 -06:00
|
|
|
//
|
|
|
|
// ```
|
2021-07-05 07:24:25 -05:00
|
|
|
// # //- minicore: option
|
|
|
|
// fn foo(name: Option<&str>) {
|
|
|
|
// let name = name.unwrap$0();
|
2021-01-01 23:47:35 -06:00
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
2021-07-05 07:24:25 -05:00
|
|
|
// fn foo(name: Option<&str>) {
|
|
|
|
// let name = match name {
|
|
|
|
// Some(val) => val,
|
|
|
|
// None => panic!("called `Option::unwrap()` on a `None` value"),
|
|
|
|
// };
|
2021-01-01 23:47:35 -06:00
|
|
|
// }
|
|
|
|
// ```
|
2022-07-20 08:02:08 -05:00
|
|
|
pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
2021-09-26 07:56:43 -05:00
|
|
|
let name_ref: ast::NameRef = ctx.find_node_at_offset()?;
|
|
|
|
let call_info = CallInfo::from_name_ref(name_ref.clone())?;
|
|
|
|
let (function, label) = match &call_info.node {
|
2021-10-03 05:42:00 -05:00
|
|
|
ast::CallableExpr::Call(call) => {
|
2021-09-26 07:56:43 -05:00
|
|
|
let path = match call.expr()? {
|
|
|
|
ast::Expr::PathExpr(path) => path.path(),
|
|
|
|
_ => None,
|
|
|
|
}?;
|
2021-07-03 10:13:56 -05:00
|
|
|
let function = match ctx.sema.resolve_path(&path)? {
|
2021-09-26 07:56:43 -05:00
|
|
|
PathResolution::Def(hir::ModuleDef::Function(f)) => f,
|
2021-07-03 10:13:56 -05:00
|
|
|
_ => return None,
|
|
|
|
};
|
2022-10-10 13:20:27 -05:00
|
|
|
(function, format!("Inline `{path}`"))
|
2021-09-26 07:56:43 -05:00
|
|
|
}
|
2021-10-03 05:42:00 -05:00
|
|
|
ast::CallableExpr::MethodCall(call) => {
|
2022-10-10 13:20:27 -05:00
|
|
|
(ctx.sema.resolve_method_call(call)?, format!("Inline `{name_ref}`"))
|
2021-09-26 07:56:43 -05:00
|
|
|
}
|
|
|
|
};
|
2021-07-03 10:13:56 -05:00
|
|
|
|
2021-10-07 07:46:49 -05:00
|
|
|
let fn_source = ctx.sema.source(function)?;
|
2021-09-26 07:38:58 -05:00
|
|
|
let fn_body = fn_source.value.body()?;
|
|
|
|
let param_list = fn_source.value.param_list()?;
|
2021-09-25 11:39:43 -05:00
|
|
|
|
2022-07-05 05:46:09 -05:00
|
|
|
let FileRange { file_id, range } = fn_source.syntax().original_file_range(ctx.sema.db);
|
2021-10-13 07:39:37 -05:00
|
|
|
if file_id == ctx.file_id() && range.contains(ctx.offset()) {
|
2021-09-26 07:38:58 -05:00
|
|
|
cov_mark::hit!(inline_call_recursive);
|
|
|
|
return None;
|
|
|
|
}
|
2021-09-25 11:39:43 -05:00
|
|
|
let params = get_fn_params(ctx.sema.db, function, ¶m_list)?;
|
|
|
|
|
|
|
|
if call_info.arguments.len() != params.len() {
|
|
|
|
// Can't inline the function because they've passed the wrong number of
|
|
|
|
// arguments to this function
|
|
|
|
cov_mark::hit!(inline_call_incorrect_number_of_arguments);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let syntax = call_info.node.syntax().clone();
|
|
|
|
acc.add(
|
|
|
|
AssistId("inline_call", AssistKind::RefactorInline),
|
|
|
|
label,
|
|
|
|
syntax.text_range(),
|
|
|
|
|builder| {
|
|
|
|
let replacement = inline(&ctx.sema, file_id, function, &fn_body, ¶ms, &call_info);
|
|
|
|
builder.replace_ast(
|
|
|
|
match call_info.node {
|
2021-10-03 05:42:00 -05:00
|
|
|
ast::CallableExpr::Call(it) => ast::Expr::CallExpr(it),
|
|
|
|
ast::CallableExpr::MethodCall(it) => ast::Expr::MethodCallExpr(it),
|
2021-09-25 11:39:43 -05:00
|
|
|
},
|
|
|
|
replacement,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
)
|
2021-07-02 18:33:34 -05:00
|
|
|
}
|
2021-01-01 23:47:35 -06:00
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
struct CallInfo {
|
2021-10-03 05:42:00 -05:00
|
|
|
node: ast::CallableExpr,
|
2021-09-25 11:39:43 -05:00
|
|
|
arguments: Vec<ast::Expr>,
|
2021-08-10 07:39:56 -05:00
|
|
|
generic_arg_list: Option<ast::GenericArgList>,
|
2021-09-25 11:39:43 -05:00
|
|
|
}
|
|
|
|
|
2021-09-26 07:56:43 -05:00
|
|
|
impl CallInfo {
|
|
|
|
fn from_name_ref(name_ref: ast::NameRef) -> Option<CallInfo> {
|
|
|
|
let parent = name_ref.syntax().parent()?;
|
|
|
|
if let Some(call) = ast::MethodCallExpr::cast(parent.clone()) {
|
|
|
|
let receiver = call.receiver()?;
|
|
|
|
let mut arguments = vec![receiver];
|
|
|
|
arguments.extend(call.arg_list()?.args());
|
|
|
|
Some(CallInfo {
|
|
|
|
generic_arg_list: call.generic_arg_list(),
|
2021-10-03 05:42:00 -05:00
|
|
|
node: ast::CallableExpr::MethodCall(call),
|
2021-09-26 07:56:43 -05:00
|
|
|
arguments,
|
|
|
|
})
|
|
|
|
} else if let Some(segment) = ast::PathSegment::cast(parent) {
|
|
|
|
let path = segment.syntax().parent().and_then(ast::Path::cast)?;
|
|
|
|
let path = path.syntax().parent().and_then(ast::PathExpr::cast)?;
|
|
|
|
let call = path.syntax().parent().and_then(ast::CallExpr::cast)?;
|
|
|
|
|
|
|
|
Some(CallInfo {
|
|
|
|
arguments: call.arg_list()?.args().collect(),
|
2021-10-03 05:42:00 -05:00
|
|
|
node: ast::CallableExpr::Call(call),
|
2021-09-26 07:56:43 -05:00
|
|
|
generic_arg_list: segment.generic_arg_list(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
fn get_fn_params(
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
function: hir::Function,
|
|
|
|
param_list: &ast::ParamList,
|
|
|
|
) -> Option<Vec<(ast::Pat, Option<ast::Type>, hir::Param)>> {
|
|
|
|
let mut assoc_fn_params = function.assoc_fn_params(db).into_iter();
|
2021-01-01 23:47:35 -06:00
|
|
|
|
2021-07-02 18:33:34 -05:00
|
|
|
let mut params = Vec::new();
|
|
|
|
if let Some(self_param) = param_list.self_param() {
|
|
|
|
// FIXME this should depend on the receiver as well as the self_param
|
2021-07-03 13:05:00 -05:00
|
|
|
params.push((
|
2021-07-02 18:33:34 -05:00
|
|
|
make::ident_pat(
|
|
|
|
self_param.amp_token().is_some(),
|
|
|
|
self_param.mut_token().is_some(),
|
|
|
|
make::name("this"),
|
|
|
|
)
|
|
|
|
.into(),
|
2021-07-10 12:16:32 -05:00
|
|
|
None,
|
2021-07-03 13:05:00 -05:00
|
|
|
assoc_fn_params.next()?,
|
|
|
|
));
|
2021-07-02 18:33:34 -05:00
|
|
|
}
|
|
|
|
for param in param_list.params() {
|
2021-07-10 12:16:32 -05:00
|
|
|
params.push((param.pat()?, param.ty(), assoc_fn_params.next()?));
|
2021-07-02 18:33:34 -05:00
|
|
|
}
|
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
Some(params)
|
|
|
|
}
|
2021-07-03 13:05:00 -05:00
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
fn inline(
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2021-09-25 11:39:43 -05:00
|
|
|
function_def_file_id: FileId,
|
|
|
|
function: hir::Function,
|
|
|
|
fn_body: &ast::BlockExpr,
|
|
|
|
params: &[(ast::Pat, Option<ast::Type>, hir::Param)],
|
|
|
|
CallInfo { node, arguments, generic_arg_list }: &CallInfo,
|
|
|
|
) -> ast::Expr {
|
2023-06-18 03:59:11 -05:00
|
|
|
let mut body = if sema.hir_file_for(fn_body.syntax()).is_macro() {
|
2022-07-25 15:43:25 -05:00
|
|
|
cov_mark::hit!(inline_call_defined_in_macro);
|
|
|
|
if let Some(body) = ast::BlockExpr::cast(insert_ws_into(fn_body.syntax().clone())) {
|
|
|
|
body
|
|
|
|
} else {
|
|
|
|
fn_body.clone_for_update()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fn_body.clone_for_update()
|
|
|
|
};
|
2021-09-25 11:39:43 -05:00
|
|
|
let usages_for_locals = |local| {
|
|
|
|
Definition::Local(local)
|
2022-03-12 06:04:13 -06:00
|
|
|
.usages(sema)
|
2021-09-25 11:39:43 -05:00
|
|
|
.all()
|
|
|
|
.references
|
|
|
|
.remove(&function_def_file_id)
|
|
|
|
.unwrap_or_default()
|
|
|
|
.into_iter()
|
|
|
|
};
|
|
|
|
let param_use_nodes: Vec<Vec<_>> = params
|
|
|
|
.iter()
|
|
|
|
.map(|(pat, _, param)| {
|
|
|
|
if !matches!(pat, ast::Pat::IdentPat(pat) if pat.is_simple_ident()) {
|
|
|
|
return Vec::new();
|
2021-07-03 13:05:00 -05:00
|
|
|
}
|
2022-04-08 17:55:45 -05:00
|
|
|
// FIXME: we need to fetch all locals declared in the parameter here
|
|
|
|
// not only the local if it is a simple binding
|
|
|
|
match param.as_local(sema.db) {
|
|
|
|
Some(l) => usages_for_locals(l)
|
|
|
|
.map(|FileReference { name, range, .. }| match name {
|
2023-12-05 08:42:39 -06:00
|
|
|
FileReferenceNode::NameRef(_) => body
|
2022-04-08 17:55:45 -05:00
|
|
|
.syntax()
|
|
|
|
.covering_element(range)
|
|
|
|
.ancestors()
|
|
|
|
.nth(3)
|
|
|
|
.and_then(ast::PathExpr::cast),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect::<Option<Vec<_>>>()
|
|
|
|
.unwrap_or_default(),
|
|
|
|
None => Vec::new(),
|
|
|
|
}
|
2021-09-25 11:39:43 -05:00
|
|
|
})
|
|
|
|
.collect();
|
2022-08-18 20:06:00 -05:00
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
if function.self_param(sema.db).is_some() {
|
2023-09-09 07:35:26 -05:00
|
|
|
let this = || {
|
|
|
|
make::name_ref("this")
|
|
|
|
.syntax()
|
|
|
|
.clone_for_update()
|
|
|
|
.first_token()
|
|
|
|
.expect("NameRef should have had a token.")
|
|
|
|
};
|
2022-04-08 17:55:45 -05:00
|
|
|
if let Some(self_local) = params[0].2.as_local(sema.db) {
|
|
|
|
usages_for_locals(self_local)
|
2023-03-15 03:14:49 -05:00
|
|
|
.filter_map(|FileReference { name, range, .. }| match name {
|
2023-12-05 08:42:39 -06:00
|
|
|
FileReferenceNode::NameRef(_) => Some(body.syntax().covering_element(range)),
|
2022-04-08 17:55:45 -05:00
|
|
|
_ => None,
|
|
|
|
})
|
2023-09-09 07:35:26 -05:00
|
|
|
.for_each(|usage| {
|
|
|
|
ted::replace(usage, &this());
|
|
|
|
});
|
2022-04-08 17:55:45 -05:00
|
|
|
}
|
2021-09-25 11:39:43 -05:00
|
|
|
}
|
2022-10-21 12:28:59 -05:00
|
|
|
|
2023-12-13 09:13:55 -06:00
|
|
|
// We should place the following code after last usage of `usages_for_locals`
|
|
|
|
// because `ted::replace` will change the offset in syntax tree, which makes
|
|
|
|
// `FileReference` incorrect
|
2023-12-13 10:42:33 -06:00
|
|
|
if let Some(imp) =
|
|
|
|
sema.ancestors_with_macros(fn_body.syntax().clone()).find_map(ast::Impl::cast)
|
|
|
|
{
|
2023-12-13 09:13:55 -06:00
|
|
|
if !node.syntax().ancestors().any(|anc| &anc == imp.syntax()) {
|
|
|
|
if let Some(t) = imp.self_ty() {
|
|
|
|
while let Some(self_tok) = body
|
|
|
|
.syntax()
|
|
|
|
.descendants_with_tokens()
|
|
|
|
.filter_map(NodeOrToken::into_token)
|
|
|
|
.find(|tok| tok.kind() == SyntaxKind::SELF_TYPE_KW)
|
|
|
|
{
|
|
|
|
let replace_with = t.clone_subtree().syntax().clone_for_update();
|
|
|
|
ted::replace(self_tok, replace_with);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 12:28:59 -05:00
|
|
|
let mut func_let_vars: BTreeSet<String> = BTreeSet::new();
|
|
|
|
|
|
|
|
// grab all of the local variable declarations in the function
|
|
|
|
for stmt in fn_body.statements() {
|
|
|
|
if let Some(let_stmt) = ast::LetStmt::cast(stmt.syntax().to_owned()) {
|
|
|
|
for has_token in let_stmt.syntax().children_with_tokens() {
|
|
|
|
if let Some(node) = has_token.as_node() {
|
|
|
|
if let Some(ident_pat) = ast::IdentPat::cast(node.to_owned()) {
|
|
|
|
func_let_vars.insert(ident_pat.syntax().text().to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-18 03:59:11 -05:00
|
|
|
let mut let_stmts = Vec::new();
|
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
// Inline parameter expressions or generate `let` statements depending on whether inlining works or not.
|
2023-06-18 03:59:11 -05:00
|
|
|
for ((pat, param_ty, _), usages, expr) in izip!(params, param_use_nodes, arguments) {
|
2022-10-21 12:28:59 -05:00
|
|
|
// izip confuses RA due to our lack of hygiene info currently losing us type info causing incorrect errors
|
2022-12-23 01:51:52 -06:00
|
|
|
let usages: &[ast::PathExpr] = &usages;
|
2022-10-21 12:28:59 -05:00
|
|
|
let expr: &ast::Expr = expr;
|
|
|
|
|
2023-06-18 03:59:11 -05:00
|
|
|
let mut insert_let_stmt = || {
|
2022-10-21 12:28:59 -05:00
|
|
|
let ty = sema.type_of_expr(expr).filter(TypeInfo::has_adjustment).and(param_ty.clone());
|
2023-06-18 03:59:11 -05:00
|
|
|
let_stmts.push(
|
|
|
|
make::let_stmt(pat.clone(), ty, Some(expr.clone())).clone_for_update().into(),
|
|
|
|
);
|
2022-10-21 12:28:59 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// check if there is a local var in the function that conflicts with parameter
|
|
|
|
// if it does then emit a let statement and continue
|
|
|
|
if func_let_vars.contains(&expr.syntax().text().to_string()) {
|
|
|
|
insert_let_stmt();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-28 08:31:55 -05:00
|
|
|
let inline_direct = |usage, replacement: &ast::Expr| {
|
|
|
|
if let Some(field) = path_expr_as_record_field(usage) {
|
|
|
|
cov_mark::hit!(inline_call_inline_direct_field);
|
|
|
|
field.replace_expr(replacement.clone_for_update());
|
|
|
|
} else {
|
|
|
|
ted::replace(usage.syntax(), &replacement.syntax().clone_for_update());
|
|
|
|
}
|
|
|
|
};
|
2022-10-21 12:28:59 -05:00
|
|
|
|
2021-09-28 08:31:55 -05:00
|
|
|
match usages {
|
2021-09-25 11:39:43 -05:00
|
|
|
// inline single use closure arguments
|
|
|
|
[usage]
|
|
|
|
if matches!(expr, ast::Expr::ClosureExpr(_))
|
|
|
|
&& usage.syntax().parent().and_then(ast::Expr::cast).is_some() =>
|
2021-07-10 12:16:32 -05:00
|
|
|
{
|
2021-09-25 11:39:43 -05:00
|
|
|
cov_mark::hit!(inline_call_inline_closure);
|
|
|
|
let expr = make::expr_paren(expr.clone());
|
2021-09-28 08:31:55 -05:00
|
|
|
inline_direct(usage, &expr);
|
2021-09-25 11:39:43 -05:00
|
|
|
}
|
|
|
|
// inline single use literals
|
|
|
|
[usage] if matches!(expr, ast::Expr::Literal(_)) => {
|
|
|
|
cov_mark::hit!(inline_call_inline_literal);
|
2022-03-12 06:04:13 -06:00
|
|
|
inline_direct(usage, expr);
|
2021-07-02 18:33:34 -05:00
|
|
|
}
|
2021-09-25 11:39:43 -05:00
|
|
|
// inline direct local arguments
|
2022-03-12 06:04:13 -06:00
|
|
|
[_, ..] if expr_as_name_ref(expr).is_some() => {
|
2021-09-25 11:39:43 -05:00
|
|
|
cov_mark::hit!(inline_call_inline_locals);
|
2022-03-12 08:57:57 -06:00
|
|
|
usages.iter().for_each(|usage| inline_direct(usage, expr));
|
2021-09-25 11:39:43 -05:00
|
|
|
}
|
2021-09-28 08:31:55 -05:00
|
|
|
// can't inline, emit a let statement
|
2021-09-25 11:39:43 -05:00
|
|
|
_ => {
|
2022-10-21 12:28:59 -05:00
|
|
|
insert_let_stmt();
|
2021-08-10 07:39:56 -05:00
|
|
|
}
|
2021-09-25 11:39:43 -05:00
|
|
|
}
|
|
|
|
}
|
2022-10-21 12:28:59 -05:00
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
if let Some(generic_arg_list) = generic_arg_list.clone() {
|
2022-03-31 04:12:08 -05:00
|
|
|
if let Some((target, source)) = &sema.scope(node.syntax()).zip(sema.scope(fn_body.syntax()))
|
|
|
|
{
|
|
|
|
PathTransform::function_call(target, source, function, generic_arg_list)
|
|
|
|
.apply(body.syntax());
|
|
|
|
}
|
2021-09-25 11:39:43 -05:00
|
|
|
}
|
2021-07-03 13:05:00 -05:00
|
|
|
|
2023-06-18 03:59:11 -05:00
|
|
|
let is_async_fn = function.is_async(sema.db);
|
|
|
|
if is_async_fn {
|
|
|
|
cov_mark::hit!(inline_call_async_fn);
|
|
|
|
body = make::async_move_block_expr(body.statements(), body.tail_expr()).clone_for_update();
|
|
|
|
|
|
|
|
// Arguments should be evaluated outside the async block, and then moved into it.
|
|
|
|
if !let_stmts.is_empty() {
|
|
|
|
cov_mark::hit!(inline_call_async_fn_with_let_stmts);
|
|
|
|
body.indent(IndentLevel(1));
|
|
|
|
body = make::block_expr(let_stmts, Some(body.into())).clone_for_update();
|
|
|
|
}
|
|
|
|
} else if let Some(stmt_list) = body.stmt_list() {
|
|
|
|
ted::insert_all(
|
2023-09-09 07:35:26 -05:00
|
|
|
ted::Position::after(
|
|
|
|
stmt_list.l_curly_token().expect("L_CURLY for StatementList is missing."),
|
|
|
|
),
|
2023-06-18 03:59:11 -05:00
|
|
|
let_stmts.into_iter().map(|stmt| stmt.syntax().clone().into()).collect(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
let original_indentation = match node {
|
2021-10-03 05:42:00 -05:00
|
|
|
ast::CallableExpr::Call(it) => it.indent_level(),
|
|
|
|
ast::CallableExpr::MethodCall(it) => it.indent_level(),
|
2021-09-25 11:39:43 -05:00
|
|
|
};
|
|
|
|
body.reindent_to(original_indentation);
|
2021-07-03 13:05:00 -05:00
|
|
|
|
2023-09-21 23:55:10 -05:00
|
|
|
let no_stmts = body.statements().next().is_none();
|
2021-09-25 11:39:43 -05:00
|
|
|
match body.tail_expr() {
|
2023-09-21 23:55:10 -05:00
|
|
|
Some(expr) if matches!(expr, ast::Expr::ClosureExpr(_)) && no_stmts => {
|
|
|
|
make::expr_paren(expr).clone_for_update()
|
|
|
|
}
|
|
|
|
Some(expr) if !is_async_fn && no_stmts => expr,
|
2021-11-20 10:48:04 -06:00
|
|
|
_ => match node
|
|
|
|
.syntax()
|
|
|
|
.parent()
|
|
|
|
.and_then(ast::BinExpr::cast)
|
|
|
|
.and_then(|bin_expr| bin_expr.lhs())
|
|
|
|
{
|
|
|
|
Some(lhs) if lhs.syntax() == node.syntax() => {
|
|
|
|
make::expr_paren(ast::Expr::BlockExpr(body)).clone_for_update()
|
|
|
|
}
|
|
|
|
_ => ast::Expr::BlockExpr(body),
|
|
|
|
},
|
2021-09-25 11:39:43 -05:00
|
|
|
}
|
2021-01-01 23:47:35 -06:00
|
|
|
}
|
|
|
|
|
2021-09-28 08:31:55 -05:00
|
|
|
fn path_expr_as_record_field(usage: &PathExpr) -> Option<ast::RecordExprField> {
|
|
|
|
let path = usage.path()?;
|
|
|
|
let name_ref = path.as_single_name_ref()?;
|
|
|
|
ast::RecordExprField::for_name_ref(&name_ref)
|
|
|
|
}
|
|
|
|
|
2021-01-01 23:47:35 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_args_or_return_value_gets_inlined_without_block() {
|
|
|
|
check_assist(
|
2021-07-03 10:13:56 -05:00
|
|
|
inline_call,
|
2021-01-01 23:47:35 -06:00
|
|
|
r#"
|
|
|
|
fn foo() { println!("Hello, World!"); }
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
fo$0o();
|
2021-01-01 23:47:35 -06:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo() { println!("Hello, World!"); }
|
|
|
|
fn main() {
|
2021-07-03 13:05:00 -05:00
|
|
|
{ println!("Hello, World!"); };
|
2021-01-01 23:47:35 -06:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-05 06:37:40 -05:00
|
|
|
#[test]
|
|
|
|
fn not_applicable_when_incorrect_number_of_parameters_are_provided() {
|
|
|
|
cov_mark::check!(inline_call_incorrect_number_of_arguments);
|
|
|
|
check_assist_not_applicable(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn add(a: u32, b: u32) -> u32 { a + b }
|
|
|
|
fn main() { let x = add$0(42); }
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-01 23:47:35 -06:00
|
|
|
#[test]
|
|
|
|
fn args_with_side_effects() {
|
|
|
|
check_assist(
|
2021-07-03 10:13:56 -05:00
|
|
|
inline_call,
|
2021-01-01 23:47:35 -06:00
|
|
|
r#"
|
2021-07-05 06:37:40 -05:00
|
|
|
fn foo(name: String) {
|
|
|
|
println!("Hello, {}!", name);
|
|
|
|
}
|
2021-01-01 23:47:35 -06:00
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
foo$0(String::from("Michael"));
|
2021-01-01 23:47:35 -06:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
2021-07-05 06:37:40 -05:00
|
|
|
fn foo(name: String) {
|
|
|
|
println!("Hello, {}!", name);
|
|
|
|
}
|
2021-01-01 23:47:35 -06:00
|
|
|
fn main() {
|
2021-07-05 06:37:40 -05:00
|
|
|
{
|
|
|
|
let name = String::from("Michael");
|
|
|
|
println!("Hello, {}!", name);
|
|
|
|
};
|
2021-01-01 23:47:35 -06:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_with_multiple_statements() {
|
|
|
|
check_assist(
|
2021-07-03 10:13:56 -05:00
|
|
|
inline_call,
|
2021-01-01 23:47:35 -06:00
|
|
|
r#"
|
|
|
|
fn foo(a: u32, b: u32) -> u32 {
|
|
|
|
let x = a + b;
|
|
|
|
let y = x - b;
|
|
|
|
x * y
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
let x = foo$0(1, 2);
|
2021-01-01 23:47:35 -06:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo(a: u32, b: u32) -> u32 {
|
|
|
|
let x = a + b;
|
|
|
|
let y = x - b;
|
|
|
|
x * y
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-07-05 06:37:40 -05:00
|
|
|
let x = {
|
|
|
|
let b = 2;
|
2021-07-03 13:05:00 -05:00
|
|
|
let x = 1 + b;
|
2021-01-01 23:47:35 -06:00
|
|
|
let y = x - b;
|
|
|
|
x * y
|
|
|
|
};
|
|
|
|
}
|
2021-07-02 18:33:34 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_with_self_param() {
|
|
|
|
check_assist(
|
2021-07-03 10:13:56 -05:00
|
|
|
inline_call,
|
2021-07-02 18:33:34 -05:00
|
|
|
r#"
|
|
|
|
struct Foo(u32);
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn add(self, a: u32) -> Self {
|
|
|
|
Foo(self.0 + a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = Foo::add$0(Foo(3), 2);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo(u32);
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn add(self, a: u32) -> Self {
|
|
|
|
Foo(self.0 + a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-07-05 09:33:45 -05:00
|
|
|
let x = {
|
|
|
|
let this = Foo(3);
|
|
|
|
Foo(this.0 + 2)
|
|
|
|
};
|
2021-07-02 18:33:34 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn method_by_val() {
|
|
|
|
check_assist(
|
2021-07-03 10:13:56 -05:00
|
|
|
inline_call,
|
2021-07-02 18:33:34 -05:00
|
|
|
r#"
|
|
|
|
struct Foo(u32);
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn add(self, a: u32) -> Self {
|
|
|
|
Foo(self.0 + a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = Foo(3).add$0(2);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo(u32);
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn add(self, a: u32) -> Self {
|
|
|
|
Foo(self.0 + a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-07-05 09:33:45 -05:00
|
|
|
let x = {
|
|
|
|
let this = Foo(3);
|
|
|
|
Foo(this.0 + 2)
|
|
|
|
};
|
2021-07-02 18:33:34 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn method_by_ref() {
|
|
|
|
check_assist(
|
2021-07-03 10:13:56 -05:00
|
|
|
inline_call,
|
2021-07-02 18:33:34 -05:00
|
|
|
r#"
|
|
|
|
struct Foo(u32);
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn add(&self, a: u32) -> Self {
|
|
|
|
Foo(self.0 + a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = Foo(3).add$0(2);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo(u32);
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn add(&self, a: u32) -> Self {
|
|
|
|
Foo(self.0 + a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-15 03:14:49 -05:00
|
|
|
fn main() {
|
|
|
|
let x = {
|
|
|
|
let ref this = Foo(3);
|
|
|
|
Foo(this.0 + 2)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generic_method_by_ref() {
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
struct Foo(u32);
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn add<T>(&self, a: u32) -> Self {
|
|
|
|
Foo(self.0 + a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = Foo(3).add$0::<usize>(2);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo(u32);
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn add<T>(&self, a: u32) -> Self {
|
|
|
|
Foo(self.0 + a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-02 18:33:34 -05:00
|
|
|
fn main() {
|
2021-07-05 06:37:40 -05:00
|
|
|
let x = {
|
|
|
|
let ref this = Foo(3);
|
2021-07-03 13:05:00 -05:00
|
|
|
Foo(this.0 + 2)
|
2021-07-02 18:33:34 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn method_by_ref_mut() {
|
|
|
|
check_assist(
|
2021-07-03 10:13:56 -05:00
|
|
|
inline_call,
|
2021-07-02 18:33:34 -05:00
|
|
|
r#"
|
|
|
|
struct Foo(u32);
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn clear(&mut self) {
|
|
|
|
self.0 = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut foo = Foo(3);
|
|
|
|
foo.clear$0();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo(u32);
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn clear(&mut self) {
|
|
|
|
self.0 = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut foo = Foo(3);
|
2021-07-05 06:37:40 -05:00
|
|
|
{
|
|
|
|
let ref mut this = foo;
|
2021-07-02 18:33:34 -05:00
|
|
|
this.0 = 0;
|
|
|
|
};
|
|
|
|
}
|
2021-07-03 14:42:59 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_multi_use_expr_in_param() {
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn square(x: u32) -> u32 {
|
|
|
|
x * x
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
let x = 51;
|
|
|
|
let y = square$0(10 + x);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn square(x: u32) -> u32 {
|
|
|
|
x * x
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
let x = 51;
|
2021-07-05 06:37:40 -05:00
|
|
|
let y = {
|
|
|
|
let x = 10 + x;
|
2021-07-03 14:42:59 -05:00
|
|
|
x * x
|
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-05 09:33:45 -05:00
|
|
|
fn function_use_local_in_param() {
|
|
|
|
cov_mark::check!(inline_call_inline_locals);
|
2021-07-03 14:42:59 -05:00
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn square(x: u32) -> u32 {
|
|
|
|
x * x
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
let local = 51;
|
|
|
|
let y = square$0(local);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn square(x: u32) -> u32 {
|
|
|
|
x * x
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
let local = 51;
|
|
|
|
let y = local * local;
|
|
|
|
}
|
2021-07-05 06:37:40 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn method_in_impl() {
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(&self) {
|
|
|
|
self;
|
|
|
|
self;
|
|
|
|
}
|
|
|
|
fn bar(&self) {
|
|
|
|
self.foo$0();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(&self) {
|
|
|
|
self;
|
|
|
|
self;
|
|
|
|
}
|
|
|
|
fn bar(&self) {
|
|
|
|
{
|
|
|
|
let ref this = self;
|
|
|
|
this;
|
|
|
|
this;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2021-07-05 08:34:01 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn wraps_closure_in_paren() {
|
2021-07-05 09:33:45 -05:00
|
|
|
cov_mark::check!(inline_call_inline_closure);
|
2021-07-05 08:34:01 -05:00
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn foo(x: fn()) {
|
|
|
|
x();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
foo$0(|| {})
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo(x: fn()) {
|
|
|
|
x();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
{
|
|
|
|
(|| {})();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn foo(x: fn()) {
|
|
|
|
x();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
foo$0(main)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo(x: fn()) {
|
|
|
|
x();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
{
|
|
|
|
main();
|
|
|
|
}
|
|
|
|
}
|
2021-07-05 09:33:45 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_single_literal_expr() {
|
|
|
|
cov_mark::check!(inline_call_inline_literal);
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn foo(x: u32) -> u32{
|
|
|
|
x
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
foo$0(222);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo(x: u32) -> u32{
|
|
|
|
x
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
222;
|
|
|
|
}
|
2021-07-10 12:16:32 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_emits_type_for_coercion() {
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn foo(x: *const u32) -> u32 {
|
|
|
|
x as u32
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
foo$0(&222);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo(x: *const u32) -> u32 {
|
|
|
|
x as u32
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
{
|
|
|
|
let x: *const u32 = &222;
|
|
|
|
x as u32
|
|
|
|
};
|
|
|
|
}
|
2021-08-10 07:39:56 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_substitutes_generics() {
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn foo<T, const N: usize>() {
|
|
|
|
bar::<T, N>()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar<U, const M: usize>() {}
|
|
|
|
|
|
|
|
fn main() {
|
2021-09-26 07:56:43 -05:00
|
|
|
foo$0::<usize, {0}>();
|
2021-08-10 07:39:56 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo<T, const N: usize>() {
|
|
|
|
bar::<T, N>()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar<U, const M: usize>() {}
|
|
|
|
|
|
|
|
fn main() {
|
2023-06-15 10:04:59 -05:00
|
|
|
bar::<usize, {0}>();
|
2021-08-10 07:39:56 -05:00
|
|
|
}
|
2021-09-25 11:39:43 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_callers() {
|
|
|
|
check_assist(
|
|
|
|
inline_into_callers,
|
|
|
|
r#"
|
|
|
|
fn do_the_math$0(b: u32) -> u32 {
|
|
|
|
let foo = 10;
|
|
|
|
foo * b + foo
|
|
|
|
}
|
|
|
|
fn foo() {
|
|
|
|
do_the_math(0);
|
|
|
|
let bar = 10;
|
|
|
|
do_the_math(bar);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
{
|
|
|
|
let foo = 10;
|
|
|
|
foo * 0 + foo
|
|
|
|
};
|
|
|
|
let bar = 10;
|
|
|
|
{
|
|
|
|
let foo = 10;
|
|
|
|
foo * bar + foo
|
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_callers_across_files() {
|
|
|
|
check_assist(
|
|
|
|
inline_into_callers,
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
mod foo;
|
|
|
|
fn do_the_math$0(b: u32) -> u32 {
|
|
|
|
let foo = 10;
|
|
|
|
foo * b + foo
|
|
|
|
}
|
|
|
|
//- /foo.rs
|
|
|
|
use super::do_the_math;
|
|
|
|
fn foo() {
|
|
|
|
do_the_math(0);
|
|
|
|
let bar = 10;
|
|
|
|
do_the_math(bar);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
2021-09-26 09:01:54 -05:00
|
|
|
//- /lib.rs
|
|
|
|
mod foo;
|
|
|
|
|
|
|
|
//- /foo.rs
|
2021-09-25 11:39:43 -05:00
|
|
|
fn foo() {
|
|
|
|
{
|
|
|
|
let foo = 10;
|
|
|
|
foo * 0 + foo
|
|
|
|
};
|
|
|
|
let bar = 10;
|
|
|
|
{
|
|
|
|
let foo = 10;
|
|
|
|
foo * bar + foo
|
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_callers_across_files_with_def_file() {
|
|
|
|
check_assist(
|
|
|
|
inline_into_callers,
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
mod foo;
|
|
|
|
fn do_the_math$0(b: u32) -> u32 {
|
|
|
|
let foo = 10;
|
|
|
|
foo * b + foo
|
|
|
|
}
|
|
|
|
fn bar(a: u32, b: u32) -> u32 {
|
|
|
|
do_the_math(0);
|
|
|
|
}
|
|
|
|
//- /foo.rs
|
|
|
|
use super::do_the_math;
|
|
|
|
fn foo() {
|
|
|
|
do_the_math(0);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
mod foo;
|
2021-09-26 09:01:54 -05:00
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
fn bar(a: u32, b: u32) -> u32 {
|
|
|
|
{
|
|
|
|
let foo = 10;
|
|
|
|
foo * 0 + foo
|
|
|
|
};
|
|
|
|
}
|
|
|
|
//- /foo.rs
|
|
|
|
fn foo() {
|
|
|
|
{
|
|
|
|
let foo = 10;
|
|
|
|
foo * 0 + foo
|
|
|
|
};
|
|
|
|
}
|
2021-09-26 07:38:58 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_callers_recursive() {
|
|
|
|
cov_mark::check!(inline_into_callers_recursive);
|
|
|
|
check_assist_not_applicable(
|
|
|
|
inline_into_callers,
|
|
|
|
r#"
|
|
|
|
fn foo$0() {
|
|
|
|
foo();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_call_recursive() {
|
|
|
|
cov_mark::check!(inline_call_recursive);
|
|
|
|
check_assist_not_applicable(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
foo$0();
|
|
|
|
}
|
2021-09-28 08:31:55 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_call_field_shorthand() {
|
|
|
|
cov_mark::check!(inline_call_inline_direct_field);
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
struct Foo {
|
|
|
|
field: u32,
|
|
|
|
field1: u32,
|
|
|
|
field2: u32,
|
|
|
|
field3: u32,
|
|
|
|
}
|
|
|
|
fn foo(field: u32, field1: u32, val2: u32, val3: u32) -> Foo {
|
|
|
|
Foo {
|
|
|
|
field,
|
|
|
|
field1,
|
|
|
|
field2: val2,
|
|
|
|
field3: val3,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
let bar = 0;
|
|
|
|
let baz = 0;
|
|
|
|
foo$0(bar, 0, baz, 0);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo {
|
|
|
|
field: u32,
|
|
|
|
field1: u32,
|
|
|
|
field2: u32,
|
|
|
|
field3: u32,
|
|
|
|
}
|
|
|
|
fn foo(field: u32, field1: u32, val2: u32, val3: u32) -> Foo {
|
|
|
|
Foo {
|
|
|
|
field,
|
|
|
|
field1,
|
|
|
|
field2: val2,
|
|
|
|
field3: val3,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
let bar = 0;
|
|
|
|
let baz = 0;
|
|
|
|
Foo {
|
|
|
|
field: bar,
|
|
|
|
field1: 0,
|
|
|
|
field2: baz,
|
|
|
|
field3: 0,
|
|
|
|
};
|
|
|
|
}
|
2021-01-01 23:47:35 -06:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
2021-11-20 10:48:04 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_callers_wrapped_in_parentheses() {
|
|
|
|
check_assist(
|
|
|
|
inline_into_callers,
|
|
|
|
r#"
|
|
|
|
fn foo$0() -> u32 {
|
|
|
|
let x = 0;
|
|
|
|
x
|
|
|
|
}
|
|
|
|
fn bar() -> u32 {
|
|
|
|
foo() + foo()
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
|
|
|
|
fn bar() -> u32 {
|
|
|
|
({
|
|
|
|
let x = 0;
|
|
|
|
x
|
|
|
|
}) + {
|
|
|
|
let x = 0;
|
|
|
|
x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_call_wrapped_in_parentheses() {
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn foo() -> u32 {
|
|
|
|
let x = 0;
|
|
|
|
x
|
|
|
|
}
|
|
|
|
fn bar() -> u32 {
|
|
|
|
foo$0() + foo()
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo() -> u32 {
|
|
|
|
let x = 0;
|
|
|
|
x
|
|
|
|
}
|
|
|
|
fn bar() -> u32 {
|
|
|
|
({
|
|
|
|
let x = 0;
|
|
|
|
x
|
|
|
|
}) + foo()
|
|
|
|
}
|
2022-07-25 15:43:25 -05:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_call_defined_in_macro() {
|
|
|
|
cov_mark::check!(inline_call_defined_in_macro);
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
macro_rules! define_foo {
|
|
|
|
() => { fn foo() -> u32 {
|
|
|
|
let x = 0;
|
|
|
|
x
|
|
|
|
} };
|
|
|
|
}
|
|
|
|
define_foo!();
|
|
|
|
fn bar() -> u32 {
|
|
|
|
foo$0()
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
macro_rules! define_foo {
|
|
|
|
() => { fn foo() -> u32 {
|
|
|
|
let x = 0;
|
|
|
|
x
|
|
|
|
} };
|
|
|
|
}
|
|
|
|
define_foo!();
|
|
|
|
fn bar() -> u32 {
|
|
|
|
{
|
|
|
|
let x = 0;
|
|
|
|
x
|
|
|
|
}
|
|
|
|
}
|
2022-08-18 20:06:00 -05:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_call_with_self_type() {
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
struct A(u32);
|
|
|
|
impl A {
|
|
|
|
fn f() -> Self { Self(114514) }
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
A::f$0();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct A(u32);
|
|
|
|
impl A {
|
|
|
|
fn f() -> Self { Self(114514) }
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
A(114514);
|
|
|
|
}
|
2022-08-22 23:55:05 -05:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_call_with_self_type_but_within_same_impl() {
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
struct A(u32);
|
|
|
|
impl A {
|
|
|
|
fn f() -> Self { Self(1919810) }
|
|
|
|
fn main() {
|
|
|
|
Self::f$0();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct A(u32);
|
|
|
|
impl A {
|
|
|
|
fn f() -> Self { Self(1919810) }
|
|
|
|
fn main() {
|
|
|
|
Self(1919810);
|
|
|
|
}
|
|
|
|
}
|
2021-11-20 10:48:04 -06:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
2022-10-21 12:28:59 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn local_variable_shadowing_callers_argument() {
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn foo(bar: u32, baz: u32) -> u32 {
|
|
|
|
let a = 1;
|
|
|
|
bar * baz * a * 6
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
let a = 7;
|
|
|
|
let b = 1;
|
|
|
|
let res = foo$0(a, b);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo(bar: u32, baz: u32) -> u32 {
|
|
|
|
let a = 1;
|
|
|
|
bar * baz * a * 6
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
let a = 7;
|
|
|
|
let b = 1;
|
|
|
|
let res = {
|
|
|
|
let bar = a;
|
|
|
|
let a = 1;
|
|
|
|
bar * b * a * 6
|
|
|
|
};
|
|
|
|
}
|
2023-06-18 03:59:11 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn async_fn_single_expression() {
|
|
|
|
cov_mark::check!(inline_call_async_fn);
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
async fn bar(x: u32) -> u32 { x + 1 }
|
|
|
|
async fn foo(arg: u32) -> u32 {
|
|
|
|
bar(arg).await * 2
|
|
|
|
}
|
|
|
|
fn spawn<T>(_: T) {}
|
|
|
|
fn main() {
|
|
|
|
spawn(foo$0(42));
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
async fn bar(x: u32) -> u32 { x + 1 }
|
|
|
|
async fn foo(arg: u32) -> u32 {
|
|
|
|
bar(arg).await * 2
|
|
|
|
}
|
|
|
|
fn spawn<T>(_: T) {}
|
|
|
|
fn main() {
|
|
|
|
spawn(async move {
|
|
|
|
bar(42).await * 2
|
|
|
|
});
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn async_fn_multiple_statements() {
|
|
|
|
cov_mark::check!(inline_call_async_fn);
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
async fn bar(x: u32) -> u32 { x + 1 }
|
|
|
|
async fn foo(arg: u32) -> u32 {
|
|
|
|
bar(arg).await;
|
|
|
|
42
|
|
|
|
}
|
|
|
|
fn spawn<T>(_: T) {}
|
|
|
|
fn main() {
|
|
|
|
spawn(foo$0(42));
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
async fn bar(x: u32) -> u32 { x + 1 }
|
|
|
|
async fn foo(arg: u32) -> u32 {
|
|
|
|
bar(arg).await;
|
|
|
|
42
|
|
|
|
}
|
|
|
|
fn spawn<T>(_: T) {}
|
|
|
|
fn main() {
|
|
|
|
spawn(async move {
|
|
|
|
bar(42).await;
|
|
|
|
42
|
|
|
|
});
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn async_fn_with_let_statements() {
|
|
|
|
cov_mark::check!(inline_call_async_fn);
|
|
|
|
cov_mark::check!(inline_call_async_fn_with_let_stmts);
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
async fn bar(x: u32) -> u32 { x + 1 }
|
|
|
|
async fn foo(x: u32, y: u32, z: &u32) -> u32 {
|
|
|
|
bar(x).await;
|
|
|
|
y + y + *z
|
|
|
|
}
|
|
|
|
fn spawn<T>(_: T) {}
|
|
|
|
fn main() {
|
|
|
|
let var = 42;
|
|
|
|
spawn(foo$0(var, var + 1, &var));
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
async fn bar(x: u32) -> u32 { x + 1 }
|
|
|
|
async fn foo(x: u32, y: u32, z: &u32) -> u32 {
|
|
|
|
bar(x).await;
|
|
|
|
y + y + *z
|
|
|
|
}
|
|
|
|
fn spawn<T>(_: T) {}
|
|
|
|
fn main() {
|
|
|
|
let var = 42;
|
|
|
|
spawn({
|
|
|
|
let y = var + 1;
|
|
|
|
let z: &u32 = &var;
|
|
|
|
async move {
|
|
|
|
bar(var).await;
|
|
|
|
y + y + *z
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-09-21 23:55:10 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_call_closure_body() {
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn f() -> impl Fn() -> i32 {
|
|
|
|
|| 2
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let _ = $0f()();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn f() -> impl Fn() -> i32 {
|
|
|
|
|| 2
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let _ = (|| 2)();
|
|
|
|
}
|
2022-10-21 12:28:59 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
2023-12-13 09:13:55 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_call_with_multiple_self_types_eq() {
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
#[derive(PartialEq, Eq)]
|
|
|
|
enum Enum {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Enum {
|
|
|
|
fn a_or_b_eq(&self) -> bool {
|
|
|
|
self == &Self::A || self == &Self::B
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn a() -> bool {
|
|
|
|
Enum::A.$0a_or_b_eq()
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
#[derive(PartialEq, Eq)]
|
|
|
|
enum Enum {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Enum {
|
|
|
|
fn a_or_b_eq(&self) -> bool {
|
|
|
|
self == &Self::A || self == &Self::B
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn a() -> bool {
|
|
|
|
{
|
|
|
|
let ref this = Enum::A;
|
|
|
|
this == &Enum::A || this == &Enum::B
|
|
|
|
}
|
|
|
|
}
|
2023-12-13 10:42:33 -06:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_call_with_self_type_in_macros() {
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
trait Trait<T1> {
|
|
|
|
fn f(a: T1) -> Self;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_from {
|
|
|
|
($t: ty) => {
|
|
|
|
impl Trait<$t> for $t {
|
|
|
|
fn f(a: $t) -> Self {
|
|
|
|
a as Self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
struct A {}
|
|
|
|
|
|
|
|
impl_from!(A);
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let a: A = A{};
|
|
|
|
let b = <A as Trait<A>>::$0f(a);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
trait Trait<T1> {
|
|
|
|
fn f(a: T1) -> Self;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_from {
|
|
|
|
($t: ty) => {
|
|
|
|
impl Trait<$t> for $t {
|
|
|
|
fn f(a: $t) -> Self {
|
|
|
|
a as Self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
struct A {}
|
|
|
|
|
|
|
|
impl_from!(A);
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let a: A = A{};
|
|
|
|
let b = {
|
|
|
|
let a = a;
|
|
|
|
a as A
|
|
|
|
};
|
|
|
|
}
|
2023-12-13 09:13:55 -06:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
2021-01-01 23:47:35 -06:00
|
|
|
}
|