Skip to content

Commit 9db3fdd

Browse files
authored
Rollup merge of #66217 - RalfJung:diagnostic-items, r=Centril
invalid_value lint: use diagnostic items This adjusts the invalid_value lint to use diagnostic items. @Centril @oli-obk For some reason, this fails to recognize `transmute` -- somehow the diagnostic item is not found. Any idea why? r? @Centril Cc #66075
2 parents f166609 + 769d527 commit 9db3fdd

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
@@ -256,6 +256,7 @@ impl<T> MaybeUninit<T> {
256256
/// [type]: union.MaybeUninit.html
257257
#[stable(feature = "maybe_uninit", since = "1.36.0")]
258258
#[inline(always)]
259+
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "maybe_uninit_uninit")]
259260
pub const fn uninit() -> MaybeUninit<T> {
260261
MaybeUninit { uninit: () }
261262
}
@@ -339,6 +340,7 @@ impl<T> MaybeUninit<T> {
339340
/// ```
340341
#[stable(feature = "maybe_uninit", since = "1.36.0")]
341342
#[inline]
343+
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "maybe_uninit_zeroed")]
342344
pub fn zeroed() -> MaybeUninit<T> {
343345
let mut u = MaybeUninit::<T>::uninit();
344346
unsafe {

src/libcore/mem/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ pub const fn needs_drop<T>() -> bool {
468468
#[stable(feature = "rust1", since = "1.0.0")]
469469
#[allow(deprecated_in_future)]
470470
#[allow(deprecated)]
471+
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "mem_zeroed")]
471472
pub unsafe fn zeroed<T>() -> T {
472473
intrinsics::panic_if_uninhabited::<T>();
473474
intrinsics::init()
@@ -496,6 +497,7 @@ pub unsafe fn zeroed<T>() -> T {
496497
#[stable(feature = "rust1", since = "1.0.0")]
497498
#[allow(deprecated_in_future)]
498499
#[allow(deprecated)]
500+
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "mem_uninitialized")]
499501
pub unsafe fn uninitialized<T>() -> T {
500502
intrinsics::panic_if_uninhabited::<T>();
501503
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,
@@ -713,8 +714,6 @@ symbols! {
713714
underscore_imports,
714715
underscore_lifetimes,
715716
uniform_paths,
716-
uninit,
717-
uninitialized,
718717
universal_impl_trait,
719718
unmarked_api,
720719
unreachable_code,
@@ -745,7 +744,6 @@ symbols! {
745744
windows,
746745
windows_subsystem,
747746
Yield,
748-
zeroed,
749747
}
750748
}
751749

0 commit comments

Comments
 (0)