a number of code simplifications

This commit is contained in:
Daniel Eades 2023-01-10 18:48:51 +00:00
parent 3987c835f2
commit ac3844a0bb
13 changed files with 99 additions and 124 deletions

View file

@ -80,7 +80,7 @@ impl<'a> Write for Printer<'a> {
fn write_str(&mut self, s: &str) -> fmt::Result {
for line in s.split_inclusive('\n') {
if self.needs_indent {
match self.buf.chars().rev().skip_while(|ch| *ch == ' ').next() {
match self.buf.chars().rev().find(|ch| *ch != ' ') {
Some('\n') | None => {}
_ => self.buf.push('\n'),
}

View file

@ -1600,8 +1600,7 @@ impl ModCollector<'_, '_> {
FunctionLoc { container, id: ItemTreeId::new(self.tree_id, id) }.intern(db);
let vis = resolve_vis(def_map, &self.item_tree[it.visibility]);
if self.def_collector.is_proc_macro {
if self.module_id == def_map.root {
if self.def_collector.is_proc_macro && self.module_id == def_map.root {
if let Some(proc_macro) = attrs.parse_proc_macro_decl(&it.name) {
let crate_root = def_map.module_id(def_map.root);
self.def_collector.export_proc_macro(
@ -1612,7 +1611,6 @@ impl ModCollector<'_, '_> {
);
}
}
}
update_def(self.def_collector, fn_id.into(), &it.name, vis, false);
}

View file

@ -472,8 +472,8 @@ impl Module {
let def_map = self.id.def_map(db.upcast());
let children = def_map[self.id.local_id]
.children
.iter()
.map(|(_, module_id)| Module { id: def_map.module_id(*module_id) })
.values()
.map(|module_id| Module { id: def_map.module_id(*module_id) })
.collect::<Vec<_>>();
children.into_iter()
}

View file

@ -82,18 +82,18 @@ fn generate_trait_impl_text_from_impl(impl_: &ast::Impl, trait_text: &str, code:
let generic_params = impl_.generic_param_list().map(|generic_params| {
let lifetime_params =
generic_params.lifetime_params().map(ast::GenericParam::LifetimeParam);
let ty_or_const_params = generic_params.type_or_const_params().filter_map(|param| {
let ty_or_const_params = generic_params.type_or_const_params().map(|param| {
// remove defaults since they can't be specified in impls
match param {
ast::TypeOrConstParam::Type(param) => {
let param = param.clone_for_update();
param.remove_default();
Some(ast::GenericParam::TypeParam(param))
ast::GenericParam::TypeParam(param)
}
ast::TypeOrConstParam::Const(param) => {
let param = param.clone_for_update();
param.remove_default();
Some(ast::GenericParam::ConstParam(param))
ast::GenericParam::ConstParam(param)
}
}
});

View file

@ -92,7 +92,7 @@ trait Merge: AstNode + Clone {
fn try_merge_from(self, items: &mut dyn Iterator<Item = Self>) -> Option<Vec<Edit>> {
let mut edits = Vec::new();
let mut merged = self.clone();
while let Some(item) = items.next() {
for item in items {
merged = merged.try_merge(&item)?;
edits.push(Edit::Remove(item.into_either()));
}

View file

@ -86,8 +86,7 @@ pub(crate) fn unmerge_match_arm(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
it.prev_sibling_or_token()
})
.map(|it| it.kind())
.skip_while(|it| it.is_trivia())
.next()
.find(|it| !it.is_trivia())
== Some(T![,]);
let has_arms_after = neighbor(&match_arm, Direction::Next).is_some();
if !has_comma_after && !has_arms_after {

View file

@ -334,13 +334,11 @@ pub fn source_edit_from_references(
}
_ => false,
};
if !has_emitted_edit {
if !edited_ranges.contains(&range.start()) {
if !has_emitted_edit && !edited_ranges.contains(&range.start()) {
edit.replace(range, new_name.to_string());
edited_ranges.push(range.start());
}
}
}
edit.finish()
}

View file

@ -494,13 +494,11 @@ impl<'a> FindUsages<'a> {
}
// Search for `super` and `crate` resolving to our module
match self.def {
Definition::Module(module) => {
let scope = search_scope
.intersection(&SearchScope::module_and_children(self.sema.db, module));
if let Definition::Module(module) = self.def {
let scope =
search_scope.intersection(&SearchScope::module_and_children(self.sema.db, module));
let is_crate_root =
module.is_crate_root(self.sema.db).then(|| Finder::new("crate"));
let is_crate_root = module.is_crate_root(self.sema.db).then(|| Finder::new("crate"));
let finder = &Finder::new("super");
for (text, file_id, search_range) in scope_files(sema, &scope) {
@ -528,8 +526,6 @@ impl<'a> FindUsages<'a> {
}
}
}
_ => (),
}
// search for module `self` references in our module's definition source
match self.def {

View file

@ -99,8 +99,7 @@ pub(crate) fn json_in_items(
&& node.last_token().map(|x| x.kind()) == Some(SyntaxKind::R_CURLY)
{
let node_string = node.to_string();
if let Ok(it) = serde_json::from_str(&node_string) {
if let serde_json::Value::Object(it) = it {
if let Ok(serde_json::Value::Object(it)) = serde_json::from_str(&node_string) {
let import_scope = ImportScope::find_insert_use_container(node, sema)?;
let range = node.text_range();
let mut edit = TextEdit::builder();
@ -139,11 +138,7 @@ pub(crate) fn json_in_items(
config.insert_use.prefix_kind,
config.prefer_no_std,
) {
insert_use(
&scope,
mod_path_to_ast(&it),
&config.insert_use,
);
insert_use(&scope, mod_path_to_ast(&it), &config.insert_use);
}
}
}
@ -155,11 +150,7 @@ pub(crate) fn json_in_items(
config.insert_use.prefix_kind,
config.prefer_no_std,
) {
insert_use(
&scope,
mod_path_to_ast(&it),
&config.insert_use,
);
insert_use(&scope, mod_path_to_ast(&it), &config.insert_use);
}
}
}
@ -170,7 +161,6 @@ pub(crate) fn json_in_items(
);
}
}
}
Some(())
})();
}

View file

@ -11,10 +11,7 @@ pub(crate) fn private_assoc_item(
d: &hir::PrivateAssocItem,
) -> Diagnostic {
// FIXME: add quickfix
let name = match d.item.name(ctx.sema.db) {
Some(name) => format!("`{}` ", name),
None => String::new(),
};
let name = d.item.name(ctx.sema.db).map(|name| format!("`{name}` ")).unwrap_or_default();
Diagnostic::new(
"private-assoc-item",
format!(

View file

@ -34,10 +34,7 @@ pub(crate) fn unresolved_proc_macro(
let message = format!(
"{message}: {}",
if config_enabled {
match def_map.proc_macro_loading_error() {
Some(e) => e,
None => "proc macro not found in the built dylib",
}
def_map.proc_macro_loading_error().unwrap_or("proc macro not found in the built dylib")
} else {
match d.kind {
hir::MacroKind::Attr if proc_macros_enabled => {

View file

@ -212,7 +212,7 @@ fn expand_var(ctx: &mut ExpandCtx<'_>, v: &SmolStr, id: tt::TokenId) -> ExpandRe
} else {
ctx.bindings.get(v, &mut ctx.nesting).map_or_else(
|e| ExpandResult { value: Fragment::Tokens(tt::TokenTree::empty()), err: Some(e) },
|it| ExpandResult::ok(it),
ExpandResult::ok,
)
}
}

View file

@ -366,11 +366,11 @@ impl ProjectWorkspace {
_ => None,
})
.collect();
let ref mut outputs = match WorkspaceBuildScripts::run_once(config, &cargo_ws, progress) {
let outputs = &mut (match WorkspaceBuildScripts::run_once(config, &cargo_ws, progress) {
Ok(it) => Ok(it.into_iter()),
// io::Error is not Clone?
Err(e) => Err(Arc::new(e)),
};
});
workspaces
.iter()