Skip to content

Commit c6e3a4c

Browse files
committed
minor: code review tweak
1 parent 1d4602e commit c6e3a4c

File tree

1 file changed

+8
-24
lines changed

1 file changed

+8
-24
lines changed

crates/ide/src/highlight_related.rs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub(crate) fn highlight_related(
8989
T![break] | T![loop] | T![while] | T![continue] if config.break_points => {
9090
highlight_break_points(sema, token).remove(&file_id)
9191
}
92-
T![unsafe] if token.parent_ancestors().find_map(ast::BlockExpr::cast).is_some() => {
92+
T![unsafe] if token.parent().and_then(ast::BlockExpr::cast).is_some() => {
9393
highlight_unsafe_points(sema, token).remove(&file_id)
9494
}
9595
T![|] if config.closure_captures => {
@@ -715,7 +715,7 @@ pub(crate) fn highlight_unsafe_points(
715715
) -> FxHashMap<EditionedFileId, Vec<HighlightedRange>> {
716716
fn hl(
717717
sema: &Semantics<'_, RootDatabase>,
718-
unsafe_token: Option<SyntaxToken>,
718+
unsafe_token: &SyntaxToken,
719719
block_expr: Option<ast::BlockExpr>,
720720
) -> Option<FxHashMap<EditionedFileId, Vec<HighlightedRange>>> {
721721
let mut highlights: FxHashMap<EditionedFileId, Vec<_>> = FxHashMap::default();
@@ -728,39 +728,23 @@ pub(crate) fn highlight_unsafe_points(
728728
};
729729

730730
// highlight unsafe keyword itself
731-
let unsafe_token = unsafe_token?;
732731
let unsafe_token_file_id = sema.hir_file_for(&unsafe_token.parent()?);
733732
push_to_highlights(unsafe_token_file_id, Some(unsafe_token.text_range()));
734733

734+
// highlight unsafe operations
735735
if let Some(block) = block_expr {
736-
if let Some(node) = block.syntax().ancestors().find(|n| ast::Fn::can_cast(n.kind())) {
737-
if let Some(function) = ast::Fn::cast(node) {
738-
// highlight unsafe keyword of the function
739-
if let Some(unsafe_token) = function.unsafe_token() {
740-
push_to_highlights(unsafe_token_file_id, Some(unsafe_token.text_range()));
741-
}
742-
// highlight unsafe operations
743-
if let Some(f) = sema.to_def(&function) {
744-
let unsafe_ops = sema.get_unsafe_ops(f.into());
745-
for unsafe_op in unsafe_ops {
746-
push_to_highlights(
747-
unsafe_op.file_id,
748-
Some(unsafe_op.value.text_range()),
749-
);
750-
}
751-
}
736+
if let Some(body) = sema.body_for(InFile::new(unsafe_token_file_id, block.syntax())) {
737+
let unsafe_ops = sema.get_unsafe_ops(body);
738+
for unsafe_op in unsafe_ops {
739+
push_to_highlights(unsafe_op.file_id, Some(unsafe_op.value.text_range()));
752740
}
753741
}
754742
}
755743

756744
Some(highlights)
757745
}
758746

759-
let Some(block_expr) = token.parent().and_then(ast::BlockExpr::cast) else {
760-
return FxHashMap::default();
761-
};
762-
763-
hl(sema, Some(token), Some(block_expr)).unwrap_or_default()
747+
hl(sema, &token, token.parent().and_then(ast::BlockExpr::cast)).unwrap_or_default()
764748
}
765749

766750
#[cfg(test)]

0 commit comments

Comments
 (0)