11566: minor: Make hir::Local::name infallible r=Veykril a=Veykril

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2022-02-26 15:48:11 +00:00 committed by GitHub
commit a153b18d6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 28 deletions

View file

@ -2047,10 +2047,13 @@ impl Local {
pub fn is_param(self, db: &dyn HirDatabase) -> bool {
let src = self.source(db);
match src.value {
Either::Left(bind_pat) => {
bind_pat.syntax().ancestors().any(|it| ast::Param::can_cast(it.kind()))
}
Either::Right(_self_param) => true,
Either::Left(pat) => pat
.syntax()
.ancestors()
.map(|it| it.kind())
.take_while(|&kind| ast::Pat::can_cast(kind) || ast::Param::can_cast(kind))
.any(ast::Param::can_cast),
Either::Right(_) => true,
}
}
@ -2061,17 +2064,19 @@ impl Local {
}
}
// FIXME: why is this an option? It shouldn't be?
pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
pub fn name(self, db: &dyn HirDatabase) -> Name {
let body = db.body(self.parent);
match &body[self.pat_id] {
Pat::Bind { name, .. } => Some(name.clone()),
_ => None,
Pat::Bind { name, .. } => name.clone(),
_ => {
stdx::never!("hir::Local is missing a name!");
Name::missing()
}
}
}
pub fn is_self(self, db: &dyn HirDatabase) -> bool {
self.name(db) == Some(name![self])
self.name(db) == name![self]
}
pub fn is_mut(self, db: &dyn HirDatabase) -> bool {

View file

@ -480,7 +480,7 @@ fn local(db: &RootDatabase, it: hir::Local) -> Option<Markup> {
let is_mut = if it.is_mut(db) { "mut " } else { "" };
let desc = match it.source(db).value {
Either::Left(ident) => {
let name = it.name(db).unwrap();
let name = it.name(db);
let let_kw = if ident
.syntax()
.parent()

View file

@ -390,10 +390,7 @@ impl ToNav for hir::Local {
let FileRange { file_id, range: full_range } =
InFile::new(file_id, node).original_file_range(db);
let name = match self.name(db) {
Some(it) => it.to_smol_str(),
None => "".into(),
};
let name = self.name(db).to_smol_str();
let kind = if self.is_self(db) {
SymbolKind::SelfParam
} else if self.is_param(db) {

View file

@ -235,10 +235,9 @@ fn highlight_name_ref(
let mut h = match name_class {
NameRefClass::Definition(def) => {
if let Definition::Local(local) = &def {
if let Some(name) = local.name(db) {
let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
*binding_hash = Some(calc_binding_hash(&name, *shadow_count))
}
let name = local.name(db);
let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
*binding_hash = Some(calc_binding_hash(&name, *shadow_count))
};
let mut h = highlight_def(sema, krate, def);
@ -288,11 +287,10 @@ fn highlight_name(
) -> Highlight {
let name_kind = NameClass::classify(sema, &name);
if let Some(NameClass::Definition(Definition::Local(local))) = &name_kind {
if let Some(name) = local.name(sema.db) {
let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
*shadow_count += 1;
*binding_hash = Some(calc_binding_hash(&name, *shadow_count))
}
let name = local.name(sema.db);
let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
*shadow_count += 1;
*binding_hash = Some(calc_binding_hash(&name, *shadow_count))
};
match name_kind {
Some(NameClass::Definition(def)) => {

View file

@ -393,7 +393,7 @@ impl Param {
}
fn to_param(&self, ctx: &AssistContext, module: hir::Module) -> ast::Param {
let var = self.var.name(ctx.db()).unwrap().to_string();
let var = self.var.name(ctx.db()).to_string();
let var_name = make::name(&var);
let pat = match self.kind() {
ParamKind::MutValue => make::ident_pat(false, true, var_name),
@ -1144,12 +1144,12 @@ fn make_call(ctx: &AssistContext, fun: &Function, indent: IndentLevel) -> String
match fun.outliving_locals.as_slice() {
[] => {}
[var] => {
format_to!(buf, "let {}{} = ", mut_modifier(var), var.local.name(ctx.db()).unwrap())
format_to!(buf, "let {}{} = ", mut_modifier(var), var.local.name(ctx.db()))
}
vars => {
buf.push_str("let (");
let bindings = vars.iter().format_with(", ", |local, f| {
f(&format_args!("{}{}", mut_modifier(local), local.local.name(ctx.db()).unwrap()))
f(&format_args!("{}{}", mut_modifier(local), local.local.name(ctx.db())))
});
format_to!(buf, "{}", bindings);
buf.push_str(") = ");
@ -1288,7 +1288,7 @@ impl FlowHandler {
}
fn path_expr_from_local(ctx: &AssistContext, var: Local) -> ast::Expr {
let name = var.name(ctx.db()).unwrap().to_string();
let name = var.name(ctx.db()).to_string();
make::expr_path(make::ext::ident_path(&name))
}

View file

@ -106,7 +106,7 @@ impl Definition {
Definition::TypeAlias(it) => it.name(db),
Definition::BuiltinType(it) => it.name(),
Definition::SelfType(_) => return None,
Definition::Local(it) => it.name(db)?,
Definition::Local(it) => it.name(db),
Definition::GenericParam(it) => it.name(db),
Definition::Label(it) => it.name(db),
Definition::BuiltinAttr(_) => return None, // FIXME