Skip to content

Commit

Permalink
Merge unused_tuple_struct_fields into dead_code
Browse files Browse the repository at this point in the history
This implicitly upgrades the lint from `allow` to `warn` and places it
into the `unused` lint group.
  • Loading branch information
shepmaster committed Nov 29, 2023
1 parent abe34e9 commit 0ee2b06
Show file tree
Hide file tree
Showing 178 changed files with 262 additions and 290 deletions.
36 changes: 7 additions & 29 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,13 @@ declare_lint! {
/// Dead code may signal a mistake or unfinished code. To silence the
/// warning for individual items, prefix the name with an underscore such
/// as `_foo`. If it was intended to expose the item outside of the crate,
/// consider adding a visibility modifier like `pub`. Otherwise consider
/// removing the unused code.
/// consider adding a visibility modifier like `pub`.
///
/// To preserve the numbering of tuple structs with unused fields,
/// change the unused fields to have unit type or use
/// `PhantomData`.
///
/// Otherwise consider removing the unused code.
pub DEAD_CODE,
Warn,
"detect unused, unexported items"
Expand Down Expand Up @@ -604,32 +609,6 @@ declare_lint! {
"detects attributes that were not used by the compiler"
}

declare_lint! {
/// The `unused_tuple_struct_fields` lint detects fields of tuple structs
/// that are never read.
///
/// ### Example
///
/// ```rust
/// #[warn(unused_tuple_struct_fields)]
/// struct S(i32, i32, i32);
/// let s = S(1, 2, 3);
/// let _ = (s.0, s.2);
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// Tuple struct fields that are never read anywhere may indicate a
/// mistake or unfinished code. To silence this warning, consider
/// removing the unused field(s) or, to preserve the numbering of the
/// remaining fields, change the unused field(s) to have unit type.
pub UNUSED_TUPLE_STRUCT_FIELDS,
Allow,
"detects tuple struct fields that are never read"
}

declare_lint! {
/// The `unreachable_code` lint detects unreachable code paths.
///
Expand Down Expand Up @@ -3466,7 +3445,6 @@ declare_lint_pass! {
UNUSED_MACROS,
UNUSED_MUT,
UNUSED_QUALIFICATIONS,
UNUSED_TUPLE_STRUCT_FIELDS,
UNUSED_UNSAFE,
UNUSED_VARIABLES,
USELESS_DEPRECATED,
Expand Down
29 changes: 11 additions & 18 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,12 +835,6 @@ impl<'tcx> DeadVisitor<'tcx> {
let multiple = num > 6;
let name_list = names.into();

let lint = if is_positional {
lint::builtin::UNUSED_TUPLE_STRUCT_FIELDS
} else {
lint::builtin::DEAD_CODE
};

let parent_info = if let Some(parent_item) = parent_item {
let parent_descr = tcx.def_descr(parent_item.to_def_id());
let span = if let DefKind::Impl { .. } = tcx.def_kind(parent_item) {
Expand Down Expand Up @@ -893,7 +887,12 @@ impl<'tcx> DeadVisitor<'tcx> {
}
};

self.tcx.emit_spanned_lint(lint, first_hir_id, MultiSpan::from_spans(spans), diag);
self.tcx.emit_spanned_lint(
lint::builtin::DEAD_CODE,
first_hir_id,
MultiSpan::from_spans(spans),
diag,
);
}

fn warn_multiple(
Expand Down Expand Up @@ -1013,17 +1012,11 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) {
if let ShouldWarnAboutField::Yes(is_pos) =
visitor.should_warn_about_field(field)
{
let level = tcx
.lint_level_at_node(
if is_pos {
is_positional = true;
lint::builtin::UNUSED_TUPLE_STRUCT_FIELDS
} else {
lint::builtin::DEAD_CODE
},
hir_id,
)
.0;
if is_pos {
is_positional = true;
}

let level = tcx.lint_level_at_node(lint::builtin::DEAD_CODE, hir_id).0;
Some(DeadItem { def_id, name: field.name, level })
} else {
None
Expand Down
1 change: 1 addition & 0 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
//! Creating a recursive data structure:
//!
//! ```
//! ##[allow(dead_code)]
//! #[derive(Debug)]
//! enum List<T> {
//! Cons(T, Box<List<T>>),
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/boxed/thin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ struct WithHeader<H>(NonNull<u8>, PhantomData<H>);
/// An opaque representation of `WithHeader<H>` to avoid the
/// projection invariance of `<T as Pointee>::Metadata`.
#[repr(transparent)]
#[allow(unused_tuple_struct_fields)] // Field only used through `WithHeader` type above.
#[allow(dead_code)] // Field only used through `WithHeader` type above.
struct WithOpaqueHeader(NonNull<u8>);

impl WithOpaqueHeader {
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/collections/btree/set/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ fn test_extend_ref() {
#[test]
fn test_recovery() {
#[derive(Debug)]
struct Foo(&'static str, i32);
struct Foo(&'static str, #[allow(dead_code)] i32);

impl PartialEq for Foo {
fn eq(&self, other: &Self) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/collections/vec_deque/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ fn test_clone_from() {
fn test_vec_deque_truncate_drop() {
static mut DROPS: u32 = 0;
#[derive(Clone)]
struct Elem(i32);
struct Elem(#[allow(dead_code)] i32);
impl Drop for Elem {
fn drop(&mut self) {
unsafe {
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/tests/autotraits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fn require_sync<T: Sync>(_: T) {}
fn require_send_sync<T: Send + Sync>(_: T) {}

struct NotSend(*const ());
struct NotSend(#[allow(dead_code)] *const ());
unsafe impl Sync for NotSend {}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions library/alloc/tests/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ fn test_cmp() {
#[test]
fn test_vec_truncate_drop() {
static mut DROPS: u32 = 0;
struct Elem(i32);
struct Elem(#[allow(dead_code)] i32);
impl Drop for Elem {
fn drop(&mut self) {
unsafe {
Expand Down Expand Up @@ -1091,7 +1091,7 @@ fn test_into_iter_advance_by() {

#[test]
fn test_into_iter_drop_allocator() {
struct ReferenceCountedAllocator<'a>(DropCounter<'a>);
struct ReferenceCountedAllocator<'a>(#[allow(dead_code)] DropCounter<'a>);

unsafe impl Allocator for ReferenceCountedAllocator<'_> {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, core::alloc::AllocError> {
Expand Down Expand Up @@ -2401,7 +2401,7 @@ fn test_vec_dedup_multiple_ident() {
#[test]
fn test_vec_dedup_partialeq() {
#[derive(Debug)]
struct Foo(i32, i32);
struct Foo(i32, #[allow(dead_code)] i32);

impl PartialEq for Foo {
fn eq(&self, other: &Foo) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions library/core/benches/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn binary_search_l3_worst_case(b: &mut Bencher) {
}

#[derive(Clone)]
struct Rgb(u8, u8, u8);
struct Rgb(#[allow(dead_code)] u8, #[allow(dead_code)] u8, #[allow(dead_code)] u8);

impl Rgb {
fn gen(i: usize) -> Self {
Expand Down Expand Up @@ -154,7 +154,7 @@ swap_with_slice!(swap_with_slice_5x_usize_3000, 3000, |i| [i; 5]);
#[bench]
fn fill_byte_sized(b: &mut Bencher) {
#[derive(Copy, Clone)]
struct NewType(u8);
struct NewType(#[allow(dead_code)] u8);

let mut ary = [NewType(0); 1024];

Expand Down
2 changes: 1 addition & 1 deletion library/core/tests/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn any_unsized() {
fn distinct_type_names() {
// https://github.com/rust-lang/rust/issues/84666

struct Velocity(f32, f32);
struct Velocity(#[allow(dead_code)] f32, #[allow(dead_code)] f32);

fn type_name_of_val<T>(_: T) -> &'static str {
type_name::<T>()
Expand Down
2 changes: 1 addition & 1 deletion library/core/tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ fn array_default_impl_avoids_leaks_on_panic() {
use core::sync::atomic::{AtomicUsize, Ordering::Relaxed};
static COUNTER: AtomicUsize = AtomicUsize::new(0);
#[derive(Debug)]
struct Bomb(usize);
struct Bomb(#[allow(dead_code)] usize);

impl Default for Bomb {
fn default() -> Bomb {
Expand Down
2 changes: 1 addition & 1 deletion library/core/tests/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ fn ptr_bitops() {
#[cfg(any(not(target_arch = "arm"), target_os = "linux"))] // Missing intrinsic in compiler-builtins
fn ptr_bitops_tagging() {
#[repr(align(16))]
struct Tagme(u128);
struct Tagme(#[allow(dead_code)] u128);

let tagme = Tagme(1000);
let ptr = &tagme as *const Tagme as *mut Tagme;
Expand Down
6 changes: 3 additions & 3 deletions library/core/tests/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::intrinsics::assume;
#[test]
fn test_typeid_sized_types() {
struct X;
struct Y(u32);
struct Y(#[allow(dead_code)] u32);

assert_eq!(TypeId::of::<X>(), TypeId::of::<X>());
assert_eq!(TypeId::of::<Y>(), TypeId::of::<Y>());
Expand All @@ -14,8 +14,8 @@ fn test_typeid_sized_types() {
#[test]
fn test_typeid_unsized_types() {
trait Z {}
struct X(str);
struct Y(dyn Z + 'static);
struct X(#[allow(dead_code)] str);
struct Y(#[allow(dead_code)] dyn Z + 'static);

assert_eq!(TypeId::of::<X>(), TypeId::of::<X>());
assert_eq!(TypeId::of::<Y>(), TypeId::of::<Y>());
Expand Down
38 changes: 19 additions & 19 deletions library/core/tests/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,34 +451,34 @@ fn align_offset_various_strides() {
for ptr in 1usize..4 * align {
unsafe {
#[repr(packed)]
struct A3(u16, u8);
struct A3(#[allow(dead_code)] u16, #[allow(dead_code)] u8);
x |= test_stride::<A3>(ptr::invalid::<A3>(ptr), align);

struct A4(u32);
struct A4(#[allow(dead_code)] u32);
x |= test_stride::<A4>(ptr::invalid::<A4>(ptr), align);

#[repr(packed)]
struct A5(u32, u8);
struct A5(#[allow(dead_code)] u32, #[allow(dead_code)] u8);
x |= test_stride::<A5>(ptr::invalid::<A5>(ptr), align);

#[repr(packed)]
struct A6(u32, u16);
struct A6(#[allow(dead_code)] u32, #[allow(dead_code)] u16);
x |= test_stride::<A6>(ptr::invalid::<A6>(ptr), align);

#[repr(packed)]
struct A7(u32, u16, u8);
struct A7(#[allow(dead_code)] u32, #[allow(dead_code)] u16, #[allow(dead_code)] u8);
x |= test_stride::<A7>(ptr::invalid::<A7>(ptr), align);

#[repr(packed)]
struct A8(u32, u32);
struct A8(#[allow(dead_code)] u32, #[allow(dead_code)] u32);
x |= test_stride::<A8>(ptr::invalid::<A8>(ptr), align);

#[repr(packed)]
struct A9(u32, u32, u8);
struct A9(#[allow(dead_code)] u32, #[allow(dead_code)] u32, #[allow(dead_code)] u8);
x |= test_stride::<A9>(ptr::invalid::<A9>(ptr), align);

#[repr(packed)]
struct A10(u32, u32, u16);
struct A10(#[allow(dead_code)] u32, #[allow(dead_code)] u32, #[allow(dead_code)] u16);
x |= test_stride::<A10>(ptr::invalid::<A10>(ptr), align);

x |= test_stride::<u32>(ptr::invalid::<u32>(ptr), align);
Expand Down Expand Up @@ -517,34 +517,34 @@ fn align_offset_various_strides_const() {
while ptr < 4 * align {
unsafe {
#[repr(packed)]
struct A3(u16, u8);
struct A3(#[allow(dead_code)] u16, #[allow(dead_code)] u8);
test_stride::<A3>(ptr::invalid::<A3>(ptr), ptr, align);

struct A4(u32);
struct A4(#[allow(dead_code)] u32);
test_stride::<A4>(ptr::invalid::<A4>(ptr), ptr, align);

#[repr(packed)]
struct A5(u32, u8);
struct A5(#[allow(dead_code)] u32, #[allow(dead_code)] u8);
test_stride::<A5>(ptr::invalid::<A5>(ptr), ptr, align);

#[repr(packed)]
struct A6(u32, u16);
struct A6(#[allow(dead_code)] u32, #[allow(dead_code)] u16);
test_stride::<A6>(ptr::invalid::<A6>(ptr), ptr, align);

#[repr(packed)]
struct A7(u32, u16, u8);
struct A7(#[allow(dead_code)] u32, #[allow(dead_code)] u16, #[allow(dead_code)] u8);
test_stride::<A7>(ptr::invalid::<A7>(ptr), ptr, align);

#[repr(packed)]
struct A8(u32, u32);
struct A8(#[allow(dead_code)] u32, #[allow(dead_code)] u32);
test_stride::<A8>(ptr::invalid::<A8>(ptr), ptr, align);

#[repr(packed)]
struct A9(u32, u32, u8);
struct A9(#[allow(dead_code)] u32, #[allow(dead_code)] u32, #[allow(dead_code)] u8);
test_stride::<A9>(ptr::invalid::<A9>(ptr), ptr, align);

#[repr(packed)]
struct A10(u32, u32, u16);
struct A10(#[allow(dead_code)] u32, #[allow(dead_code)] u32, #[allow(dead_code)] u16);
test_stride::<A10>(ptr::invalid::<A10>(ptr), ptr, align);

test_stride::<u32>(ptr::invalid::<u32>(ptr), ptr, align);
Expand Down Expand Up @@ -672,7 +672,7 @@ fn align_offset_issue_103361() {
const SIZE: usize = 1 << 30;
#[cfg(target_pointer_width = "16")]
const SIZE: usize = 1 << 13;
struct HugeSize([u8; SIZE - 1]);
struct HugeSize(#[allow(dead_code)] [u8; SIZE - 1]);
let _ = ptr::invalid::<HugeSize>(SIZE).align_offset(SIZE);
}

Expand All @@ -684,7 +684,7 @@ fn align_offset_issue_103361_const() {
const SIZE: usize = 1 << 30;
#[cfg(target_pointer_width = "16")]
const SIZE: usize = 1 << 13;
struct HugeSize([u8; SIZE - 1]);
struct HugeSize(#[allow(dead_code)] [u8; SIZE - 1]);

const {
assert!(ptr::invalid::<HugeSize>(SIZE - 1).align_offset(SIZE) == SIZE - 1);
Expand Down Expand Up @@ -834,7 +834,7 @@ fn ptr_metadata_bounds() {
fn dyn_metadata() {
#[derive(Debug)]
#[repr(align(32))]
struct Something([u8; 47]);
struct Something(#[allow(dead_code)] [u8; 47]);

let value = Something([0; 47]);
let trait_object: &dyn Debug = &value;
Expand Down
6 changes: 3 additions & 3 deletions library/core/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2110,9 +2110,9 @@ fn test_align_to_zst() {
#[test]
fn test_align_to_non_trivial() {
#[repr(align(8))]
struct U64(u64, u64);
struct U64(#[allow(dead_code)] u64, #[allow(dead_code)] u64);
#[repr(align(8))]
struct U64U64U32(u64, u64, u32);
struct U64U64U32(#[allow(dead_code)] u64, #[allow(dead_code)] u64, #[allow(dead_code)] u32);
let data = [
U64(1, 2),
U64(3, 4),
Expand Down Expand Up @@ -2197,7 +2197,7 @@ fn test_slice_partition_dedup_multiple_ident() {
#[test]
fn test_slice_partition_dedup_partialeq() {
#[derive(Debug)]
struct Foo(i32, i32);
struct Foo(i32, #[allow(dead_code)] i32);

impl PartialEq for Foo {
fn eq(&self, other: &Foo) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/collections/hash/set/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ fn test_replace() {
use crate::hash;

#[derive(Debug)]
struct Foo(&'static str, i32);
struct Foo(&'static str, #[allow(dead_code)] i32);

impl PartialEq for Foo {
fn eq(&self, other: &Self) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions tests/codegen-units/item-collection/generic-drop-glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ enum EnumNoDrop<T1, T2> {
}


struct NonGenericNoDrop(#[allow(unused_tuple_struct_fields)] i32);
struct NonGenericNoDrop(#[allow(dead_code)] i32);

struct NonGenericWithDrop(#[allow(unused_tuple_struct_fields)] i32);
struct NonGenericWithDrop(#[allow(dead_code)] i32);
//~ MONO_ITEM fn std::ptr::drop_in_place::<NonGenericWithDrop> - shim(Some(NonGenericWithDrop)) @@ generic_drop_glue-cgu.0[Internal]

impl Drop for NonGenericWithDrop {
Expand Down
Loading

0 comments on commit 0ee2b06

Please sign in to comment.