From ddad5e0f86176e7bd2edaa95ec5272911791669f Mon Sep 17 00:00:00 2001 From: Laura Peskin Date: Fri, 29 Sep 2017 21:01:02 -0400 Subject: [PATCH] add tests for false positives --- clippy_lints/src/invalid_ref.rs | 20 ++++++++----------- tests/ui/invalid_ref.rs | 21 ++++++++++++++++++++ tests/ui/invalid_ref.stderr | 34 ++++++++++++++++----------------- 3 files changed, 46 insertions(+), 29 deletions(-) diff --git a/clippy_lints/src/invalid_ref.rs b/clippy_lints/src/invalid_ref.rs index 440e3055bb10..ad3398cb0782 100644 --- a/clippy_lints/src/invalid_ref.rs +++ b/clippy_lints/src/invalid_ref.rs @@ -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; } } - diff --git a/tests/ui/invalid_ref.rs b/tests/ui/invalid_ref.rs index d7341575e95d..2b8f04c9781c 100644 --- a/tests/ui/invalid_ref.rs +++ b/tests/ui/invalid_ref.rs @@ -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: &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 +} + + diff --git a/tests/ui/invalid_ref.stderr b/tests/ui/invalid_ref.stderr index 7cdc31a0ffa1..420fed017444 100644 --- a/tests/ui/invalid_ref.stderr +++ b/tests/ui/invalid_ref.stderr @@ -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