Skip to content

Commit 769d527

Browse files
committed
partially port invalid_value lint to diagnostic items
1 parent 7a76fe7 commit 769d527

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

src/libcore/mem/maybe_uninit.rs

+2
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ impl<T> MaybeUninit<T> {
254254
/// [type]: union.MaybeUninit.html
255255
#[stable(feature = "maybe_uninit", since = "1.36.0")]
256256
#[inline(always)]
257+
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "maybe_uninit_uninit")]
257258
pub const fn uninit() -> MaybeUninit<T> {
258259
MaybeUninit { uninit: () }
259260
}
@@ -300,6 +301,7 @@ impl<T> MaybeUninit<T> {
300301
/// ```
301302
#[stable(feature = "maybe_uninit", since = "1.36.0")]
302303
#[inline]
304+
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "maybe_uninit_zeroed")]
303305
pub fn zeroed() -> MaybeUninit<T> {
304306
let mut u = MaybeUninit::<T>::uninit();
305307
unsafe {

src/libcore/mem/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ pub const fn needs_drop<T>() -> bool {
457457
#[stable(feature = "rust1", since = "1.0.0")]
458458
#[allow(deprecated_in_future)]
459459
#[allow(deprecated)]
460+
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "mem_zeroed")]
460461
pub unsafe fn zeroed<T>() -> T {
461462
intrinsics::panic_if_uninhabited::<T>();
462463
intrinsics::init()
@@ -485,6 +486,7 @@ pub unsafe fn zeroed<T>() -> T {
485486
#[stable(feature = "rust1", since = "1.0.0")]
486487
#[allow(deprecated_in_future)]
487488
#[allow(deprecated)]
489+
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "mem_uninitialized")]
488490
pub unsafe fn uninitialized<T>() -> T {
489491
intrinsics::panic_if_uninhabited::<T>();
490492
intrinsics::uninit()

src/librustc_lint/builtin.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -1903,29 +1903,23 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidValue {
19031903

19041904
/// Determine if this expression is a "dangerous initialization".
19051905
fn is_dangerous_init(cx: &LateContext<'_, '_>, expr: &hir::Expr) -> Option<InitKind> {
1906-
const ZEROED_PATH: &[Symbol] = &[sym::core, sym::mem, sym::zeroed];
1907-
const UININIT_PATH: &[Symbol] = &[sym::core, sym::mem, sym::uninitialized];
19081906
// `transmute` is inside an anonymous module (the `extern` block?);
19091907
// `Invalid` represents the empty string and matches that.
1908+
// FIXME(#66075): use diagnostic items. Somehow, that does not seem to work
1909+
// on intrinsics right now.
19101910
const TRANSMUTE_PATH: &[Symbol] =
19111911
&[sym::core, sym::intrinsics, kw::Invalid, sym::transmute];
1912-
const MU_ZEROED_PATH: &[Symbol] =
1913-
&[sym::core, sym::mem, sym::maybe_uninit, sym::MaybeUninit, sym::zeroed];
1914-
const MU_UNINIT_PATH: &[Symbol] =
1915-
&[sym::core, sym::mem, sym::maybe_uninit, sym::MaybeUninit, sym::uninit];
19161912

19171913
if let hir::ExprKind::Call(ref path_expr, ref args) = expr.kind {
19181914
// Find calls to `mem::{uninitialized,zeroed}` methods.
19191915
if let hir::ExprKind::Path(ref qpath) = path_expr.kind {
19201916
let def_id = cx.tables.qpath_res(qpath, path_expr.hir_id).opt_def_id()?;
19211917

1922-
if cx.match_def_path(def_id, ZEROED_PATH) {
1918+
if cx.tcx.is_diagnostic_item(sym::mem_zeroed, def_id) {
19231919
return Some(InitKind::Zeroed);
1924-
}
1925-
if cx.match_def_path(def_id, UININIT_PATH) {
1920+
} else if cx.tcx.is_diagnostic_item(sym::mem_uninitialized, def_id) {
19261921
return Some(InitKind::Uninit);
1927-
}
1928-
if cx.match_def_path(def_id, TRANSMUTE_PATH) {
1922+
} else if cx.match_def_path(def_id, TRANSMUTE_PATH) {
19291923
if is_zero(&args[0]) {
19301924
return Some(InitKind::Zeroed);
19311925
}
@@ -1940,9 +1934,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidValue {
19401934
if let hir::ExprKind::Call(ref path_expr, _) = args[0].kind {
19411935
if let hir::ExprKind::Path(ref qpath) = path_expr.kind {
19421936
let def_id = cx.tables.qpath_res(qpath, path_expr.hir_id).opt_def_id()?;
1943-
if cx.match_def_path(def_id, MU_ZEROED_PATH) {
1937+
1938+
if cx.tcx.is_diagnostic_item(sym::maybe_uninit_zeroed, def_id) {
19441939
return Some(InitKind::Zeroed);
1945-
} else if cx.match_def_path(def_id, MU_UNINIT_PATH) {
1940+
} else if cx.tcx.is_diagnostic_item(sym::maybe_uninit_uninit, def_id) {
19461941
return Some(InitKind::Uninit);
19471942
}
19481943
}

src/libsyntax_pos/symbol.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,10 @@ symbols! {
418418
match_beginning_vert,
419419
match_default_bindings,
420420
may_dangle,
421-
maybe_uninit,
422-
MaybeUninit,
423-
mem,
421+
maybe_uninit_uninit,
422+
maybe_uninit_zeroed,
423+
mem_uninitialized,
424+
mem_zeroed,
424425
member_constraints,
425426
message,
426427
meta,
@@ -712,8 +713,6 @@ symbols! {
712713
underscore_imports,
713714
underscore_lifetimes,
714715
uniform_paths,
715-
uninit,
716-
uninitialized,
717716
universal_impl_trait,
718717
unmarked_api,
719718
unreachable_code,
@@ -745,7 +744,6 @@ symbols! {
745744
windows,
746745
windows_subsystem,
747746
Yield,
748-
zeroed,
749747
}
750748
}
751749

0 commit comments

Comments
 (0)