add tests for false positives
This commit is contained in:
parent
7fd11d23b0
commit
ddad5e0f86
3 changed files with 46 additions and 29 deletions
|
|
@ -22,6 +22,7 @@ declare_lint! {
|
|||
|
||||
const ZERO_REF_SUMMARY: &str = "reference to zeroed memory";
|
||||
const UNINIT_REF_SUMMARY: &str = "reference to uninitialized memory";
|
||||
const HELP: &str = "Creation of a null reference is undefined behavior; see https://doc.rust-lang.org/reference/behavior-considered-undefined.html";
|
||||
|
||||
pub struct InvalidRef;
|
||||
|
||||
|
|
@ -34,26 +35,21 @@ impl LintPass for InvalidRef {
|
|||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidRef {
|
||||
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
|
||||
if_let_chain!{[
|
||||
let ty::TyRef(..) = cx.tables.expr_ty(expr).sty,
|
||||
let ExprCall(ref path, ref args) = expr.node,
|
||||
let ExprPath(ref qpath) = path.node,
|
||||
args.len() == 0,
|
||||
let ty::TyRef(..) = cx.tables.expr_ty(expr).sty,
|
||||
let Some(def_id) = opt_def_id(cx.tables.qpath_def(qpath, path.hir_id)),
|
||||
], {
|
||||
let help = "Creation of a null reference is undefined behavior; see https://doc.rust-lang.org/reference/behavior-considered-undefined.html";
|
||||
if match_def_path(cx.tcx, def_id, &paths::MEM_ZEROED) | match_def_path(cx.tcx, def_id, &paths::INIT) {
|
||||
let lint = INVALID_REF;
|
||||
let msg = ZERO_REF_SUMMARY;
|
||||
span_help_and_lint(cx, lint, expr.span, &msg, &help);
|
||||
let msg = if match_def_path(cx.tcx, def_id, &paths::MEM_ZEROED) | match_def_path(cx.tcx, def_id, &paths::INIT) {
|
||||
ZERO_REF_SUMMARY
|
||||
} else if match_def_path(cx.tcx, def_id, &paths::MEM_UNINIT) | match_def_path(cx.tcx, def_id, &paths::UNINIT) {
|
||||
let lint = INVALID_REF;
|
||||
let msg = UNINIT_REF_SUMMARY;
|
||||
span_help_and_lint(cx, lint, expr.span, &msg, &help);
|
||||
UNINIT_REF_SUMMARY
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}}
|
||||
};
|
||||
span_help_and_lint(cx, INVALID_REF, expr.span, msg, HELP);
|
||||
}}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,6 +16,10 @@ fn main() {
|
|||
ref_to_uninit_std(&x);
|
||||
ref_to_uninit_core(&x);
|
||||
ref_to_uninit_intr(&x);
|
||||
some_ref();
|
||||
std_zeroed_no_ref();
|
||||
core_zeroed_no_ref();
|
||||
intr_init_no_ref();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -43,3 +47,20 @@ unsafe fn ref_to_uninit_intr<T: ?Sized>(t: &T) {
|
|||
let ref_uninit: &T = std::intrinsics::uninit(); // warning
|
||||
}
|
||||
|
||||
fn some_ref() {
|
||||
let some_ref = &1;
|
||||
}
|
||||
|
||||
unsafe fn std_zeroed_no_ref() {
|
||||
let mem_zero: usize = std::mem::zeroed(); // no warning
|
||||
}
|
||||
|
||||
unsafe fn core_zeroed_no_ref() {
|
||||
let mem_zero: usize = core::mem::zeroed(); // no warning
|
||||
}
|
||||
|
||||
unsafe fn intr_init_no_ref() {
|
||||
let mem_zero: usize = std::intrinsics::init(); // no warning
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,48 +1,48 @@
|
|||
error: reference to zeroed memory
|
||||
--> $DIR/invalid_ref.rs:23:24
|
||||
--> $DIR/invalid_ref.rs:27:24
|
||||
|
|
||||
23 | let ref_zero: &T = std::mem::zeroed(); // warning
|
||||
27 | let ref_zero: &T = std::mem::zeroed(); // warning
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D invalid-ref` implied by `-D warnings`
|
||||
= help: Creation of a null reference is undefined behavior; see https://doc.rust-lang.org/reference/behavior-considered-undefined.html
|
||||
|
||||
error: reference to zeroed memory
|
||||
--> $DIR/invalid_ref.rs:27:24
|
||||
--> $DIR/invalid_ref.rs:31:24
|
||||
|
|
||||
27 | let ref_zero: &T = core::mem::zeroed(); // warning
|
||||
31 | let ref_zero: &T = core::mem::zeroed(); // warning
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: Creation of a null reference is undefined behavior; see https://doc.rust-lang.org/reference/behavior-considered-undefined.html
|
||||
|
||||
error: reference to zeroed memory
|
||||
--> $DIR/invalid_ref.rs:31:24
|
||||
--> $DIR/invalid_ref.rs:35:24
|
||||
|
|
||||
31 | let ref_zero: &T = std::intrinsics::init(); // warning
|
||||
35 | let ref_zero: &T = std::intrinsics::init(); // warning
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: Creation of a null reference is undefined behavior; see https://doc.rust-lang.org/reference/behavior-considered-undefined.html
|
||||
|
||||
error: reference to uninitialized memory
|
||||
--> $DIR/invalid_ref.rs:35:26
|
||||
|
|
||||
35 | let ref_uninit: &T = std::mem::uninitialized(); // warning
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: Creation of a null reference is undefined behavior; see https://doc.rust-lang.org/reference/behavior-considered-undefined.html
|
||||
|
||||
error: reference to uninitialized memory
|
||||
--> $DIR/invalid_ref.rs:39:26
|
||||
|
|
||||
39 | let ref_uninit: &T = core::mem::uninitialized(); // warning
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
39 | let ref_uninit: &T = std::mem::uninitialized(); // warning
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: Creation of a null reference is undefined behavior; see https://doc.rust-lang.org/reference/behavior-considered-undefined.html
|
||||
|
||||
error: reference to uninitialized memory
|
||||
--> $DIR/invalid_ref.rs:43:26
|
||||
|
|
||||
43 | let ref_uninit: &T = std::intrinsics::uninit(); // warning
|
||||
43 | let ref_uninit: &T = core::mem::uninitialized(); // warning
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: Creation of a null reference is undefined behavior; see https://doc.rust-lang.org/reference/behavior-considered-undefined.html
|
||||
|
||||
error: reference to uninitialized memory
|
||||
--> $DIR/invalid_ref.rs:47:26
|
||||
|
|
||||
47 | let ref_uninit: &T = std::intrinsics::uninit(); // warning
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: Creation of a null reference is undefined behavior; see https://doc.rust-lang.org/reference/behavior-considered-undefined.html
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue