mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Add tests covering unsafe blocks, more attempts to get call expr tests passing
This commit is contained in:
parent
daf1cac9f8
commit
b358fbfdf8
2 changed files with 86 additions and 1 deletions
|
@ -329,13 +329,28 @@ pub fn unsafe_expressions(
|
|||
match expr {
|
||||
Expr::Call { callee, .. } => {
|
||||
if infer
|
||||
.method_resolution(*callee)
|
||||
.method_resolution(/* id */ *callee)
|
||||
.map(|func| db.function_data(func).is_unsafe)
|
||||
.unwrap_or(false)
|
||||
{
|
||||
unsafe_expr_ids.push(id);
|
||||
}
|
||||
}
|
||||
Expr::MethodCall {/*_receiver, method_name,*/ .. } => {
|
||||
// let receiver_ty = &infer.type_of_expr[*receiver];
|
||||
// receiver_ty
|
||||
if infer
|
||||
.method_resolution(id)
|
||||
.map(|func| {
|
||||
db.function_data(func).is_unsafe
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
false
|
||||
})
|
||||
{
|
||||
unsafe_expr_ids.push(id);
|
||||
}
|
||||
}
|
||||
Expr::UnaryOp { expr, op: UnaryOp::Deref } => {
|
||||
if let Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(..), .. }) = &infer[*expr] {
|
||||
unsafe_expr_ids.push(id);
|
||||
|
|
|
@ -602,6 +602,76 @@ fn missing_unsafe() {
|
|||
assert_snapshot!(diagnostics, @r#""fn missing_unsafe() {\n HasUnsafe.unsafe_fn();\n}": Missing unsafe keyword on fn"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_missing_unsafe_diagnostic_with_raw_ptr_in_unsafe_block() {
|
||||
let diagnostics = TestDB::with_files(
|
||||
r"
|
||||
//- /lib.rs
|
||||
fn nothing_to_see_move_along() {
|
||||
unsafe {
|
||||
let x = &5 as *usize;
|
||||
let y = *x;
|
||||
}
|
||||
}
|
||||
",
|
||||
)
|
||||
.diagnostics()
|
||||
.0;
|
||||
|
||||
assert_snapshot!(diagnostics, @"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_missing_unsafe_diagnostic_with_unsafe_call_in_unsafe_block() {
|
||||
let diagnostics = TestDB::with_files(
|
||||
r"
|
||||
//- /lib.rs
|
||||
unsafe fn unsafe_fn() {
|
||||
let x = &5 as *usize;
|
||||
let y = *x;
|
||||
}
|
||||
|
||||
fn nothing_to_see_move_along() {
|
||||
unsafe {
|
||||
unsafe_fn();
|
||||
}
|
||||
}
|
||||
",
|
||||
)
|
||||
.diagnostics()
|
||||
.0;
|
||||
|
||||
assert_snapshot!(diagnostics, @"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_missing_unsafe_diagnostic_with_unsafe_method_call_in_unsafe_block() {
|
||||
let diagnostics = TestDB::with_files(
|
||||
r"
|
||||
//- /lib.rs
|
||||
struct HasUnsafe;
|
||||
|
||||
impl HasUnsafe {
|
||||
unsafe fn unsafe_fn() {
|
||||
let x = &5 as *usize;
|
||||
let y = *x;
|
||||
}
|
||||
}
|
||||
|
||||
fn nothing_to_see_move_along() {
|
||||
unsafe {
|
||||
HasUnsafe.unsafe_fn();
|
||||
}
|
||||
}
|
||||
|
||||
",
|
||||
)
|
||||
.diagnostics()
|
||||
.0;
|
||||
|
||||
assert_snapshot!(diagnostics, @"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unnecessary_unsafe_diagnostic() {
|
||||
let diagnostics = TestDB::with_files(
|
||||
|
|
Loading…
Reference in a new issue