-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
highlight_unsafe_points(sema, token).remove(&file_id) | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
T![|] if config.closure_captures => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
highlight_closure_captures(sema, token, file_id, span_file_id.file_id()) | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -706,6 +709,60 @@ impl<'a> WalkExpandedExprCtx<'a> { | |||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
pub(crate) fn highlight_unsafe_points( | ||||||||||||||||||||||||||||||||||||||||||||||||||
sema: &Semantics<'_, RootDatabase>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
token: SyntaxToken, | ||||||||||||||||||||||||||||||||||||||||||||||||||
) -> FxHashMap<EditionedFileId, Vec<HighlightedRange>> { | ||||||||||||||||||||||||||||||||||||||||||||||||||
fn hl( | ||||||||||||||||||||||||||||||||||||||||||||||||||
sema: &Semantics<'_, RootDatabase>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
unsafe_token: Option<SyntaxToken>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
we already have the token here |
||||||||||||||||||||||||||||||||||||||||||||||||||
block_expr: Option<ast::BlockExpr>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
) -> Option<FxHashMap<EditionedFileId, Vec<HighlightedRange>>> { | ||||||||||||||||||||||||||||||||||||||||||||||||||
let mut highlights: FxHashMap<EditionedFileId, Vec<_>> = FxHashMap::default(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
let mut push_to_highlights = |file_id, range| { | ||||||||||||||||||||||||||||||||||||||||||||||||||
if let Some(FileRange { file_id, range }) = original_frange(sema.db, file_id, range) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
let hrange = HighlightedRange { category: ReferenceCategory::empty(), range }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
highlights.entry(file_id).or_default().push(hrange); | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
// highlight unsafe keyword itself | ||||||||||||||||||||||||||||||||||||||||||||||||||
let unsafe_token = unsafe_token?; | ||||||||||||||||||||||||||||||||||||||||||||||||||
let unsafe_token_file_id = sema.hir_file_for(&unsafe_token.parent()?); | ||||||||||||||||||||||||||||||||||||||||||||||||||
push_to_highlights(unsafe_token_file_id, Some(unsafe_token.text_range())); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
if let Some(block) = block_expr { | ||||||||||||||||||||||||||||||||||||||||||||||||||
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()), | ||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+736
to
+753
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
Some(highlights) | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
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() | ||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+759
to
+763
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(test)] | ||||||||||||||||||||||||||||||||||||||||||||||||||
mod tests { | ||||||||||||||||||||||||||||||||||||||||||||||||||
use itertools::Itertools; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -754,6 +811,32 @@ mod tests { | |||||||||||||||||||||||||||||||||||||||||||||||||
assert_eq!(expected, actual); | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
#[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||
fn test_hl_unsafe_block() { | ||||||||||||||||||||||||||||||||||||||||||||||||||
check( | ||||||||||||||||||||||||||||||||||||||||||||||||||
r#" | ||||||||||||||||||||||||||||||||||||||||||||||||||
fn foo() { | ||||||||||||||||||||||||||||||||||||||||||||||||||
unsafe fn this_is_unsafe_function() {} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
unsa$0fe { | ||||||||||||||||||||||||||||||||||||||||||||||||||
//^^^^^^ | ||||||||||||||||||||||||||||||||||||||||||||||||||
let raw_ptr = &42 as *const i32; | ||||||||||||||||||||||||||||||||||||||||||||||||||
let val = *raw_ptr; | ||||||||||||||||||||||||||||||||||||||||||||||||||
//^^^^^^^^ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
let mut_ptr = &mut 5 as *mut i32; | ||||||||||||||||||||||||||||||||||||||||||||||||||
*mut_ptr = 10; | ||||||||||||||||||||||||||||||||||||||||||||||||||
//^^^^^^^^ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
this_is_unsafe_function(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
//^^^^^^^^^^^^^^^^^^^^^^^^^ | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
"#, | ||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
#[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||
fn test_hl_tuple_fields() { | ||||||||||||||||||||||||||||||||||||||||||||||||||
check( | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.