about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2025-04-10 15:56:31 +0200
committerLukas Wirth <lukastw97@gmail.com>2025-04-10 16:10:30 +0200
commitff008f187742bdb51051fb0ee57b55d5f80616dc (patch)
tree2ec5666d7b0a148628bb733f51f13d49ce5ba4c2
parentbaecfcd9fc26f624f9bb3a3f6ffdc0c796a3f204 (diff)
downloadrust-ff008f187742bdb51051fb0ee57b55d5f80616dc.tar.gz
rust-ff008f187742bdb51051fb0ee57b55d5f80616dc.zip
fix: Walk const block expressions for unsafety checking
-rw-r--r--src/tools/rust-analyzer/crates/hir-def/src/expr_store.rs7
-rw-r--r--src/tools/rust-analyzer/crates/hir-ty/src/diagnostics/unsafe_check.rs1
-rw-r--r--src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/missing_unsafe.rs13
3 files changed, 21 insertions, 0 deletions
diff --git a/src/tools/rust-analyzer/crates/hir-def/src/expr_store.rs b/src/tools/rust-analyzer/crates/hir-def/src/expr_store.rs
index 05c220d223e..eb66e592d68 100644
--- a/src/tools/rust-analyzer/crates/hir-def/src/expr_store.rs
+++ b/src/tools/rust-analyzer/crates/hir-def/src/expr_store.rs
@@ -282,6 +282,9 @@ impl ExpressionStore {
         }
     }
 
+    /// Walks the immediate children expressions and calls `f` for each child expression.
+    ///
+    /// Note that this does not walk const blocks.
     pub fn walk_child_exprs(&self, expr_id: ExprId, mut f: impl FnMut(ExprId)) {
         let expr = &self[expr_id];
         match expr {
@@ -415,6 +418,10 @@ impl ExpressionStore {
         }
     }
 
+    /// Walks the immediate children expressions and calls `f` for each child expression but does
+    /// not walk expressions within patterns.
+    ///
+    /// Note that this does not walk const blocks.
     pub fn walk_child_exprs_without_pats(&self, expr_id: ExprId, mut f: impl FnMut(ExprId)) {
         let expr = &self[expr_id];
         match expr {
diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/diagnostics/unsafe_check.rs b/src/tools/rust-analyzer/crates/hir-ty/src/diagnostics/unsafe_check.rs
index ca0f33fa693..73b99db7268 100644
--- a/src/tools/rust-analyzer/crates/hir-ty/src/diagnostics/unsafe_check.rs
+++ b/src/tools/rust-analyzer/crates/hir-ty/src/diagnostics/unsafe_check.rs
@@ -348,6 +348,7 @@ impl<'a> UnsafeVisitor<'a> {
             Expr::Closure { args, .. } => {
                 self.walk_pats_top(args.iter().copied(), current);
             }
+            Expr::Const(e) => self.walk_expr(*e),
             _ => {}
         }
 
diff --git a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/missing_unsafe.rs b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/missing_unsafe.rs
index a9b481f8998..c851a9c2390 100644
--- a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/missing_unsafe.rs
+++ b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/missing_unsafe.rs
@@ -878,4 +878,17 @@ fn f(it: unsafe fn()){
         "#,
         );
     }
+
+    #[test]
+    fn unsafe_call_in_const_expr() {
+        check_diagnostics(
+            r#"
+unsafe fn f() {}
+fn main() {
+    const { f(); };
+         // ^^^ 💡 error: call to unsafe function is unsafe and requires an unsafe function or block
+}
+        "#,
+        );
+    }
 }