Skip to content

Commit

Permalink
fix invalid_ref lint and prepare for invalid_value lint
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Aug 9, 2019
1 parent c55d38e commit fd3ab4d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
15 changes: 8 additions & 7 deletions clippy_lints/src/invalid_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ declare_clippy_lint! {
"creation of invalid reference"
}

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; \
const SUMMARY: &str = "invalid reference";
const ZERO_REF_HELP: &str = "Creation of a null reference is undefined behavior; \
see https://doc.rust-lang.org/reference/behavior-considered-undefined.html";
const UNINIT_REF_HELP: &str = "Creation of an uninitialized reference is undefined behavior; \
see https://doc.rust-lang.org/reference/behavior-considered-undefined.html";

declare_lint_pass!(InvalidRef => [INVALID_REF]);
Expand All @@ -37,18 +38,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidRef {
if let ty::Ref(..) = cx.tables.expr_ty(expr).sty;
if let Some(def_id) = cx.tables.qpath_res(qpath, path.hir_id).opt_def_id();
then {
let msg = if match_def_path(cx, def_id, &paths::MEM_ZEROED) |
let help = if match_def_path(cx, def_id, &paths::MEM_ZEROED) |
match_def_path(cx, def_id, &paths::INIT)
{
ZERO_REF_SUMMARY
ZERO_REF_HELP
} else if match_def_path(cx, def_id, &paths::MEM_UNINIT) |
match_def_path(cx, def_id, &paths::UNINIT)
{
UNINIT_REF_SUMMARY
UNINIT_REF_HELP
} else {
return;
};
span_help_and_lint(cx, INVALID_REF, expr.span, msg, HELP);
span_help_and_lint(cx, INVALID_REF, expr.span, SUMMARY, help);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/invalid_ref.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(deprecated, unused)]
#![allow(deprecated, unused, unknown_lints, invalid_value)]
#![feature(core_intrinsics)]

extern crate core;
Expand Down
14 changes: 7 additions & 7 deletions tests/ui/invalid_ref.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: reference to zeroed memory
error: invalid reference
--> $DIR/invalid_ref.rs:23:24
|
LL | let ref_zero: &T = std::mem::zeroed(); // warning
Expand All @@ -7,37 +7,37 @@ LL | let ref_zero: &T = std::mem::zeroed(); // warning
= note: `#[deny(clippy::invalid_ref)]` on by default
= 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
error: invalid reference
--> $DIR/invalid_ref.rs:27:24
|
LL | 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
error: invalid reference
--> $DIR/invalid_ref.rs:31:24
|
LL | 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
error: invalid reference
--> $DIR/invalid_ref.rs:35:26
|
LL | 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
= help: Creation of an uninitialized reference is undefined behavior; see https://doc.rust-lang.org/reference/behavior-considered-undefined.html

error: reference to uninitialized memory
error: invalid reference
--> $DIR/invalid_ref.rs:39:26
|
LL | 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
= help: Creation of an uninitialized reference is undefined behavior; see https://doc.rust-lang.org/reference/behavior-considered-undefined.html

error: aborting due to 5 previous errors

0 comments on commit fd3ab4d

Please sign in to comment.