Skip to content

Commit eb72a9e

Browse files
committed
Simplify
1 parent 4453fd4 commit eb72a9e

File tree

12 files changed

+62
-53
lines changed

12 files changed

+62
-53
lines changed

src/tools/rust-analyzer/crates/hir-expand/src/files.rs

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//! Things to wrap other things in file ids.
2+
use std::borrow::Borrow;
3+
24
use either::Either;
35
use span::{
46
AstIdNode, ErasedFileAstId, FileAstId, FileId, FileRange, HirFileId, HirFileIdRepr,
@@ -76,6 +78,13 @@ impl<FileKind: Copy, T> InFileWrapper<FileKind, T> {
7678
pub fn as_ref(&self) -> InFileWrapper<FileKind, &T> {
7779
self.with_value(&self.value)
7880
}
81+
82+
pub fn borrow<U>(&self) -> InFileWrapper<FileKind, &U>
83+
where
84+
T: Borrow<U>,
85+
{
86+
self.with_value(self.value.borrow())
87+
}
7988
}
8089

8190
impl<FileKind: Copy, T: Clone> InFileWrapper<FileKind, &T> {
@@ -156,8 +165,13 @@ impl<FileId: Copy, N: AstNode> InFileWrapper<FileId, &N> {
156165
}
157166

158167
// region:specific impls
168+
impl<SN: Borrow<SyntaxNode>> InRealFile<SN> {
169+
pub fn file_range(&self) -> FileRange {
170+
FileRange { file_id: self.file_id, range: self.value.borrow().text_range() }
171+
}
172+
}
159173

160-
impl InFile<&SyntaxNode> {
174+
impl<SN: Borrow<SyntaxNode>> InFile<SN> {
161175
pub fn parent_ancestors_with_macros(
162176
self,
163177
db: &dyn db::ExpandDatabase,
@@ -172,7 +186,7 @@ impl InFile<&SyntaxNode> {
172186
.map(|node| node.parent())
173187
.transpose(),
174188
};
175-
std::iter::successors(succ(&self.cloned()), succ)
189+
std::iter::successors(succ(&self.borrow().cloned()), succ)
176190
}
177191

178192
pub fn ancestors_with_macros(
@@ -189,31 +203,31 @@ impl InFile<&SyntaxNode> {
189203
.map(|node| node.parent())
190204
.transpose(),
191205
};
192-
std::iter::successors(Some(self.cloned()), succ)
206+
std::iter::successors(Some(self.borrow().cloned()), succ)
207+
}
208+
209+
pub fn kind(&self) -> parser::SyntaxKind {
210+
self.value.borrow().kind()
211+
}
212+
213+
pub fn text_range(&self) -> TextRange {
214+
self.value.borrow().text_range()
193215
}
194216

195217
/// Falls back to the macro call range if the node cannot be mapped up fully.
196218
///
197219
/// For attributes and derives, this will point back to the attribute only.
198220
/// For the entire item use [`InFile::original_file_range_full`].
199221
pub fn original_file_range_rooted(self, db: &dyn db::ExpandDatabase) -> FileRange {
200-
self.map(SyntaxNode::text_range).original_node_file_range_rooted(db)
222+
self.borrow().map(SyntaxNode::text_range).original_node_file_range_rooted(db)
201223
}
202224

203225
/// Falls back to the macro call range if the node cannot be mapped up fully.
204226
pub fn original_file_range_with_macro_call_body(
205227
self,
206228
db: &dyn db::ExpandDatabase,
207229
) -> FileRange {
208-
self.map(SyntaxNode::text_range).original_node_file_range_with_macro_call_body(db)
209-
}
210-
211-
/// Attempts to map the syntax node back up its macro calls.
212-
pub fn original_file_range_opt(
213-
self,
214-
db: &dyn db::ExpandDatabase,
215-
) -> Option<(FileRange, SyntaxContextId)> {
216-
self.map(SyntaxNode::text_range).original_node_file_range_opt(db)
230+
self.borrow().map(SyntaxNode::text_range).original_node_file_range_with_macro_call_body(db)
217231
}
218232

219233
pub fn original_syntax_node_rooted(
@@ -224,16 +238,19 @@ impl InFile<&SyntaxNode> {
224238
// as we don't have node inputs otherwise and therefore can't find an `N` node in the input
225239
let file_id = match self.file_id.repr() {
226240
HirFileIdRepr::FileId(file_id) => {
227-
return Some(InRealFile { file_id, value: self.value.clone() })
241+
return Some(InRealFile { file_id, value: self.value.borrow().clone() })
228242
}
229243
HirFileIdRepr::MacroFile(m) if m.is_attr_macro(db) => m,
230244
_ => return None,
231245
};
232246

233-
let FileRange { file_id, range } =
234-
map_node_range_up_rooted(db, &db.expansion_span_map(file_id), self.value.text_range())?;
247+
let FileRange { file_id, range } = map_node_range_up_rooted(
248+
db,
249+
&db.expansion_span_map(file_id),
250+
self.value.borrow().text_range(),
251+
)?;
235252

236-
let kind = self.value.kind();
253+
let kind = self.kind();
237254
let value = db
238255
.parse(file_id)
239256
.syntax_node()
@@ -245,6 +262,16 @@ impl InFile<&SyntaxNode> {
245262
}
246263
}
247264

265+
impl InFile<&SyntaxNode> {
266+
/// Attempts to map the syntax node back up its macro calls.
267+
pub fn original_file_range_opt(
268+
self,
269+
db: &dyn db::ExpandDatabase,
270+
) -> Option<(FileRange, SyntaxContextId)> {
271+
self.borrow().map(SyntaxNode::text_range).original_node_file_range_opt(db)
272+
}
273+
}
274+
248275
impl InMacroFile<SyntaxToken> {
249276
pub fn upmap_once(
250277
self,

src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ impl SourceAnalyzer {
9696
None => scope_for(db, &scopes, &source_map, node),
9797
Some(offset) => {
9898
debug_assert!(
99-
node.value.text_range().contains_inclusive(offset),
99+
node.text_range().contains_inclusive(offset),
100100
"{:?} not in {:?}",
101101
offset,
102-
node.value.text_range()
102+
node.text_range()
103103
);
104104
scope_for_offset(db, &scopes, &source_map, node.file_id, offset)
105105
}
@@ -966,9 +966,7 @@ fn scope_for(
966966
node: InFile<&SyntaxNode>,
967967
) -> Option<ScopeId> {
968968
node.ancestors_with_macros(db.upcast())
969-
.take_while(|it| {
970-
!ast::Item::can_cast(it.value.kind()) || ast::MacroCall::can_cast(it.value.kind())
971-
})
969+
.take_while(|it| !ast::Item::can_cast(it.kind()) || ast::MacroCall::can_cast(it.kind()))
972970
.filter_map(|it| it.map(ast::Expr::cast).transpose())
973971
.filter_map(|it| source_map.node_expr(it.as_ref()))
974972
.find_map(|it| scopes.scope_for(it))
@@ -996,8 +994,8 @@ fn scope_for_offset(
996994
Some(it.file_id.macro_file()?.call_node(db.upcast()))
997995
})
998996
.find(|it| it.file_id == from_file)
999-
.filter(|it| it.value.kind() == SyntaxKind::MACRO_CALL)?;
1000-
Some((source.value.text_range(), scope))
997+
.filter(|it| it.kind() == SyntaxKind::MACRO_CALL)?;
998+
Some((source.text_range(), scope))
1001999
})
10021000
.filter(|(expr_range, _scope)| expr_range.start() <= offset && offset <= expr_range.end())
10031001
// find containing scope

src/tools/rust-analyzer/crates/hir/src/term_search/tactics.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,9 +598,8 @@ pub(super) fn famous_types<'a, DB: HirDatabase>(
598598
Expr::FamousType { ty: Type::new(db, module.id, TyBuilder::unit()), value: "()" },
599599
]
600600
.into_iter()
601-
.map(|exprs| {
601+
.inspect(|exprs| {
602602
lookup.insert(exprs.ty(db), std::iter::once(exprs.clone()));
603-
exprs
604603
})
605604
.filter(|expr| expr.ty(db).could_unify_with_deeply(db, &ctx.goal))
606605
}

src/tools/rust-analyzer/crates/ide-db/src/rust_doc.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ pub fn is_rust_fence(s: &str) -> bool {
77
let mut seen_rust_tags = false;
88
let mut seen_other_tags = false;
99

10-
let tokens = s
11-
.trim()
12-
.split(|c| c == ',' || c == ' ' || c == '\t')
13-
.map(str::trim)
14-
.filter(|t| !t.is_empty());
10+
let tokens = s.trim().split([',', ' ', '\t']).map(str::trim).filter(|t| !t.is_empty());
1511

1612
for token in tokens {
1713
match token {

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/trait_impl_redundant_assoc_item.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,23 @@ pub(crate) fn trait_impl_redundant_assoc_item(
2828
let function = id;
2929
(
3030
format!("`fn {redundant_assoc_item_name}`"),
31-
function
32-
.source(db)
33-
.map(|it| it.syntax().value.text_range())
34-
.unwrap_or(default_range),
31+
function.source(db).map(|it| it.syntax().text_range()).unwrap_or(default_range),
3532
format!("\n {};", function.display(db)),
3633
)
3734
}
3835
hir::AssocItem::Const(id) => {
3936
let constant = id;
4037
(
4138
format!("`const {redundant_assoc_item_name}`"),
42-
constant
43-
.source(db)
44-
.map(|it| it.syntax().value.text_range())
45-
.unwrap_or(default_range),
39+
constant.source(db).map(|it| it.syntax().text_range()).unwrap_or(default_range),
4640
format!("\n {};", constant.display(db)),
4741
)
4842
}
4943
hir::AssocItem::TypeAlias(id) => {
5044
let type_alias = id;
5145
(
5246
format!("`type {redundant_assoc_item_name}`"),
53-
type_alias
54-
.source(db)
55-
.map(|it| it.syntax().value.text_range())
56-
.unwrap_or(default_range),
47+
type_alias.source(db).map(|it| it.syntax().text_range()).unwrap_or(default_range),
5748
format!("\n type {};", type_alias.name(ctx.sema.db).to_smol_str()),
5849
)
5950
}

src/tools/rust-analyzer/crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ impl flags::AnalysisStats {
651651
if let Some(src) = source {
652652
let original_file = src.file_id.original_file(db);
653653
let path = vfs.file_path(original_file);
654-
let syntax_range = src.value.text_range();
654+
let syntax_range = src.text_range();
655655
format!("processing: {} ({} {:?})", full_name(), path, syntax_range)
656656
} else {
657657
format!("processing: {}", full_name())
@@ -945,7 +945,7 @@ impl flags::AnalysisStats {
945945
if let Some(src) = source {
946946
let original_file = src.file_id.original_file(db);
947947
let path = vfs.file_path(original_file);
948-
let syntax_range = src.value.text_range();
948+
let syntax_range = src.text_range();
949949
format!("processing: {} ({} {:?})", full_name(), path, syntax_range)
950950
} else {
951951
format!("processing: {}", full_name())

src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3367,7 +3367,7 @@ mod tests {
33673367
for idx in url_offsets {
33683368
let link = &schema[idx..];
33693369
// matching on whitespace to ignore normal links
3370-
if let Some(link_end) = link.find(|c| c == ' ' || c == '[') {
3370+
if let Some(link_end) = link.find([' ', '[']) {
33713371
if link.chars().nth(link_end) == Some('[') {
33723372
if let Some(link_text_end) = link.find(']') {
33733373
let link_text = link[link_end..(link_text_end + 1)].to_string();

src/tools/rust-analyzer/crates/rust-analyzer/tests/slow-tests/support.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,8 @@ impl Server {
388388
}
389389
fn recv(&self) -> Result<Option<Message>, Timeout> {
390390
let msg = recv_timeout(&self.client.receiver)?;
391-
let msg = msg.map(|msg| {
391+
let msg = msg.inspect(|msg| {
392392
self.messages.borrow_mut().push(msg.clone());
393-
msg
394393
});
395394
Ok(msg)
396395
}

src/tools/rust-analyzer/crates/rust-analyzer/tests/slow-tests/tidy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ Zlib OR Apache-2.0 OR MIT
155155

156156
let meta = cmd!(sh, "cargo metadata --format-version 1").read().unwrap();
157157
let mut licenses = meta
158-
.split(|c| c == ',' || c == '{' || c == '}')
158+
.split([',', '{', '}'])
159159
.filter(|it| it.contains(r#""license""#))
160160
.map(|it| it.trim())
161161
.map(|it| it[r#""license":"#.len()..].trim_matches('"'))

src/tools/rust-analyzer/crates/test-utils/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ fn extract_line_annotations(mut line: &str) -> Vec<LineAnnotation> {
305305
}
306306
let range = TextRange::at(offset, len.try_into().unwrap());
307307
let line_no_caret = &line[len..];
308-
let end_marker = line_no_caret.find(|c| c == '$');
308+
let end_marker = line_no_caret.find('$');
309309
let next = line_no_caret.find(marker).map_or(line.len(), |it| it + len);
310310

311311
let cond = |end_marker| {

0 commit comments

Comments
 (0)