partially port invalid_value lint to diagnostic items

This commit is contained in:
Ralf Jung
2019-11-09 10:34:16 +01:00
parent 7a76fe76f7
commit 769d52774b
4 changed files with 16 additions and 19 deletions

View File

@@ -254,6 +254,7 @@ impl<T> MaybeUninit<T> {
/// [type]: union.MaybeUninit.html /// [type]: union.MaybeUninit.html
#[stable(feature = "maybe_uninit", since = "1.36.0")] #[stable(feature = "maybe_uninit", since = "1.36.0")]
#[inline(always)] #[inline(always)]
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "maybe_uninit_uninit")]
pub const fn uninit() -> MaybeUninit<T> { pub const fn uninit() -> MaybeUninit<T> {
MaybeUninit { uninit: () } MaybeUninit { uninit: () }
} }
@@ -300,6 +301,7 @@ impl<T> MaybeUninit<T> {
/// ``` /// ```
#[stable(feature = "maybe_uninit", since = "1.36.0")] #[stable(feature = "maybe_uninit", since = "1.36.0")]
#[inline] #[inline]
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "maybe_uninit_zeroed")]
pub fn zeroed() -> MaybeUninit<T> { pub fn zeroed() -> MaybeUninit<T> {
let mut u = MaybeUninit::<T>::uninit(); let mut u = MaybeUninit::<T>::uninit();
unsafe { unsafe {

View File

@@ -457,6 +457,7 @@ pub const fn needs_drop<T>() -> bool {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[allow(deprecated_in_future)] #[allow(deprecated_in_future)]
#[allow(deprecated)] #[allow(deprecated)]
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "mem_zeroed")]
pub unsafe fn zeroed<T>() -> T { pub unsafe fn zeroed<T>() -> T {
intrinsics::panic_if_uninhabited::<T>(); intrinsics::panic_if_uninhabited::<T>();
intrinsics::init() intrinsics::init()
@@ -485,6 +486,7 @@ pub unsafe fn zeroed<T>() -> T {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[allow(deprecated_in_future)] #[allow(deprecated_in_future)]
#[allow(deprecated)] #[allow(deprecated)]
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "mem_uninitialized")]
pub unsafe fn uninitialized<T>() -> T { pub unsafe fn uninitialized<T>() -> T {
intrinsics::panic_if_uninhabited::<T>(); intrinsics::panic_if_uninhabited::<T>();
intrinsics::uninit() intrinsics::uninit()

View File

@@ -1903,29 +1903,23 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidValue {
/// Determine if this expression is a "dangerous initialization". /// Determine if this expression is a "dangerous initialization".
fn is_dangerous_init(cx: &LateContext<'_, '_>, expr: &hir::Expr) -> Option<InitKind> { fn is_dangerous_init(cx: &LateContext<'_, '_>, expr: &hir::Expr) -> Option<InitKind> {
const ZEROED_PATH: &[Symbol] = &[sym::core, sym::mem, sym::zeroed];
const UININIT_PATH: &[Symbol] = &[sym::core, sym::mem, sym::uninitialized];
// `transmute` is inside an anonymous module (the `extern` block?); // `transmute` is inside an anonymous module (the `extern` block?);
// `Invalid` represents the empty string and matches that. // `Invalid` represents the empty string and matches that.
// FIXME(#66075): use diagnostic items. Somehow, that does not seem to work
// on intrinsics right now.
const TRANSMUTE_PATH: &[Symbol] = const TRANSMUTE_PATH: &[Symbol] =
&[sym::core, sym::intrinsics, kw::Invalid, sym::transmute]; &[sym::core, sym::intrinsics, kw::Invalid, sym::transmute];
const MU_ZEROED_PATH: &[Symbol] =
&[sym::core, sym::mem, sym::maybe_uninit, sym::MaybeUninit, sym::zeroed];
const MU_UNINIT_PATH: &[Symbol] =
&[sym::core, sym::mem, sym::maybe_uninit, sym::MaybeUninit, sym::uninit];
if let hir::ExprKind::Call(ref path_expr, ref args) = expr.kind { if let hir::ExprKind::Call(ref path_expr, ref args) = expr.kind {
// Find calls to `mem::{uninitialized,zeroed}` methods. // Find calls to `mem::{uninitialized,zeroed}` methods.
if let hir::ExprKind::Path(ref qpath) = path_expr.kind { if let hir::ExprKind::Path(ref qpath) = path_expr.kind {
let def_id = cx.tables.qpath_res(qpath, path_expr.hir_id).opt_def_id()?; let def_id = cx.tables.qpath_res(qpath, path_expr.hir_id).opt_def_id()?;
if cx.match_def_path(def_id, ZEROED_PATH) { if cx.tcx.is_diagnostic_item(sym::mem_zeroed, def_id) {
return Some(InitKind::Zeroed); return Some(InitKind::Zeroed);
} } else if cx.tcx.is_diagnostic_item(sym::mem_uninitialized, def_id) {
if cx.match_def_path(def_id, UININIT_PATH) {
return Some(InitKind::Uninit); return Some(InitKind::Uninit);
} } else if cx.match_def_path(def_id, TRANSMUTE_PATH) {
if cx.match_def_path(def_id, TRANSMUTE_PATH) {
if is_zero(&args[0]) { if is_zero(&args[0]) {
return Some(InitKind::Zeroed); return Some(InitKind::Zeroed);
} }
@@ -1940,9 +1934,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidValue {
if let hir::ExprKind::Call(ref path_expr, _) = args[0].kind { if let hir::ExprKind::Call(ref path_expr, _) = args[0].kind {
if let hir::ExprKind::Path(ref qpath) = path_expr.kind { if let hir::ExprKind::Path(ref qpath) = path_expr.kind {
let def_id = cx.tables.qpath_res(qpath, path_expr.hir_id).opt_def_id()?; let def_id = cx.tables.qpath_res(qpath, path_expr.hir_id).opt_def_id()?;
if cx.match_def_path(def_id, MU_ZEROED_PATH) {
if cx.tcx.is_diagnostic_item(sym::maybe_uninit_zeroed, def_id) {
return Some(InitKind::Zeroed); return Some(InitKind::Zeroed);
} else if cx.match_def_path(def_id, MU_UNINIT_PATH) { } else if cx.tcx.is_diagnostic_item(sym::maybe_uninit_uninit, def_id) {
return Some(InitKind::Uninit); return Some(InitKind::Uninit);
} }
} }

View File

@@ -418,9 +418,10 @@ symbols! {
match_beginning_vert, match_beginning_vert,
match_default_bindings, match_default_bindings,
may_dangle, may_dangle,
maybe_uninit, maybe_uninit_uninit,
MaybeUninit, maybe_uninit_zeroed,
mem, mem_uninitialized,
mem_zeroed,
member_constraints, member_constraints,
message, message,
meta, meta,
@@ -712,8 +713,6 @@ symbols! {
underscore_imports, underscore_imports,
underscore_lifetimes, underscore_lifetimes,
uniform_paths, uniform_paths,
uninit,
uninitialized,
universal_impl_trait, universal_impl_trait,
unmarked_api, unmarked_api,
unreachable_code, unreachable_code,
@@ -745,7 +744,6 @@ symbols! {
windows, windows,
windows_subsystem, windows_subsystem,
Yield, Yield,
zeroed,
} }
} }