Add tests covering unsafe blocks, more attempts to get call expr tests passing

This commit is contained in:
Paul Daniel Faria 2020-05-24 02:10:34 -04:00
parent daf1cac9f8
commit b358fbfdf8
2 changed files with 86 additions and 1 deletions

View file

@ -329,13 +329,28 @@ pub fn unsafe_expressions(
match expr { match expr {
Expr::Call { callee, .. } => { Expr::Call { callee, .. } => {
if infer if infer
.method_resolution(*callee) .method_resolution(/* id */ *callee)
.map(|func| db.function_data(func).is_unsafe) .map(|func| db.function_data(func).is_unsafe)
.unwrap_or(false) .unwrap_or(false)
{ {
unsafe_expr_ids.push(id); 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 } => { Expr::UnaryOp { expr, op: UnaryOp::Deref } => {
if let Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(..), .. }) = &infer[*expr] { if let Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(..), .. }) = &infer[*expr] {
unsafe_expr_ids.push(id); unsafe_expr_ids.push(id);

View file

@ -602,6 +602,76 @@ fn missing_unsafe() {
assert_snapshot!(diagnostics, @r#""fn missing_unsafe() {\n HasUnsafe.unsafe_fn();\n}": Missing unsafe keyword on fn"#); 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] #[test]
fn unnecessary_unsafe_diagnostic() { fn unnecessary_unsafe_diagnostic() {
let diagnostics = TestDB::with_files( let diagnostics = TestDB::with_files(