Skip to content

Commit 176f7f6

Browse files
bors[bot]matklad
andauthored
Merge #3923
3923: Cleanup keyword accessors r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents eb07803 + 30084a5 commit 176f7f6

File tree

16 files changed

+151
-1060
lines changed

16 files changed

+151
-1060
lines changed

crates/ra_assists/src/handlers/inline_local_variable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option<Assist> {
2929
ast::Pat::BindPat(pat) => pat,
3030
_ => return None,
3131
};
32-
if bind_pat.mut_kw_token().is_some() {
32+
if bind_pat.mut_token().is_some() {
3333
tested_by!(test_not_inline_mut_variable);
3434
return None;
3535
}

crates/ra_hir_def/src/body/lower.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ impl ExprCollector<'_> {
372372
}
373373
ast::Expr::RefExpr(e) => {
374374
let expr = self.collect_expr_opt(e.expr());
375-
let mutability = Mutability::from_mutable(e.is_mut());
375+
let mutability = Mutability::from_mutable(e.mut_token().is_some());
376376
self.alloc_expr(Expr::Ref { expr, mutability }, syntax_ptr)
377377
}
378378
ast::Expr::PrefixExpr(e) => {
@@ -587,10 +587,8 @@ impl ExprCollector<'_> {
587587
let pattern = match &pat {
588588
ast::Pat::BindPat(bp) => {
589589
let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
590-
let annotation = BindingAnnotation::new(
591-
bp.mut_kw_token().is_some(),
592-
bp.ref_kw_token().is_some(),
593-
);
590+
let annotation =
591+
BindingAnnotation::new(bp.mut_token().is_some(), bp.ref_token().is_some());
594592
let subpat = bp.pat().map(|subpat| self.collect_pat(subpat));
595593
if annotation == BindingAnnotation::Unannotated && subpat.is_none() {
596594
// This could also be a single-segment path pattern. To
@@ -631,7 +629,7 @@ impl ExprCollector<'_> {
631629
}
632630
ast::Pat::RefPat(p) => {
633631
let pat = self.collect_pat_opt(p.pat());
634-
let mutability = Mutability::from_mutable(p.mut_kw_token().is_some());
632+
let mutability = Mutability::from_mutable(p.mut_token().is_some());
635633
Pat::Ref { pat, mutability }
636634
}
637635
ast::Pat::PathPat(p) => {

crates/ra_hir_def/src/data.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl FunctionData {
7474
TypeRef::unit()
7575
};
7676

77-
let ret_type = if src.value.async_kw_token().is_some() {
77+
let ret_type = if src.value.async_token().is_some() {
7878
let future_impl = desugar_future_path(ret_type);
7979
let ty_bound = TypeBound::Path(future_impl);
8080
TypeRef::ImplTrait(vec![ty_bound])
@@ -135,7 +135,7 @@ impl TraitData {
135135
pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc<TraitData> {
136136
let src = tr.lookup(db).source(db);
137137
let name = src.value.name().map_or_else(Name::missing, |n| n.as_name());
138-
let auto = src.value.auto_kw_token().is_some();
138+
let auto = src.value.auto_token().is_some();
139139
let ast_id_map = db.ast_id_map(src.file_id);
140140

141141
let container = AssocContainerId::TraitId(tr);

crates/ra_hir_def/src/type_ref.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl TypeRef {
7777
}
7878
ast::TypeRef::PointerType(inner) => {
7979
let inner_ty = TypeRef::from_ast_opt(inner.type_ref());
80-
let mutability = Mutability::from_mutable(inner.mut_kw_token().is_some());
80+
let mutability = Mutability::from_mutable(inner.mut_token().is_some());
8181
TypeRef::RawPtr(Box::new(inner_ty), mutability)
8282
}
8383
ast::TypeRef::ArrayType(inner) => {
@@ -88,7 +88,7 @@ impl TypeRef {
8888
}
8989
ast::TypeRef::ReferenceType(inner) => {
9090
let inner_ty = TypeRef::from_ast_opt(inner.type_ref());
91-
let mutability = Mutability::from_mutable(inner.mut_kw_token().is_some());
91+
let mutability = Mutability::from_mutable(inner.mut_token().is_some());
9292
TypeRef::Reference(Box::new(inner_ty), mutability)
9393
}
9494
ast::TypeRef::PlaceholderType(_inner) => TypeRef::Placeholder,

crates/ra_hir_ty/src/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use insta::assert_snapshot;
2323
use ra_db::{fixture::WithFixture, salsa::Database, FilePosition, SourceDatabase};
2424
use ra_syntax::{
2525
algo,
26-
ast::{self, AstNode, AstToken},
26+
ast::{self, AstNode},
2727
};
2828
use stdx::format_to;
2929

@@ -101,7 +101,7 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
101101
let node = src_ptr.value.to_node(&src_ptr.file_syntax(&db));
102102

103103
let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node.clone()) {
104-
(self_param.self_kw_token().unwrap().syntax().text_range(), "self".to_string())
104+
(self_param.self_token().unwrap().text_range(), "self".to_string())
105105
} else {
106106
(src_ptr.value.range(), node.text().to_string().replace("\n", " "))
107107
};

crates/ra_ide/src/completion/complete_fn_param.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! FIXME: write short doc here
22
3-
use ra_syntax::{ast, match_ast, AstNode};
3+
use ra_syntax::{
4+
ast::{self, ModuleItemOwner},
5+
match_ast, AstNode,
6+
};
47
use rustc_hash::FxHashMap;
58

69
use crate::completion::{CompletionContext, CompletionItem, CompletionKind, Completions};
@@ -16,11 +19,19 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
1619

1720
let mut params = FxHashMap::default();
1821
for node in ctx.token.parent().ancestors() {
19-
match_ast! {
22+
let items = match_ast! {
2023
match node {
21-
ast::SourceFile(it) => process(it, &mut params),
22-
ast::ItemList(it) => process(it, &mut params),
23-
_ => (),
24+
ast::SourceFile(it) => it.items(),
25+
ast::ItemList(it) => it.items(),
26+
_ => continue,
27+
}
28+
};
29+
for item in items {
30+
if let ast::ModuleItem::FnDef(func) = item {
31+
func.param_list().into_iter().flat_map(|it| it.params()).for_each(|param| {
32+
let text = param.syntax().text().to_string();
33+
params.entry(text).or_insert((0, param)).0 += 1;
34+
})
2435
}
2536
}
2637
}
@@ -39,15 +50,6 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
3950
.lookup_by(lookup)
4051
.add_to(acc)
4152
});
42-
43-
fn process<N: ast::FnDefOwner>(node: N, params: &mut FxHashMap<String, (u32, ast::Param)>) {
44-
node.functions().filter_map(|it| it.param_list()).flat_map(|it| it.params()).for_each(
45-
|param| {
46-
let text = param.syntax().text().to_string();
47-
params.entry(text).or_insert((0, param)).0 += 1;
48-
},
49-
)
50-
}
5153
}
5254

5355
#[cfg(test)]

crates/ra_ide/src/completion/completion_context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ impl<'a> CompletionContext<'a> {
191191
if let Some(bind_pat) = name.syntax().ancestors().find_map(ast::BindPat::cast) {
192192
self.is_pat_binding_or_const = true;
193193
if bind_pat.at_token().is_some()
194-
|| bind_pat.ref_kw_token().is_some()
195-
|| bind_pat.mut_kw_token().is_some()
194+
|| bind_pat.ref_token().is_some()
195+
|| bind_pat.mut_token().is_some()
196196
{
197197
self.is_pat_binding_or_const = false;
198198
}

crates/ra_ide/src/references.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ fn decl_access(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> Optio
152152
if stmt.initializer().is_some() {
153153
let pat = stmt.pat()?;
154154
if let ast::Pat::BindPat(it) = pat {
155-
if it.mut_kw_token().is_some() {
155+
if it.mut_token().is_some() {
156156
return Some(ReferenceAccess::Write);
157157
}
158158
}

crates/ra_syntax/src/ast.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl<N: AstNode> Iterator for AstChildren<N> {
8080
}
8181

8282
mod support {
83-
use super::{AstChildren, AstNode, AstToken, SyntaxNode};
83+
use super::{AstChildren, AstNode, AstToken, SyntaxKind, SyntaxNode, SyntaxToken};
8484

8585
pub(super) fn child<N: AstNode>(parent: &SyntaxNode) -> Option<N> {
8686
parent.children().find_map(N::cast)
@@ -93,6 +93,10 @@ mod support {
9393
pub(super) fn token<T: AstToken>(parent: &SyntaxNode) -> Option<T> {
9494
parent.children_with_tokens().filter_map(|it| it.into_token()).find_map(T::cast)
9595
}
96+
97+
pub(super) fn token2(parent: &SyntaxNode, kind: SyntaxKind) -> Option<SyntaxToken> {
98+
parent.children_with_tokens().filter_map(|it| it.into_token()).find(|it| it.kind() == kind)
99+
}
96100
}
97101

98102
#[test]

crates/ra_syntax/src/ast/expr_extensions.rs

-10
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,6 @@ impl ast::IfExpr {
4848
}
4949
}
5050

51-
impl ast::RefExpr {
52-
pub fn is_mut(&self) -> bool {
53-
self.syntax().children_with_tokens().any(|n| n.kind() == T![mut])
54-
}
55-
56-
pub fn raw_token(&self) -> Option<SyntaxToken> {
57-
None // FIXME: implement &raw
58-
}
59-
}
60-
6151
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
6252
pub enum PrefixOp {
6353
/// The `*` operator for dereferencing

crates/ra_syntax/src/ast/extensions.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ pub enum SelfParamKind {
279279
impl ast::SelfParam {
280280
pub fn kind(&self) -> SelfParamKind {
281281
if self.amp_token().is_some() {
282-
if self.amp_mut_kw_token().is_some() {
282+
if self.amp_mut_token().is_some() {
283283
SelfParamKind::MutRef
284284
} else {
285285
SelfParamKind::Ref
@@ -290,21 +290,21 @@ impl ast::SelfParam {
290290
}
291291

292292
/// the "mut" in "mut self", not the one in "&mut self"
293-
pub fn mut_kw_token(&self) -> Option<ast::MutKw> {
293+
pub fn mut_token(&self) -> Option<SyntaxToken> {
294294
self.syntax()
295295
.children_with_tokens()
296296
.filter_map(|it| it.into_token())
297297
.take_while(|it| it.kind() != T![&])
298-
.find_map(ast::MutKw::cast)
298+
.find(|it| it.kind() == T![mut])
299299
}
300300

301301
/// the "mut" in "&mut self", not the one in "mut self"
302-
pub fn amp_mut_kw_token(&self) -> Option<ast::MutKw> {
302+
pub fn amp_mut_token(&self) -> Option<SyntaxToken> {
303303
self.syntax()
304304
.children_with_tokens()
305305
.filter_map(|it| it.into_token())
306306
.skip_while(|it| it.kind() != T![&])
307-
.find_map(ast::MutKw::cast)
307+
.find(|it| it.kind() == T![mut])
308308
}
309309
}
310310

@@ -340,7 +340,7 @@ impl ast::TypeBound {
340340
}
341341

342342
pub fn question_token(&self) -> Option<ast::Question> {
343-
if self.const_kw_token().is_some() {
343+
if self.const_token().is_some() {
344344
self.syntax()
345345
.children_with_tokens()
346346
.filter_map(|it| it.into_token())
@@ -364,11 +364,11 @@ impl ast::Visibility {
364364
pub fn kind(&self) -> VisibilityKind {
365365
if let Some(path) = support::children(self.syntax()).next() {
366366
VisibilityKind::In(path)
367-
} else if self.crate_kw_token().is_some() {
367+
} else if self.crate_token().is_some() {
368368
VisibilityKind::PubCrate
369-
} else if self.super_kw_token().is_some() {
369+
} else if self.super_token().is_some() {
370370
VisibilityKind::PubSuper
371-
} else if self.self_kw_token().is_some() {
371+
} else if self.self_token().is_some() {
372372
VisibilityKind::PubSuper
373373
} else {
374374
VisibilityKind::Pub

0 commit comments

Comments
 (0)