1 use ide_db::{base_db::FileId, defs::Definition, search::FileReference};
3 algo::find_node_at_range,
4 ast::{self, ArgListOwner},
5 AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, T,
8 use SyntaxKind::WHITESPACE;
11 assist_context::AssistBuilder, utils::next_prev, AssistContext, AssistId, AssistKind, Assists,
14 // Assist: remove_unused_param
16 // Removes unused function parameter.
19 // fn frobnicate(x: i32$0) {}
33 pub(crate) fn remove_unused_param(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
34 let param: ast::Param = ctx.find_node_at_offset()?;
35 let ident_pat = match param.pat()? {
36 ast::Pat::IdentPat(it) => it,
39 let func = param.syntax().ancestors().find_map(ast::Fn::cast)?;
41 // check if fn is in impl Trait for ..
44 .parent() // AssocItemList
45 .and_then(|x| x.parent())
46 .and_then(ast::Impl::cast)
47 .map_or(false, |imp| imp.trait_().is_some())
49 cov_mark::hit!(trait_impl);
53 let param_position = func.param_list()?.params().position(|it| it == param)?;
55 let func = ctx.sema.to_def(&func)?;
56 Definition::ModuleDef(func.into())
60 let local = ctx.sema.to_def(&ident_pat)?;
61 Definition::Local(local)
63 if param_def.usages(&ctx.sema).at_least_one() {
64 cov_mark::hit!(keep_used);
68 AssistId("remove_unused_param", AssistKind::Refactor),
69 "Remove unused parameter",
70 param.syntax().text_range(),
72 builder.delete(range_to_remove(param.syntax()));
73 for (file_id, references) in fn_def.usages(&ctx.sema).all() {
74 process_usages(ctx, builder, file_id, references, param_position);
82 builder: &mut AssistBuilder,
84 references: Vec<FileReference>,
87 let source_file = ctx.sema.parse(file_id);
88 builder.edit_file(file_id);
89 for usage in references {
90 if let Some(text_range) = process_usage(&source_file, usage, arg_to_remove) {
91 builder.delete(text_range);
97 source_file: &SourceFile,
98 FileReference { range, .. }: FileReference,
100 ) -> Option<TextRange> {
101 let call_expr: ast::CallExpr = find_node_at_range(source_file.syntax(), range)?;
102 let call_expr_range = call_expr.expr()?.syntax().text_range();
103 if !call_expr_range.contains_range(range) {
106 let arg = call_expr.arg_list()?.args().nth(arg_to_remove)?;
107 Some(range_to_remove(arg.syntax()))
110 fn range_to_remove(node: &SyntaxNode) -> TextRange {
111 let up_to_comma = next_prev().find_map(|dir| {
112 node.siblings_with_tokens(dir)
113 .filter_map(|it| it.into_token())
114 .find(|it| it.kind() == T![,])
117 if let Some((dir, token)) = up_to_comma {
118 if node.next_sibling().is_some() {
119 let up_to_space = token
120 .siblings_with_tokens(dir)
122 .take_while(|it| it.kind() == WHITESPACE)
124 .and_then(|it| it.into_token());
127 .cover(up_to_space.map_or(token.text_range(), |it| it.text_range()));
129 node.text_range().cover(token.text_range())
137 use crate::tests::{check_assist, check_assist_not_applicable};
147 fn foo(x: i32, $0y: i32) { x; }
148 fn b() { foo(9, 2,) }
152 fn foo(x: i32) { x; }
159 fn remove_unused_first_param() {
163 fn foo($0x: i32, y: i32) { y; }
165 fn b() { foo(1, 2,) }
168 fn foo(y: i32) { y; }
176 fn remove_unused_single_param() {
180 fn foo($0x: i32) { 0; }
193 fn remove_unused_surrounded_by_parms() {
197 fn foo(x: i32, $0y: i32, z: i32) { x; }
198 fn a() { foo(1, 2, 3) }
199 fn b() { foo(1, 2, 3,) }
202 fn foo(x: i32, z: i32) { x; }
204 fn b() { foo(1, 3,) }
210 fn remove_unused_qualified_call() {
214 mod bar { pub fn foo(x: i32, $0y: i32) { x; } }
215 fn b() { bar::foo(9, 2) }
218 mod bar { pub fn foo(x: i32) { x; } }
219 fn b() { bar::foo(9) }
225 fn remove_unused_turbofished_func() {
229 pub fn foo<T>(x: T, $0y: i32) { x; }
230 fn b() { foo::<i32>(9, 2) }
233 pub fn foo<T>(x: T) { x; }
234 fn b() { foo::<i32>(9) }
240 fn remove_unused_generic_unused_param_func() {
244 pub fn foo<T>(x: i32, $0y: T) { x; }
245 fn b() { foo::<i32>(9, 2) }
246 fn b2() { foo(9, 2) }
249 pub fn foo<T>(x: i32) { x; }
250 fn b() { foo::<i32>(9) }
258 cov_mark::check!(keep_used);
259 check_assist_not_applicable(
262 fn foo(x: i32, $0y: i32) { y; }
263 fn main() { foo(9, 2) }
270 cov_mark::check!(trait_impl);
271 check_assist_not_applicable(
285 fn remove_across_files() {
290 fn foo(x: i32, $0y: i32) { x; }
303 fn foo(x: i32) { x; }