Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-diagnostics/src/handlers/missing_unsafe.rs')
| -rw-r--r-- | crates/ide-diagnostics/src/handlers/missing_unsafe.rs | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/crates/ide-diagnostics/src/handlers/missing_unsafe.rs b/crates/ide-diagnostics/src/handlers/missing_unsafe.rs index 5b43f4b2af..a630d3c7c3 100644 --- a/crates/ide-diagnostics/src/handlers/missing_unsafe.rs +++ b/crates/ide-diagnostics/src/handlers/missing_unsafe.rs @@ -1,9 +1,9 @@ use hir::db::ExpandDatabase; use hir::HirFileIdExt; +use ide_db::text_edit::TextEdit; use ide_db::{assists::Assist, source_change::SourceChange}; use syntax::{ast, SyntaxNode}; use syntax::{match_ast, AstNode}; -use text_edit::TextEdit; use crate::{fix, Diagnostic, DiagnosticCode, DiagnosticsContext}; @@ -32,7 +32,8 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingUnsafe) -> Option<Vec<Ass } let root = ctx.sema.db.parse_or_expand(d.expr.file_id); - let expr = d.expr.value.to_node(&root); + let node = d.expr.value.to_node(&root); + let expr = node.syntax().ancestors().find_map(ast::Expr::cast)?; let node_to_add_unsafe_block = pick_best_node_to_add_unsafe_block(&expr)?; @@ -595,4 +596,39 @@ unsafe fn foo(p: *mut i32) { "#, ) } + + #[test] + fn no_unsafe_diagnostic_with_safe_kw() { + check_diagnostics( + r#" +unsafe extern { + pub safe fn f(); + + pub unsafe fn g(); + + pub fn h(); + + pub safe static S1: i32; + + pub unsafe static S2: i32; + + pub static S3: i32; +} + +fn main() { + f(); + g(); + //^^^💡 error: this operation is unsafe and requires an unsafe function or block + h(); + //^^^💡 error: this operation is unsafe and requires an unsafe function or block + + let _ = S1; + let _ = S2; + //^^💡 error: this operation is unsafe and requires an unsafe function or block + let _ = S3; + //^^💡 error: this operation is unsafe and requires an unsafe function or block +} +"#, + ); + } } |