]> git.lizzy.rs Git - rust.git/commitdiff
Merge #7115
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>
Sun, 3 Jan 2021 08:56:17 +0000 (08:56 +0000)
committerGitHub <noreply@github.com>
Sun, 3 Jan 2021 08:56:17 +0000 (08:56 +0000)
7115: Migrate HasSource::source to return Option r=matklad a=nick96

I've made a start on fixing #6913 based on the provided work plan, migrating `HasSource::source` to return an `Option`. The simple cases are migrated but there are a few that I'm unsure exactly how they should be handled:

- Logging the processing of functions in `AnalysisStatsCmd::run`: In verbose mode it includes the path to the module containing the function and the syntax range. I've handled this with an if-let but would it be better to blow up here with `expect`? I'm not 100% on the code paths but if we're processing a function definition then the source should exist.

I've handled `source()` in all code paths as `None` being a valid return value but are there some cases where we should just blow up? Also, all I've done is bubble up the returned `None`s, there may be some places where we can recover and still provide something.

Co-authored-by: Nick Spain <nicholas.spain@stileeducation.com>
Co-authored-by: Nick Spain <nicholas.spain96@gmail.com>
1  2 
crates/assists/src/utils.rs

index 5a6125534f8b3860c6e20b462a53e5c63e787856,7c159b5ba0417f635374e90cbc1fcf8601f92503..b055964465a6a3fedc38cb74db9591f0cfeafeec
@@@ -94,14 -94,18 +94,18 @@@ pub fn filter_assoc_items
              ast::AssocItem::MacroCall(_) => None,
          }
          .is_some()
 -    };
 +    }
  
      items
          .iter()
-         .map(|i| match i {
-             hir::AssocItem::Function(i) => ast::AssocItem::Fn(i.source(db).value),
-             hir::AssocItem::TypeAlias(i) => ast::AssocItem::TypeAlias(i.source(db).value),
-             hir::AssocItem::Const(i) => ast::AssocItem::Const(i.source(db).value),
+         // Note: This throws away items with no source.
+         .filter_map(|i| {
+             let item = match i {
+                 hir::AssocItem::Function(i) => ast::AssocItem::Fn(i.source(db)?.value),
+                 hir::AssocItem::TypeAlias(i) => ast::AssocItem::TypeAlias(i.source(db)?.value),
+                 hir::AssocItem::Const(i) => ast::AssocItem::Const(i.source(db)?.value),
+             };
+             Some(item)
          })
          .filter(has_def_name)
          .filter(|it| match it {