From 9915103c9e2de4d12aa6cefe7d96ac5095b3719c Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 14 Dec 2021 12:38:20 +0100 Subject: [PATCH 1/2] Simplify --- crates/hir/src/source_analyzer.rs | 16 ++++++++-------- crates/ide_db/src/defs.rs | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs index 880596b37c..79163ad4d0 100644 --- a/crates/hir/src/source_analyzer.rs +++ b/crates/hir/src/source_analyzer.rs @@ -291,15 +291,12 @@ impl SourceAnalyzer { } } - if let Some(pat) = parent() - .and_then(ast::RecordPat::cast) - .map(ast::Pat::from) - .or_else(|| parent().and_then(ast::TupleStructPat::cast).map(ast::Pat::from)) - { + let record_pat = parent().and_then(ast::RecordPat::cast).map(ast::Pat::from); + let tuple_struct_pat = || parent().and_then(ast::TupleStructPat::cast).map(ast::Pat::from); + if let Some(pat) = record_pat.or_else(tuple_struct_pat) { let pat_id = self.pat_id(&pat)?; - if let Some(VariantId::EnumVariantId(variant)) = - self.infer.as_ref()?.variant_resolution_for_pat(pat_id) - { + let variant_res_for_pat = self.infer.as_ref()?.variant_resolution_for_pat(pat_id); + if let Some(VariantId::EnumVariantId(variant)) = variant_res_for_pat { return Some(PathResolution::Def(ModuleDef::Variant(variant.into()))); } } @@ -335,6 +332,9 @@ impl SourceAnalyzer { } } } else if is_path_of_attr { + // Case where we are resolving the final path segment of a path in an attribute + // in this case we have to check for inert/builtin attributes and tools and prioritize + // resolution of attributes over other namesapces let name_ref = path.as_single_name_ref(); let builtin = name_ref.as_ref().map(ast::NameRef::text).as_deref().and_then(BuiltinAttr::by_name); diff --git a/crates/ide_db/src/defs.rs b/crates/ide_db/src/defs.rs index ef2f0f940f..30ebf7828a 100644 --- a/crates/ide_db/src/defs.rs +++ b/crates/ide_db/src/defs.rs @@ -447,7 +447,7 @@ impl NameRefClass { } } - if let Some(path) = name_ref.syntax().ancestors().find_map(ast::Path::cast) { + if let Some(path) = ast::PathSegment::cast(parent.clone()).map(|it| it.parent_path()) { if path.qualifier().is_none() { if let Some(macro_call) = path.syntax().parent().and_then(ast::MacroCall::cast) { // Only use this to resolve single-segment macro calls like `foo!()`. Multi-segment From 901c7c7277a44f9fce6c44a2854fed5816a43cc4 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 14 Dec 2021 12:44:31 +0100 Subject: [PATCH 2/2] `match_ast!` takes a pattern to allow underscore usage --- .../replace_qualified_name_with_use.rs | 4 +-- crates/ide_completion/src/context.rs | 28 ++++++++-------- crates/ide_completion/src/patterns.rs | 32 +++++++++---------- crates/ide_db/src/search.rs | 2 +- crates/syntax/src/lib.rs | 2 +- crates/syntax/src/validation.rs | 2 +- 6 files changed, 35 insertions(+), 35 deletions(-) diff --git a/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs b/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs index 6b196681ca..2c540dc80d 100644 --- a/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs @@ -102,10 +102,10 @@ fn shorten_paths(node: &SyntaxNode, path: &ast::Path) { match child { // Don't modify `use` items, as this can break the `use` item when injecting a new // import into the use tree. - ast::Use(_it) => continue, + ast::Use(_) => continue, // Don't descend into submodules, they don't have the same `use` items in scope. // FIXME: This isn't true due to `super::*` imports? - ast::Module(_it) => continue, + ast::Module(_) => continue, ast::Path(p) => if maybe_replace_path(p.clone(), path.clone()).is_none() { shorten_paths(p.syntax(), path); }, diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index d459e51161..c6af285e86 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs @@ -517,7 +517,7 @@ impl<'a> CompletionContext<'a> { (ty, name) }, - ast::ArgList(_it) => { + ast::ArgList(_) => { cov_mark::hit!(expected_type_fn_param); ActiveParameter::at_token( &self.sema, @@ -608,9 +608,9 @@ impl<'a> CompletionContext<'a> { .map(|c| (Some(c.return_type()), None)) .unwrap_or((None, None)) }, - ast::ParamList(__) => (None, None), - ast::Stmt(__) => (None, None), - ast::Item(__) => (None, None), + ast::ParamList(_) => (None, None), + ast::Stmt(_) => (None, None), + ast::Item(_) => (None, None), _ => { match node.parent() { Some(n) => { @@ -702,10 +702,10 @@ impl<'a> CompletionContext<'a> { Some(match_ast! { match parent { - ast::LifetimeParam(_it) => LifetimeContext::LifetimeParam(sema.find_node_at_offset_with_macros(original_file, offset)), - ast::BreakExpr(_it) => LifetimeContext::LabelRef, - ast::ContinueExpr(_it) => LifetimeContext::LabelRef, - ast::Label(_it) => LifetimeContext::LabelDef, + ast::LifetimeParam(_) => LifetimeContext::LifetimeParam(sema.find_node_at_offset_with_macros(original_file, offset)), + ast::BreakExpr(_) => LifetimeContext::LabelRef, + ast::ContinueExpr(_) => LifetimeContext::LabelRef, + ast::Label(_) => LifetimeContext::LabelDef, _ => LifetimeContext::Lifetime, } }) @@ -753,7 +753,7 @@ impl<'a> CompletionContext<'a> { path_ctx.kind = path.syntax().ancestors().find_map(|it| { match_ast! { match it { - ast::PathType(_it) => Some(PathKind::Type), + ast::PathType(_) => Some(PathKind::Type), ast::PathExpr(it) => { path_ctx.has_call_parens = it.syntax().parent().map_or(false, |it| ast::CallExpr::can_cast(it.kind())); Some(PathKind::Expr) @@ -772,9 +772,9 @@ impl<'a> CompletionContext<'a> { Some(PathKind::Pat) }, ast::MacroCall(it) => it.excl_token().and(Some(PathKind::Mac)), - ast::Meta(_it) => Some(PathKind::Attr), + ast::Meta(_) => Some(PathKind::Attr), ast::Visibility(it) => Some(PathKind::Vis { has_in_token: it.in_token().is_some() }), - ast::UseTree(_it) => Some(PathKind::Use), + ast::UseTree(_) => Some(PathKind::Use), _ => None, } } @@ -851,9 +851,9 @@ fn pattern_context_for(pat: ast::Pat) -> PatternContext { }); return (PatternRefutability::Irrefutable, param.ty().is_some()) }, - ast::MatchArm(__) => PatternRefutability::Refutable, - ast::Condition(__) => PatternRefutability::Refutable, - ast::ForExpr(__) => PatternRefutability::Irrefutable, + ast::MatchArm(_) => PatternRefutability::Refutable, + ast::Condition(_) => PatternRefutability::Refutable, + ast::ForExpr(_) => PatternRefutability::Irrefutable, _ => PatternRefutability::Irrefutable, } }; diff --git a/crates/ide_completion/src/patterns.rs b/crates/ide_completion/src/patterns.rs index b50f76b911..41e2423f46 100644 --- a/crates/ide_completion/src/patterns.rs +++ b/crates/ide_completion/src/patterns.rs @@ -100,7 +100,7 @@ pub(crate) fn determine_prev_sibling(name_like: &ast::NameLike) -> Option ImmediatePrevSibling::Visibility, + ast::Visibility(_) => ImmediatePrevSibling::Visibility, _ => return None, } }; @@ -112,7 +112,7 @@ pub(crate) fn determine_prev_sibling(name_like: &ast::NameLike) -> Option ImmediatePrevSibling::IfExpr, + ast::IfExpr(_) => ImmediatePrevSibling::IfExpr, _ => return None, } } @@ -128,7 +128,7 @@ pub(crate) fn determine_prev_sibling(name_like: &ast::NameLike) -> Option ImmediatePrevSibling::Attribute, + ast::Attr(_) => ImmediatePrevSibling::Attribute, _ => return None, } }; @@ -200,31 +200,31 @@ pub(crate) fn determine_location( let res = match_ast! { match parent { - ast::IdentPat(_it) => ImmediateLocation::IdentPat, - ast::Rename(_it) => ImmediateLocation::Rename, - ast::StmtList(_it) => ImmediateLocation::StmtList, - ast::SourceFile(_it) => ImmediateLocation::ItemList, - ast::ItemList(_it) => ImmediateLocation::ItemList, - ast::RefExpr(_it) => ImmediateLocation::RefExpr, - ast::Variant(_it) => ImmediateLocation::Variant, + ast::IdentPat(_) => ImmediateLocation::IdentPat, + ast::Rename(_) => ImmediateLocation::Rename, + ast::StmtList(_) => ImmediateLocation::StmtList, + ast::SourceFile(_) => ImmediateLocation::ItemList, + ast::ItemList(_) => ImmediateLocation::ItemList, + ast::RefExpr(_) => ImmediateLocation::RefExpr, + ast::Variant(_) => ImmediateLocation::Variant, ast::RecordField(it) => if it.ty().map_or(false, |it| it.syntax().text_range().contains(offset)) { return None; } else { ImmediateLocation::RecordField }, - ast::RecordExprFieldList(_it) => sema + ast::RecordExprFieldList(_) => sema .find_node_at_offset_with_macros(original_file, offset) .map(ImmediateLocation::RecordExprUpdate)?, - ast::TupleField(_it) => ImmediateLocation::TupleField, - ast::TupleFieldList(_it) => ImmediateLocation::TupleField, - ast::TypeBound(_it) => ImmediateLocation::TypeBound, - ast::TypeBoundList(_it) => ImmediateLocation::TypeBound, + ast::TupleField(_) => ImmediateLocation::TupleField, + ast::TupleFieldList(_) => ImmediateLocation::TupleField, + ast::TypeBound(_) => ImmediateLocation::TypeBound, + ast::TypeBoundList(_) => ImmediateLocation::TypeBound, ast::AssocItemList(it) => match it.syntax().parent().map(|it| it.kind()) { Some(IMPL) => ImmediateLocation::Impl, Some(TRAIT) => ImmediateLocation::Trait, _ => return None, }, - ast::GenericArgList(_it) => sema + ast::GenericArgList(_) => sema .find_node_at_offset_with_macros(original_file, offset) .map(ImmediateLocation::GenericArgList)?, ast::Module(it) => { diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index c23a9ee257..0d0f7b78d8 100644 --- a/crates/ide_db/src/search.rs +++ b/crates/ide_db/src/search.rs @@ -657,7 +657,7 @@ impl ReferenceCategory { let mode = r.syntax().ancestors().find_map(|node| { match_ast! { - match (node) { + match node { ast::BinExpr(expr) => { if matches!(expr.op_kind()?, ast::BinaryOp::Assignment { .. }) { // If the variable or field ends on the LHS's end then it's a Write (covers fields and locals). diff --git a/crates/syntax/src/lib.rs b/crates/syntax/src/lib.rs index 24a099cb3c..ec281625a6 100644 --- a/crates/syntax/src/lib.rs +++ b/crates/syntax/src/lib.rs @@ -242,7 +242,7 @@ macro_rules! match_ast { (match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) }; (match ($node:expr) { - $( ast::$ast:ident($it:ident) => $res:expr, )* + $( ast::$ast:ident($it:pat) => $res:expr, )* _ => $catch_all:expr $(,)? }) => {{ $( if let Some($it) = ast::$ast::cast($node.clone()) { $res } else )* diff --git a/crates/syntax/src/validation.rs b/crates/syntax/src/validation.rs index a0a0bab5b3..8dc47e0bd3 100644 --- a/crates/syntax/src/validation.rs +++ b/crates/syntax/src/validation.rs @@ -275,7 +275,7 @@ fn validate_path_keywords(segment: ast::PathSegment, errors: &mut Vec continue, + ast::UseTreeList(_) => continue, ast::Path(parent) => path = parent, _ => return None, }