]> git.lizzy.rs Git - rust.git/blob - crates/ide-assists/src/utils.rs
fix(assists): remove `item_const` which had default value when implement missing...
[rust.git] / crates / ide-assists / src / utils.rs
1 //! Assorted functions shared by several assists.
2
3 use std::ops;
4
5 pub(crate) use gen_trait_fn_body::gen_trait_fn_body;
6 use hir::{db::HirDatabase, HirDisplay, Semantics};
7 use ide_db::{famous_defs::FamousDefs, path_transform::PathTransform, RootDatabase, SnippetCap};
8 use stdx::format_to;
9 use syntax::{
10     ast::{
11         self,
12         edit::{self, AstNodeEdit},
13         edit_in_place::{AttrsOwnerEdit, Removable},
14         make, HasArgList, HasAttrs, HasGenericParams, HasName, HasTypeBounds, Whitespace,
15     },
16     ted, AstNode, AstToken, Direction, SourceFile,
17     SyntaxKind::*,
18     SyntaxNode, TextRange, TextSize, T,
19 };
20
21 use crate::assist_context::{AssistContext, SourceChangeBuilder};
22
23 pub(crate) mod suggest_name;
24 mod gen_trait_fn_body;
25
26 pub(crate) fn unwrap_trivial_block(block_expr: ast::BlockExpr) -> ast::Expr {
27     extract_trivial_expression(&block_expr)
28         .filter(|expr| !expr.syntax().text().contains_char('\n'))
29         .unwrap_or_else(|| block_expr.into())
30 }
31
32 pub fn extract_trivial_expression(block_expr: &ast::BlockExpr) -> Option<ast::Expr> {
33     if block_expr.modifier().is_some() {
34         return None;
35     }
36     let stmt_list = block_expr.stmt_list()?;
37     let has_anything_else = |thing: &SyntaxNode| -> bool {
38         let mut non_trivial_children =
39             stmt_list.syntax().children_with_tokens().filter(|it| match it.kind() {
40                 WHITESPACE | T!['{'] | T!['}'] => false,
41                 _ => it.as_node() != Some(thing),
42             });
43         non_trivial_children.next().is_some()
44     };
45
46     if let Some(expr) = stmt_list.tail_expr() {
47         if has_anything_else(expr.syntax()) {
48             return None;
49         }
50         return Some(expr);
51     }
52     // Unwrap `{ continue; }`
53     let stmt = stmt_list.statements().next()?;
54     if let ast::Stmt::ExprStmt(expr_stmt) = stmt {
55         if has_anything_else(expr_stmt.syntax()) {
56             return None;
57         }
58         let expr = expr_stmt.expr()?;
59         if matches!(expr.syntax().kind(), CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR) {
60             return Some(expr);
61         }
62     }
63     None
64 }
65
66 /// This is a method with a heuristics to support test methods annotated with custom test annotations, such as
67 /// `#[test_case(...)]`, `#[tokio::test]` and similar.
68 /// Also a regular `#[test]` annotation is supported.
69 ///
70 /// It may produce false positives, for example, `#[wasm_bindgen_test]` requires a different command to run the test,
71 /// but it's better than not to have the runnables for the tests at all.
72 pub fn test_related_attribute(fn_def: &ast::Fn) -> Option<ast::Attr> {
73     fn_def.attrs().find_map(|attr| {
74         let path = attr.path()?;
75         let text = path.syntax().text().to_string();
76         if text.starts_with("test") || text.ends_with("test") {
77             Some(attr)
78         } else {
79             None
80         }
81     })
82 }
83
84 #[derive(Copy, Clone, PartialEq)]
85 pub enum DefaultMethods {
86     Only,
87     No,
88 }
89
90 pub fn filter_assoc_items(
91     sema: &Semantics<'_, RootDatabase>,
92     items: &[hir::AssocItem],
93     default_methods: DefaultMethods,
94 ) -> Vec<ast::AssocItem> {
95     fn has_def_name(item: &ast::AssocItem) -> bool {
96         match item {
97             ast::AssocItem::Fn(def) => def.name(),
98             ast::AssocItem::TypeAlias(def) => def.name(),
99             ast::AssocItem::Const(def) => def.name(),
100             ast::AssocItem::MacroCall(_) => None,
101         }
102         .is_some()
103     }
104
105     items
106         .iter()
107         // Note: This throws away items with no source.
108         .filter_map(|&i| {
109             let item = match i {
110                 hir::AssocItem::Function(i) => ast::AssocItem::Fn(sema.source(i)?.value),
111                 hir::AssocItem::TypeAlias(i) => ast::AssocItem::TypeAlias(sema.source(i)?.value),
112                 hir::AssocItem::Const(i) => ast::AssocItem::Const(sema.source(i)?.value),
113             };
114             Some(item)
115         })
116         .filter(has_def_name)
117         .filter(|it| match it {
118             ast::AssocItem::Fn(def) => matches!(
119                 (default_methods, def.body()),
120                 (DefaultMethods::Only, Some(_)) | (DefaultMethods::No, None)
121             ),
122             ast::AssocItem::Const(def) => matches!(
123                 (default_methods, def.body()),
124                 (DefaultMethods::Only, Some(_)) | (DefaultMethods::No, None)
125             ),
126             _ => default_methods == DefaultMethods::No,
127         })
128         .collect::<Vec<_>>()
129 }
130
131 pub fn add_trait_assoc_items_to_impl(
132     sema: &Semantics<'_, RootDatabase>,
133     items: Vec<ast::AssocItem>,
134     trait_: hir::Trait,
135     impl_: ast::Impl,
136     target_scope: hir::SemanticsScope<'_>,
137 ) -> (ast::Impl, ast::AssocItem) {
138     let source_scope = sema.scope_for_def(trait_);
139
140     let transform = PathTransform::trait_impl(&target_scope, &source_scope, trait_, impl_.clone());
141
142     let items = items.into_iter().map(|assoc_item| {
143         transform.apply(assoc_item.syntax());
144         assoc_item.remove_attrs_and_docs();
145         assoc_item
146     });
147
148     let res = impl_.clone_for_update();
149
150     let assoc_item_list = res.get_or_create_assoc_item_list();
151     let mut first_item = None;
152     for item in items {
153         first_item.get_or_insert_with(|| item.clone());
154         match &item {
155             ast::AssocItem::Fn(fn_) if fn_.body().is_none() => {
156                 let body = make::block_expr(None, Some(make::ext::expr_todo()))
157                     .indent(edit::IndentLevel(1));
158                 ted::replace(fn_.get_or_create_body().syntax(), body.clone_for_update().syntax())
159             }
160             ast::AssocItem::TypeAlias(type_alias) => {
161                 if let Some(type_bound_list) = type_alias.type_bound_list() {
162                     type_bound_list.remove()
163                 }
164             }
165             _ => {}
166         }
167
168         assoc_item_list.add_item(item)
169     }
170
171     (res, first_item.unwrap())
172 }
173
174 #[derive(Clone, Copy, Debug)]
175 pub(crate) enum Cursor<'a> {
176     Replace(&'a SyntaxNode),
177     Before(&'a SyntaxNode),
178 }
179
180 impl<'a> Cursor<'a> {
181     fn node(self) -> &'a SyntaxNode {
182         match self {
183             Cursor::Replace(node) | Cursor::Before(node) => node,
184         }
185     }
186 }
187
188 pub(crate) fn render_snippet(_cap: SnippetCap, node: &SyntaxNode, cursor: Cursor<'_>) -> String {
189     assert!(cursor.node().ancestors().any(|it| it == *node));
190     let range = cursor.node().text_range() - node.text_range().start();
191     let range: ops::Range<usize> = range.into();
192
193     let mut placeholder = cursor.node().to_string();
194     escape(&mut placeholder);
195     let tab_stop = match cursor {
196         Cursor::Replace(placeholder) => format!("${{0:{placeholder}}}"),
197         Cursor::Before(placeholder) => format!("$0{placeholder}"),
198     };
199
200     let mut buf = node.to_string();
201     buf.replace_range(range, &tab_stop);
202     return buf;
203
204     fn escape(buf: &mut String) {
205         stdx::replace(buf, '{', r"\{");
206         stdx::replace(buf, '}', r"\}");
207         stdx::replace(buf, '$', r"\$");
208     }
209 }
210
211 pub(crate) fn vis_offset(node: &SyntaxNode) -> TextSize {
212     node.children_with_tokens()
213         .find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR))
214         .map(|it| it.text_range().start())
215         .unwrap_or_else(|| node.text_range().start())
216 }
217
218 pub(crate) fn invert_boolean_expression(expr: ast::Expr) -> ast::Expr {
219     invert_special_case(&expr).unwrap_or_else(|| make::expr_prefix(T![!], expr))
220 }
221
222 fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
223     match expr {
224         ast::Expr::BinExpr(bin) => {
225             let bin = bin.clone_for_update();
226             let op_token = bin.op_token()?;
227             let rev_token = match op_token.kind() {
228                 T![==] => T![!=],
229                 T![!=] => T![==],
230                 T![<] => T![>=],
231                 T![<=] => T![>],
232                 T![>] => T![<=],
233                 T![>=] => T![<],
234                 // Parenthesize other expressions before prefixing `!`
235                 _ => return Some(make::expr_prefix(T![!], make::expr_paren(expr.clone()))),
236             };
237             ted::replace(op_token, make::token(rev_token));
238             Some(bin.into())
239         }
240         ast::Expr::MethodCallExpr(mce) => {
241             let receiver = mce.receiver()?;
242             let method = mce.name_ref()?;
243             let arg_list = mce.arg_list()?;
244
245             let method = match method.text().as_str() {
246                 "is_some" => "is_none",
247                 "is_none" => "is_some",
248                 "is_ok" => "is_err",
249                 "is_err" => "is_ok",
250                 _ => return None,
251             };
252             Some(make::expr_method_call(receiver, make::name_ref(method), arg_list))
253         }
254         ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::UnaryOp::Not => match pe.expr()? {
255             ast::Expr::ParenExpr(parexpr) => parexpr.expr(),
256             _ => pe.expr(),
257         },
258         ast::Expr::Literal(lit) => match lit.kind() {
259             ast::LiteralKind::Bool(b) => match b {
260                 true => Some(ast::Expr::Literal(make::expr_literal("false"))),
261                 false => Some(ast::Expr::Literal(make::expr_literal("true"))),
262             },
263             _ => None,
264         },
265         _ => None,
266     }
267 }
268
269 pub(crate) fn next_prev() -> impl Iterator<Item = Direction> {
270     [Direction::Next, Direction::Prev].into_iter()
271 }
272
273 pub(crate) fn does_pat_match_variant(pat: &ast::Pat, var: &ast::Pat) -> bool {
274     let first_node_text = |pat: &ast::Pat| pat.syntax().first_child().map(|node| node.text());
275
276     let pat_head = match pat {
277         ast::Pat::IdentPat(bind_pat) => match bind_pat.pat() {
278             Some(p) => first_node_text(&p),
279             None => return pat.syntax().text() == var.syntax().text(),
280         },
281         pat => first_node_text(pat),
282     };
283
284     let var_head = first_node_text(var);
285
286     pat_head == var_head
287 }
288
289 pub(crate) fn does_nested_pattern(pat: &ast::Pat) -> bool {
290     let depth = calc_depth(pat, 0);
291
292     if 1 < depth {
293         return true;
294     }
295     false
296 }
297
298 fn calc_depth(pat: &ast::Pat, depth: usize) -> usize {
299     match pat {
300         ast::Pat::IdentPat(_)
301         | ast::Pat::BoxPat(_)
302         | ast::Pat::RestPat(_)
303         | ast::Pat::LiteralPat(_)
304         | ast::Pat::MacroPat(_)
305         | ast::Pat::OrPat(_)
306         | ast::Pat::ParenPat(_)
307         | ast::Pat::PathPat(_)
308         | ast::Pat::WildcardPat(_)
309         | ast::Pat::RangePat(_)
310         | ast::Pat::RecordPat(_)
311         | ast::Pat::RefPat(_)
312         | ast::Pat::SlicePat(_)
313         | ast::Pat::TuplePat(_)
314         | ast::Pat::ConstBlockPat(_) => depth,
315
316         // FIXME: Other patterns may also be nested. Currently it simply supports only `TupleStructPat`
317         ast::Pat::TupleStructPat(pat) => {
318             let mut max_depth = depth;
319             for p in pat.fields() {
320                 let d = calc_depth(&p, depth + 1);
321                 if d > max_depth {
322                     max_depth = d
323                 }
324             }
325             max_depth
326         }
327     }
328 }
329
330 // Uses a syntax-driven approach to find any impl blocks for the struct that
331 // exist within the module/file
332 //
333 // Returns `None` if we've found an existing fn
334 //
335 // FIXME: change the new fn checking to a more semantic approach when that's more
336 // viable (e.g. we process proc macros, etc)
337 // FIXME: this partially overlaps with `find_impl_block_*`
338
339 /// `find_struct_impl` looks for impl of a struct, but this also has additional feature
340 /// where it takes a list of function names and check if they exist inside impl_, if
341 /// even one match is found, it returns None
342 pub(crate) fn find_struct_impl(
343     ctx: &AssistContext<'_>,
344     adt: &ast::Adt,
345     names: &[String],
346 ) -> Option<Option<ast::Impl>> {
347     let db = ctx.db();
348     let module = adt.syntax().parent()?;
349
350     let struct_def = ctx.sema.to_def(adt)?;
351
352     let block = module.descendants().filter_map(ast::Impl::cast).find_map(|impl_blk| {
353         let blk = ctx.sema.to_def(&impl_blk)?;
354
355         // FIXME: handle e.g. `struct S<T>; impl<U> S<U> {}`
356         // (we currently use the wrong type parameter)
357         // also we wouldn't want to use e.g. `impl S<u32>`
358
359         let same_ty = match blk.self_ty(db).as_adt() {
360             Some(def) => def == struct_def,
361             None => false,
362         };
363         let not_trait_impl = blk.trait_(db).is_none();
364
365         if !(same_ty && not_trait_impl) {
366             None
367         } else {
368             Some(impl_blk)
369         }
370     });
371
372     if let Some(ref impl_blk) = block {
373         if has_any_fn(impl_blk, names) {
374             return None;
375         }
376     }
377
378     Some(block)
379 }
380
381 fn has_any_fn(imp: &ast::Impl, names: &[String]) -> bool {
382     if let Some(il) = imp.assoc_item_list() {
383         for item in il.assoc_items() {
384             if let ast::AssocItem::Fn(f) = item {
385                 if let Some(name) = f.name() {
386                     if names.iter().any(|n| n.eq_ignore_ascii_case(&name.text())) {
387                         return true;
388                     }
389                 }
390             }
391         }
392     }
393
394     false
395 }
396
397 /// Find the start of the `impl` block for the given `ast::Impl`.
398 //
399 // FIXME: this partially overlaps with `find_struct_impl`
400 pub(crate) fn find_impl_block_start(impl_def: ast::Impl, buf: &mut String) -> Option<TextSize> {
401     buf.push('\n');
402     let start = impl_def.assoc_item_list().and_then(|it| it.l_curly_token())?.text_range().end();
403     Some(start)
404 }
405
406 /// Find the end of the `impl` block for the given `ast::Impl`.
407 //
408 // FIXME: this partially overlaps with `find_struct_impl`
409 pub(crate) fn find_impl_block_end(impl_def: ast::Impl, buf: &mut String) -> Option<TextSize> {
410     buf.push('\n');
411     let end = impl_def
412         .assoc_item_list()
413         .and_then(|it| it.r_curly_token())?
414         .prev_sibling_or_token()?
415         .text_range()
416         .end();
417     Some(end)
418 }
419
420 // Generates the surrounding `impl Type { <code> }` including type and lifetime
421 // parameters
422 pub(crate) fn generate_impl_text(adt: &ast::Adt, code: &str) -> String {
423     generate_impl_text_inner(adt, None, code)
424 }
425
426 // Generates the surrounding `impl <trait> for Type { <code> }` including type
427 // and lifetime parameters
428 pub(crate) fn generate_trait_impl_text(adt: &ast::Adt, trait_text: &str, code: &str) -> String {
429     generate_impl_text_inner(adt, Some(trait_text), code)
430 }
431
432 fn generate_impl_text_inner(adt: &ast::Adt, trait_text: Option<&str>, code: &str) -> String {
433     // Ensure lifetime params are before type & const params
434     let generic_params = adt.generic_param_list().map(|generic_params| {
435         let lifetime_params =
436             generic_params.lifetime_params().map(ast::GenericParam::LifetimeParam);
437         let ty_or_const_params = generic_params.type_or_const_params().filter_map(|param| {
438             // remove defaults since they can't be specified in impls
439             match param {
440                 ast::TypeOrConstParam::Type(param) => {
441                     let param = param.clone_for_update();
442                     param.remove_default();
443                     Some(ast::GenericParam::TypeParam(param))
444                 }
445                 ast::TypeOrConstParam::Const(param) => {
446                     let param = param.clone_for_update();
447                     param.remove_default();
448                     Some(ast::GenericParam::ConstParam(param))
449                 }
450             }
451         });
452
453         make::generic_param_list(itertools::chain(lifetime_params, ty_or_const_params))
454     });
455
456     // FIXME: use syntax::make & mutable AST apis instead
457     // `trait_text` and `code` can't be opaque blobs of text
458     let mut buf = String::with_capacity(code.len());
459
460     // Copy any cfg attrs from the original adt
461     buf.push_str("\n\n");
462     let cfg_attrs = adt
463         .attrs()
464         .filter(|attr| attr.as_simple_call().map(|(name, _arg)| name == "cfg").unwrap_or(false));
465     cfg_attrs.for_each(|attr| buf.push_str(&format!("{attr}\n")));
466
467     // `impl{generic_params} {trait_text} for {name}{generic_params.to_generic_args()}`
468     buf.push_str("impl");
469     if let Some(generic_params) = &generic_params {
470         format_to!(buf, "{generic_params}");
471     }
472     buf.push(' ');
473     if let Some(trait_text) = trait_text {
474         buf.push_str(trait_text);
475         buf.push_str(" for ");
476     }
477     buf.push_str(&adt.name().unwrap().text());
478     if let Some(generic_params) = generic_params {
479         format_to!(buf, "{}", generic_params.to_generic_args());
480     }
481
482     match adt.where_clause() {
483         Some(where_clause) => {
484             format_to!(buf, "\n{where_clause}\n{{\n{code}\n}}");
485         }
486         None => {
487             format_to!(buf, " {{\n{code}\n}}");
488         }
489     }
490
491     buf
492 }
493
494 pub(crate) fn add_method_to_adt(
495     builder: &mut SourceChangeBuilder,
496     adt: &ast::Adt,
497     impl_def: Option<ast::Impl>,
498     method: &str,
499 ) {
500     let mut buf = String::with_capacity(method.len() + 2);
501     if impl_def.is_some() {
502         buf.push('\n');
503     }
504     buf.push_str(method);
505
506     let start_offset = impl_def
507         .and_then(|impl_def| find_impl_block_end(impl_def, &mut buf))
508         .unwrap_or_else(|| {
509             buf = generate_impl_text(adt, &buf);
510             adt.syntax().text_range().end()
511         });
512
513     builder.insert(start_offset, buf);
514 }
515
516 #[derive(Debug)]
517 pub(crate) struct ReferenceConversion {
518     conversion: ReferenceConversionType,
519     ty: hir::Type,
520 }
521
522 #[derive(Debug)]
523 enum ReferenceConversionType {
524     // reference can be stripped if the type is Copy
525     Copy,
526     // &String -> &str
527     AsRefStr,
528     // &Vec<T> -> &[T]
529     AsRefSlice,
530     // &Box<T> -> &T
531     Dereferenced,
532     // &Option<T> -> Option<&T>
533     Option,
534     // &Result<T, E> -> Result<&T, &E>
535     Result,
536 }
537
538 impl ReferenceConversion {
539     pub(crate) fn convert_type(&self, db: &dyn HirDatabase) -> String {
540         match self.conversion {
541             ReferenceConversionType::Copy => self.ty.display(db).to_string(),
542             ReferenceConversionType::AsRefStr => "&str".to_string(),
543             ReferenceConversionType::AsRefSlice => {
544                 let type_argument_name =
545                     self.ty.type_arguments().next().unwrap().display(db).to_string();
546                 format!("&[{type_argument_name}]")
547             }
548             ReferenceConversionType::Dereferenced => {
549                 let type_argument_name =
550                     self.ty.type_arguments().next().unwrap().display(db).to_string();
551                 format!("&{type_argument_name}")
552             }
553             ReferenceConversionType::Option => {
554                 let type_argument_name =
555                     self.ty.type_arguments().next().unwrap().display(db).to_string();
556                 format!("Option<&{type_argument_name}>")
557             }
558             ReferenceConversionType::Result => {
559                 let mut type_arguments = self.ty.type_arguments();
560                 let first_type_argument_name =
561                     type_arguments.next().unwrap().display(db).to_string();
562                 let second_type_argument_name =
563                     type_arguments.next().unwrap().display(db).to_string();
564                 format!("Result<&{first_type_argument_name}, &{second_type_argument_name}>")
565             }
566         }
567     }
568
569     pub(crate) fn getter(&self, field_name: String) -> String {
570         match self.conversion {
571             ReferenceConversionType::Copy => format!("self.{field_name}"),
572             ReferenceConversionType::AsRefStr
573             | ReferenceConversionType::AsRefSlice
574             | ReferenceConversionType::Dereferenced
575             | ReferenceConversionType::Option
576             | ReferenceConversionType::Result => format!("self.{field_name}.as_ref()"),
577         }
578     }
579 }
580
581 // FIXME: It should return a new hir::Type, but currently constructing new types is too cumbersome
582 //        and all users of this function operate on string type names, so they can do the conversion
583 //        itself themselves.
584 pub(crate) fn convert_reference_type(
585     ty: hir::Type,
586     db: &RootDatabase,
587     famous_defs: &FamousDefs<'_, '_>,
588 ) -> Option<ReferenceConversion> {
589     handle_copy(&ty, db)
590         .or_else(|| handle_as_ref_str(&ty, db, famous_defs))
591         .or_else(|| handle_as_ref_slice(&ty, db, famous_defs))
592         .or_else(|| handle_dereferenced(&ty, db, famous_defs))
593         .or_else(|| handle_option_as_ref(&ty, db, famous_defs))
594         .or_else(|| handle_result_as_ref(&ty, db, famous_defs))
595         .map(|conversion| ReferenceConversion { ty, conversion })
596 }
597
598 fn handle_copy(ty: &hir::Type, db: &dyn HirDatabase) -> Option<ReferenceConversionType> {
599     ty.is_copy(db).then(|| ReferenceConversionType::Copy)
600 }
601
602 fn handle_as_ref_str(
603     ty: &hir::Type,
604     db: &dyn HirDatabase,
605     famous_defs: &FamousDefs<'_, '_>,
606 ) -> Option<ReferenceConversionType> {
607     let str_type = hir::BuiltinType::str().ty(db);
608
609     ty.impls_trait(db, famous_defs.core_convert_AsRef()?, &[str_type])
610         .then(|| ReferenceConversionType::AsRefStr)
611 }
612
613 fn handle_as_ref_slice(
614     ty: &hir::Type,
615     db: &dyn HirDatabase,
616     famous_defs: &FamousDefs<'_, '_>,
617 ) -> Option<ReferenceConversionType> {
618     let type_argument = ty.type_arguments().next()?;
619     let slice_type = hir::Type::new_slice(type_argument);
620
621     ty.impls_trait(db, famous_defs.core_convert_AsRef()?, &[slice_type])
622         .then(|| ReferenceConversionType::AsRefSlice)
623 }
624
625 fn handle_dereferenced(
626     ty: &hir::Type,
627     db: &dyn HirDatabase,
628     famous_defs: &FamousDefs<'_, '_>,
629 ) -> Option<ReferenceConversionType> {
630     let type_argument = ty.type_arguments().next()?;
631
632     ty.impls_trait(db, famous_defs.core_convert_AsRef()?, &[type_argument])
633         .then(|| ReferenceConversionType::Dereferenced)
634 }
635
636 fn handle_option_as_ref(
637     ty: &hir::Type,
638     db: &dyn HirDatabase,
639     famous_defs: &FamousDefs<'_, '_>,
640 ) -> Option<ReferenceConversionType> {
641     if ty.as_adt() == famous_defs.core_option_Option()?.ty(db).as_adt() {
642         Some(ReferenceConversionType::Option)
643     } else {
644         None
645     }
646 }
647
648 fn handle_result_as_ref(
649     ty: &hir::Type,
650     db: &dyn HirDatabase,
651     famous_defs: &FamousDefs<'_, '_>,
652 ) -> Option<ReferenceConversionType> {
653     if ty.as_adt() == famous_defs.core_result_Result()?.ty(db).as_adt() {
654         Some(ReferenceConversionType::Result)
655     } else {
656         None
657     }
658 }
659
660 pub(crate) fn get_methods(items: &ast::AssocItemList) -> Vec<ast::Fn> {
661     items
662         .assoc_items()
663         .flat_map(|i| match i {
664             ast::AssocItem::Fn(f) => Some(f),
665             _ => None,
666         })
667         .filter(|f| f.name().is_some())
668         .collect()
669 }
670
671 /// Trim(remove leading and trailing whitespace) `initial_range` in `source_file`, return the trimmed range.
672 pub(crate) fn trimmed_text_range(source_file: &SourceFile, initial_range: TextRange) -> TextRange {
673     let mut trimmed_range = initial_range;
674     while source_file
675         .syntax()
676         .token_at_offset(trimmed_range.start())
677         .find_map(Whitespace::cast)
678         .is_some()
679         && trimmed_range.start() < trimmed_range.end()
680     {
681         let start = trimmed_range.start() + TextSize::from(1);
682         trimmed_range = TextRange::new(start, trimmed_range.end());
683     }
684     while source_file
685         .syntax()
686         .token_at_offset(trimmed_range.end())
687         .find_map(Whitespace::cast)
688         .is_some()
689         && trimmed_range.start() < trimmed_range.end()
690     {
691         let end = trimmed_range.end() - TextSize::from(1);
692         trimmed_range = TextRange::new(trimmed_range.start(), end);
693     }
694     trimmed_range
695 }
696
697 /// Convert a list of function params to a list of arguments that can be passed
698 /// into a function call.
699 pub(crate) fn convert_param_list_to_arg_list(list: ast::ParamList) -> ast::ArgList {
700     let mut args = vec![];
701     for param in list.params() {
702         if let Some(ast::Pat::IdentPat(pat)) = param.pat() {
703             if let Some(name) = pat.name() {
704                 let name = name.to_string();
705                 let expr = make::expr_path(make::ext::ident_path(&name));
706                 args.push(expr);
707             }
708         }
709     }
710     make::arg_list(args)
711 }