From 9c39363adabe0029815a709d9d6533c5c4f15023 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 19 Sep 2021 23:34:07 +0200 Subject: [PATCH] Simplify --- crates/base_db/src/fixture.rs | 4 ++-- crates/hir_ty/src/lower.rs | 15 +++++---------- crates/ide_ssr/src/lib.rs | 4 ++-- crates/ide_ssr/src/matching.rs | 4 ++-- crates/ide_ssr/src/nester.rs | 2 +- crates/ide_ssr/src/resolving.rs | 8 ++++---- crates/ide_ssr/src/search.rs | 5 ++--- crates/proc_macro_api/src/msg/flat.rs | 4 ++-- crates/syntax/src/validation.rs | 11 ++++------- 9 files changed, 24 insertions(+), 33 deletions(-) diff --git a/crates/base_db/src/fixture.rs b/crates/base_db/src/fixture.rs index 3526778b68..900032b4cb 100644 --- a/crates/base_db/src/fixture.rs +++ b/crates/base_db/src/fixture.rs @@ -218,7 +218,7 @@ impl ChangeFixture { ); roots.push(SourceRoot::new_library(fs)); - change.change_file(proc_lib_file, Some(Arc::new(String::from(source)))); + change.change_file(proc_lib_file, Some(Arc::new(source))); let all_crates = crate_graph.crates_in_topological_order(); @@ -275,7 +275,7 @@ pub fn input_replace(attr: TokenStream, _item: TokenStream) -> TokenStream { expander: Arc::new(AttributeInputReplaceProcMacroExpander), }, ]) - .filter(|pm| proc_macros.iter().any(|name| name == &pm.name)) + .filter(|pm| proc_macros.iter().any(|name| name == pm.name)) .collect(); (proc_macros, source.into()) } diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 28b21fc5b8..3734eb1013 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -307,17 +307,12 @@ impl<'a> TyLoweringContext<'a> { let mut expander = self.expander.borrow_mut(); if expander.is_some() { (Some(expander), false) + } else if let Some(module_id) = self.resolver.module() { + *expander = + Some(Expander::new(self.db.upcast(), macro_call.file_id, module_id)); + (Some(expander), true) } else { - if let Some(module_id) = self.resolver.module() { - *expander = Some(Expander::new( - self.db.upcast(), - macro_call.file_id, - module_id, - )); - (Some(expander), true) - } else { - (None, false) - } + (None, false) } }; let ty = if let Some(mut expander) = expander { diff --git a/crates/ide_ssr/src/lib.rs b/crates/ide_ssr/src/lib.rs index 264ea753ef..2fe1f5b616 100644 --- a/crates/ide_ssr/src/lib.rs +++ b/crates/ide_ssr/src/lib.rs @@ -172,7 +172,7 @@ impl<'db> MatchFinder<'db> { for m in self.matches().matches { matches_by_file .entry(m.range.file_id) - .or_insert_with(|| SsrMatches::default()) + .or_insert_with(SsrMatches::default) .matches .push(m); } @@ -331,7 +331,7 @@ impl SsrMatches { fn flatten_into(self, out: &mut SsrMatches) { for mut m in self.matches { for p in m.placeholder_values.values_mut() { - std::mem::replace(&mut p.inner_matches, SsrMatches::default()).flatten_into(out); + std::mem::take(&mut p.inner_matches).flatten_into(out); } out.matches.push(m); } diff --git a/crates/ide_ssr/src/matching.rs b/crates/ide_ssr/src/matching.rs index 004bbeec5b..6812947218 100644 --- a/crates/ide_ssr/src/matching.rs +++ b/crates/ide_ssr/src/matching.rs @@ -462,7 +462,7 @@ impl<'db, 'sema> Matcher<'db, 'sema> { let mut last_matched_token = child; // Read code tokens util we reach one equal to the next token from our pattern // or we reach the end of the token tree. - while let Some(next) = children.next() { + for next in &mut children { match &next { SyntaxElement::Token(t) => { if Some(t.to_string()) == next_pattern_token { @@ -763,7 +763,7 @@ impl Iterator for PatternIterator { type Item = SyntaxElement; fn next(&mut self) -> Option { - while let Some(element) = self.iter.next() { + for element in &mut self.iter { if !element.kind().is_trivia() { return Some(element); } diff --git a/crates/ide_ssr/src/nester.rs b/crates/ide_ssr/src/nester.rs index 6ac355dfc2..e734125ec8 100644 --- a/crates/ide_ssr/src/nester.rs +++ b/crates/ide_ssr/src/nester.rs @@ -65,7 +65,7 @@ fn try_add_sub_match(m: Match, existing: &mut Match, sema: &hir::Semantics ResolutionScope<'db> { use syntax::ast::AstNode; if let Some(path) = ast::Path::cast(path.clone()) { if let Some(qualifier) = path.qualifier() { - if let Some(resolved_qualifier) = self.resolve_path(&qualifier) { - if let hir::PathResolution::Def(hir::ModuleDef::Adt(adt)) = resolved_qualifier { - return Some(adt.ty(self.scope.db)); - } + if let Some(hir::PathResolution::Def(hir::ModuleDef::Adt(adt))) = + self.resolve_path(&qualifier) + { + return Some(adt.ty(self.scope.db)); } } } diff --git a/crates/ide_ssr/src/search.rs b/crates/ide_ssr/src/search.rs index f2056919ed..42ce85eebe 100644 --- a/crates/ide_ssr/src/search.rs +++ b/crates/ide_ssr/src/search.rs @@ -80,7 +80,7 @@ impl<'db> MatchFinder<'db> { if let Some(path) = self.sema.find_node_at_offset_with_descend::(file.syntax(), offset) { - self.sema.ancestors_with_macros(path.syntax().clone()).skip(depth).next() + self.sema.ancestors_with_macros(path.syntax().clone()).nth(depth) } else if let Some(path) = self.sema.find_node_at_offset_with_descend::(file.syntax(), offset) { @@ -96,8 +96,7 @@ impl<'db> MatchFinder<'db> { } self.sema .ancestors_with_macros(path.syntax().clone()) - .skip(depth - PATH_DEPTH_IN_CALL_EXPR) - .next() + .nth(depth - PATH_DEPTH_IN_CALL_EXPR) } else { None } diff --git a/crates/proc_macro_api/src/msg/flat.rs b/crates/proc_macro_api/src/msg/flat.rs index d427fa87d2..3201394f7f 100644 --- a/crates/proc_macro_api/src/msg/flat.rs +++ b/crates/proc_macro_api/src/msg/flat.rs @@ -246,7 +246,7 @@ impl<'a> Writer<'a> { fn enqueue(&mut self, subtree: &'a tt::Subtree) -> u32 { let idx = self.subtree.len(); - let delimiter_id = subtree.delimiter.map(|it| it.id).unwrap_or(TokenId::unspecified()); + let delimiter_id = subtree.delimiter.map(|it| it.id).unwrap_or_else(TokenId::unspecified); let delimiter_kind = subtree.delimiter.map(|it| it.kind); self.subtree.push(SubtreeRepr { id: delimiter_id, kind: delimiter_kind, tt: [!0, !0] }); self.work.push_back((idx, subtree)); @@ -320,7 +320,7 @@ impl Reader { }) .collect(), }; - res[i] = Some(s.into()) + res[i] = Some(s) } res[0].take().unwrap() diff --git a/crates/syntax/src/validation.rs b/crates/syntax/src/validation.rs index ea71da0422..8d1c6f5d26 100644 --- a/crates/syntax/src/validation.rs +++ b/crates/syntax/src/validation.rs @@ -211,13 +211,10 @@ fn validate_numeric_name(name_ref: Option, errors: &mut Vec) { - if vis.in_token().is_none() { - if vis.path().and_then(|p| p.as_single_name_ref()).and_then(|n| n.ident_token()).is_some() { - errors.push(SyntaxError::new( - "incorrect visibility restriction", - vis.syntax.text_range(), - )); - } + let path_without_in_token = vis.in_token().is_none() + && vis.path().and_then(|p| p.as_single_name_ref()).and_then(|n| n.ident_token()).is_some(); + if path_without_in_token { + errors.push(SyntaxError::new("incorrect visibility restriction", vis.syntax.text_range())); } let parent = match vis.syntax().parent() { Some(it) => it,