Skip to content

feat: highlight unsafe operations #19687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

gohome001
Copy link
Contributor

close #19467

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 25, 2025
@@ -89,6 +89,9 @@ pub(crate) fn highlight_related(
T![break] | T![loop] | T![while] | T![continue] if config.break_points => {
highlight_break_points(sema, token).remove(&file_id)
}
T![unsafe] if token.parent_ancestors().find_map(ast::BlockExpr::cast).is_some() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
T![unsafe] if token.parent_ancestors().find_map(ast::BlockExpr::cast).is_some() => {
T![unsafe] if token.parent().and_then(ast::BlockExpr::cast).is_some() => {

) -> FxHashMap<EditionedFileId, Vec<HighlightedRange>> {
fn hl(
sema: &Semantics<'_, RootDatabase>,
unsafe_token: Option<SyntaxToken>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
unsafe_token: Option<SyntaxToken>,
unsafe_token: SyntaxToken,

we already have the token here

Comment on lines +759 to +763
let Some(block_expr) = token.parent().and_then(ast::BlockExpr::cast) else {
return FxHashMap::default();
};

hl(sema, Some(token), Some(block_expr)).unwrap_or_default()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let Some(block_expr) = token.parent().and_then(ast::BlockExpr::cast) else {
return FxHashMap::default();
};
hl(sema, Some(token), Some(block_expr)).unwrap_or_default()
hl(sema, token, token.parent().and_then(ast::BlockExpr::cast)).unwrap_or_default()

Comment on lines +736 to +753
if let Some(node) = block.syntax().ancestors().find(|n| ast::Fn::can_cast(n.kind())) {
if let Some(function) = ast::Fn::cast(node) {
// highlight unsafe keyword of the function
if let Some(unsafe_token) = function.unsafe_token() {
push_to_highlights(unsafe_token_file_id, Some(unsafe_token.text_range()));
}
// highlight unsafe operations
if let Some(f) = sema.to_def(&function) {
let unsafe_ops = sema.get_unsafe_ops(f.into());
for unsafe_op in unsafe_ops {
push_to_highlights(
unsafe_op.file_id,
Some(unsafe_op.value.text_range()),
);
}
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if let Some(node) = block.syntax().ancestors().find(|n| ast::Fn::can_cast(n.kind())) {
if let Some(function) = ast::Fn::cast(node) {
// highlight unsafe keyword of the function
if let Some(unsafe_token) = function.unsafe_token() {
push_to_highlights(unsafe_token_file_id, Some(unsafe_token.text_range()));
}
// highlight unsafe operations
if let Some(f) = sema.to_def(&function) {
let unsafe_ops = sema.get_unsafe_ops(f.into());
for unsafe_op in unsafe_ops {
push_to_highlights(
unsafe_op.file_id,
Some(unsafe_op.value.text_range()),
);
}
}
}
}
if let Some(body) = sema.body_for(InFile::new(unsafe_token_file_id, block_expr.syntax())) {
let unsafe_ops = sema.get_unsafe_ops(f.into());
for unsafe_op in unsafe_ops {
push_to_highlights(unsafe_op.file_id, Some(unsafe_op.value.text_range()));
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Highlighting references feature should highlight unsafe ops
3 participants