Skip to content

Commit 3d21d5e

Browse files
Add diagnostic for accessing an extern static
1 parent 0ae42bd commit 3d21d5e

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

crates/hir-ty/src/diagnostics/unsafe_check.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ fn walk_unsafe(
8787
let g = resolver.update_to_inner_scope(db.upcast(), def, current);
8888
let value_or_partial = resolver.resolve_path_in_value_ns(db.upcast(), path);
8989
if let Some(ResolveValueResult::ValueNs(ValueNs::StaticId(id), _)) = value_or_partial {
90-
if db.static_data(id).mutable {
90+
let static_data = db.static_data(id);
91+
if static_data.mutable || static_data.is_extern {
9192
unsafe_expr_cb(UnsafeExpr { expr: current, inside_unsafe_block });
9293
}
9394
}

crates/ide-diagnostics/src/handlers/missing_unsafe.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,31 @@ fn main() {
163163
);
164164
}
165165

166+
#[test]
167+
fn missing_unsafe_diagnostic_with_extern_static() {
168+
check_diagnostics(
169+
r#"
170+
//- minicore: copy
171+
172+
extern "C" {
173+
static EXTERN: i32;
174+
static mut EXTERN_MUT: i32;
175+
}
176+
177+
fn main() {
178+
let _x = EXTERN;
179+
//^^^^^^💡 error: this operation is unsafe and requires an unsafe function or block
180+
let _x = EXTERN_MUT;
181+
//^^^^^^^^^^💡 error: this operation is unsafe and requires an unsafe function or block
182+
unsafe {
183+
let _x = EXTERN;
184+
let _x = EXTERN_MUT;
185+
}
186+
}
187+
"#,
188+
);
189+
}
190+
166191
#[test]
167192
fn no_missing_unsafe_diagnostic_with_safe_intrinsic() {
168193
check_diagnostics(

0 commit comments

Comments
 (0)