Skip to content

Commit 5113ed2

Browse files
committed
Auto merge of rust-lang#118297 - shepmaster:warn-dead-tuple-fields, r=WaffleLapkin
Merge `unused_tuple_struct_fields` into `dead_code` This implicitly upgrades the lint from `allow` to `warn` and places it into the `unused` lint group. [Discussion on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Moving.20.60unused_tuple_struct_fields.60.20from.20allow.20to.20warn)
2 parents a59a980 + 26194a3 commit 5113ed2

File tree

227 files changed

+406
-412
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

227 files changed

+406
-412
lines changed

compiler/rustc_lint/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ fn register_builtins(store: &mut LintStore) {
328328
store.register_renamed("disjoint_capture_migration", "rust_2021_incompatible_closure_captures");
329329
store.register_renamed("or_patterns_back_compat", "rust_2021_incompatible_or_patterns");
330330
store.register_renamed("non_fmt_panic", "non_fmt_panics");
331+
store.register_renamed("unused_tuple_struct_fields", "dead_code");
331332

332333
// These were moved to tool lints, but rustc still sees them when compiling normally, before
333334
// tool lints are registered, so `check_tool_name_for_backwards_compat` doesn't work. Use

compiler/rustc_lint_defs/src/builtin.rs

+7-29
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ declare_lint_pass! {
125125
UNUSED_MACROS,
126126
UNUSED_MUT,
127127
UNUSED_QUALIFICATIONS,
128-
UNUSED_TUPLE_STRUCT_FIELDS,
129128
UNUSED_UNSAFE,
130129
UNUSED_VARIABLES,
131130
USELESS_DEPRECATED,
@@ -697,8 +696,13 @@ declare_lint! {
697696
/// Dead code may signal a mistake or unfinished code. To silence the
698697
/// warning for individual items, prefix the name with an underscore such
699698
/// as `_foo`. If it was intended to expose the item outside of the crate,
700-
/// consider adding a visibility modifier like `pub`. Otherwise consider
701-
/// removing the unused code.
699+
/// consider adding a visibility modifier like `pub`.
700+
///
701+
/// To preserve the numbering of tuple structs with unused fields,
702+
/// change the unused fields to have unit type or use
703+
/// `PhantomData`.
704+
///
705+
/// Otherwise consider removing the unused code.
702706
pub DEAD_CODE,
703707
Warn,
704708
"detect unused, unexported items"
@@ -732,32 +736,6 @@ declare_lint! {
732736
"detects attributes that were not used by the compiler"
733737
}
734738

735-
declare_lint! {
736-
/// The `unused_tuple_struct_fields` lint detects fields of tuple structs
737-
/// that are never read.
738-
///
739-
/// ### Example
740-
///
741-
/// ```rust
742-
/// #[warn(unused_tuple_struct_fields)]
743-
/// struct S(i32, i32, i32);
744-
/// let s = S(1, 2, 3);
745-
/// let _ = (s.0, s.2);
746-
/// ```
747-
///
748-
/// {{produces}}
749-
///
750-
/// ### Explanation
751-
///
752-
/// Tuple struct fields that are never read anywhere may indicate a
753-
/// mistake or unfinished code. To silence this warning, consider
754-
/// removing the unused field(s) or, to preserve the numbering of the
755-
/// remaining fields, change the unused field(s) to have unit type.
756-
pub UNUSED_TUPLE_STRUCT_FIELDS,
757-
Allow,
758-
"detects tuple struct fields that are never read"
759-
}
760-
761739
declare_lint! {
762740
/// The `unreachable_code` lint detects unreachable code paths.
763741
///

compiler/rustc_passes/src/dead.rs

+35-23
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
1515
use rustc_middle::middle::privacy::Level;
1616
use rustc_middle::query::Providers;
1717
use rustc_middle::ty::{self, TyCtxt};
18-
use rustc_session::lint::builtin::{DEAD_CODE, UNUSED_TUPLE_STRUCT_FIELDS};
19-
use rustc_session::lint::{self, Lint, LintId};
18+
use rustc_session::lint;
19+
use rustc_session::lint::builtin::DEAD_CODE;
2020
use rustc_span::symbol::{sym, Symbol};
2121
use rustc_target::abi::FieldIdx;
2222
use std::mem;
@@ -766,6 +766,12 @@ enum ShouldWarnAboutField {
766766
No,
767767
}
768768

769+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
770+
enum ReportOn {
771+
TupleField,
772+
NamedField,
773+
}
774+
769775
impl<'tcx> DeadVisitor<'tcx> {
770776
fn should_warn_about_field(&mut self, field: &ty::FieldDef) -> ShouldWarnAboutField {
771777
if self.live_symbols.contains(&field.did.expect_local()) {
@@ -787,9 +793,9 @@ impl<'tcx> DeadVisitor<'tcx> {
787793
ShouldWarnAboutField::Yes
788794
}
789795

790-
fn def_lint_level(&self, lint: &'static Lint, id: LocalDefId) -> lint::Level {
796+
fn def_lint_level(&self, id: LocalDefId) -> lint::Level {
791797
let hir_id = self.tcx.local_def_id_to_hir_id(id);
792-
self.tcx.lint_level_at_node(lint, hir_id).0
798+
self.tcx.lint_level_at_node(DEAD_CODE, hir_id).0
793799
}
794800

795801
// # Panics
@@ -803,7 +809,7 @@ impl<'tcx> DeadVisitor<'tcx> {
803809
dead_codes: &[&DeadItem],
804810
participle: &str,
805811
parent_item: Option<LocalDefId>,
806-
lint: &'static Lint,
812+
report_on: ReportOn,
807813
) {
808814
let Some(&first_item) = dead_codes.first() else {
809815
return;
@@ -864,8 +870,8 @@ impl<'tcx> DeadVisitor<'tcx> {
864870
None
865871
};
866872

867-
let diag = if LintId::of(lint) == LintId::of(UNUSED_TUPLE_STRUCT_FIELDS) {
868-
MultipleDeadCodes::UnusedTupleStructFields {
873+
let diag = match report_on {
874+
ReportOn::TupleField => MultipleDeadCodes::UnusedTupleStructFields {
869875
multiple,
870876
num,
871877
descr,
@@ -874,29 +880,29 @@ impl<'tcx> DeadVisitor<'tcx> {
874880
change_fields_suggestion: ChangeFieldsToBeOfUnitType { num, spans: spans.clone() },
875881
parent_info,
876882
ignored_derived_impls,
877-
}
878-
} else {
879-
MultipleDeadCodes::DeadCodes {
883+
},
884+
885+
ReportOn::NamedField => MultipleDeadCodes::DeadCodes {
880886
multiple,
881887
num,
882888
descr,
883889
participle,
884890
name_list,
885891
parent_info,
886892
ignored_derived_impls,
887-
}
893+
},
888894
};
889895

890896
let hir_id = tcx.local_def_id_to_hir_id(first_item.def_id);
891-
self.tcx.emit_spanned_lint(lint, hir_id, MultiSpan::from_spans(spans), diag);
897+
self.tcx.emit_spanned_lint(DEAD_CODE, hir_id, MultiSpan::from_spans(spans), diag);
892898
}
893899

894900
fn warn_multiple(
895901
&self,
896902
def_id: LocalDefId,
897903
participle: &str,
898904
dead_codes: Vec<DeadItem>,
899-
lint: &'static Lint,
905+
report_on: ReportOn,
900906
) {
901907
let mut dead_codes = dead_codes
902908
.iter()
@@ -907,17 +913,17 @@ impl<'tcx> DeadVisitor<'tcx> {
907913
}
908914
dead_codes.sort_by_key(|v| v.level);
909915
for group in dead_codes[..].group_by(|a, b| a.level == b.level) {
910-
self.lint_at_single_level(&group, participle, Some(def_id), lint);
916+
self.lint_at_single_level(&group, participle, Some(def_id), report_on);
911917
}
912918
}
913919

914920
fn warn_dead_code(&mut self, id: LocalDefId, participle: &str) {
915921
let item = DeadItem {
916922
def_id: id,
917923
name: self.tcx.item_name(id.to_def_id()),
918-
level: self.def_lint_level(DEAD_CODE, id),
924+
level: self.def_lint_level(id),
919925
};
920-
self.lint_at_single_level(&[&item], participle, None, DEAD_CODE);
926+
self.lint_at_single_level(&[&item], participle, None, ReportOn::NamedField);
921927
}
922928

923929
fn check_definition(&mut self, def_id: LocalDefId) {
@@ -964,12 +970,12 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) {
964970
let def_id = item.id.owner_id.def_id;
965971
if !visitor.is_live_code(def_id) {
966972
let name = tcx.item_name(def_id.to_def_id());
967-
let level = visitor.def_lint_level(DEAD_CODE, def_id);
973+
let level = visitor.def_lint_level(def_id);
968974

969975
dead_items.push(DeadItem { def_id, name, level })
970976
}
971977
}
972-
visitor.warn_multiple(item.owner_id.def_id, "used", dead_items, DEAD_CODE);
978+
visitor.warn_multiple(item.owner_id.def_id, "used", dead_items, ReportOn::NamedField);
973979
}
974980

975981
if !live_symbols.contains(&item.owner_id.def_id) {
@@ -991,32 +997,38 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) {
991997
let def_id = variant.def_id.expect_local();
992998
if !live_symbols.contains(&def_id) {
993999
// Record to group diagnostics.
994-
let level = visitor.def_lint_level(DEAD_CODE, def_id);
1000+
let level = visitor.def_lint_level(def_id);
9951001
dead_variants.push(DeadItem { def_id, name: variant.name, level });
9961002
continue;
9971003
}
9981004

9991005
let is_positional = variant.fields.raw.first().map_or(false, |field| {
10001006
field.name.as_str().starts_with(|c: char| c.is_ascii_digit())
10011007
});
1002-
let lint = if is_positional { UNUSED_TUPLE_STRUCT_FIELDS } else { DEAD_CODE };
1008+
let report_on =
1009+
if is_positional { ReportOn::TupleField } else { ReportOn::NamedField };
10031010
let dead_fields = variant
10041011
.fields
10051012
.iter()
10061013
.filter_map(|field| {
10071014
let def_id = field.did.expect_local();
10081015
if let ShouldWarnAboutField::Yes = visitor.should_warn_about_field(field) {
1009-
let level = visitor.def_lint_level(lint, def_id);
1016+
let level = visitor.def_lint_level(def_id);
10101017
Some(DeadItem { def_id, name: field.name, level })
10111018
} else {
10121019
None
10131020
}
10141021
})
10151022
.collect();
1016-
visitor.warn_multiple(def_id, "read", dead_fields, lint);
1023+
visitor.warn_multiple(def_id, "read", dead_fields, report_on);
10171024
}
10181025

1019-
visitor.warn_multiple(item.owner_id.def_id, "constructed", dead_variants, DEAD_CODE);
1026+
visitor.warn_multiple(
1027+
item.owner_id.def_id,
1028+
"constructed",
1029+
dead_variants,
1030+
ReportOn::NamedField,
1031+
);
10201032
}
10211033
}
10221034

library/alloc/src/boxed.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
//! Creating a recursive data structure:
2525
//!
2626
//! ```
27+
//! ##[allow(dead_code)]
2728
//! #[derive(Debug)]
2829
//! enum List<T> {
2930
//! Cons(T, Box<List<T>>),

library/alloc/src/boxed/thin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ struct WithHeader<H>(NonNull<u8>, PhantomData<H>);
171171
/// An opaque representation of `WithHeader<H>` to avoid the
172172
/// projection invariance of `<T as Pointee>::Metadata`.
173173
#[repr(transparent)]
174-
#[allow(unused_tuple_struct_fields)] // Field only used through `WithHeader` type above.
174+
#[allow(dead_code)] // Field only used through `WithHeader` type above.
175175
struct WithOpaqueHeader(NonNull<u8>);
176176

177177
impl WithOpaqueHeader {

library/alloc/src/collections/btree/set/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ fn test_extend_ref() {
524524
#[test]
525525
fn test_recovery() {
526526
#[derive(Debug)]
527-
struct Foo(&'static str, i32);
527+
struct Foo(&'static str, #[allow(dead_code)] i32);
528528

529529
impl PartialEq for Foo {
530530
fn eq(&self, other: &Self) -> bool {

library/alloc/src/collections/vec_deque/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ fn test_clone_from() {
10851085
fn test_vec_deque_truncate_drop() {
10861086
static mut DROPS: u32 = 0;
10871087
#[derive(Clone)]
1088-
struct Elem(i32);
1088+
struct Elem(#[allow(dead_code)] i32);
10891089
impl Drop for Elem {
10901090
fn drop(&mut self) {
10911091
unsafe {

library/alloc/tests/autotraits.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
fn require_sync<T: Sync>(_: T) {}
22
fn require_send_sync<T: Send + Sync>(_: T) {}
33

4-
struct NotSend(*const ());
4+
struct NotSend(#[allow(dead_code)] *const ());
55
unsafe impl Sync for NotSend {}
66

77
#[test]

library/alloc/tests/vec.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ fn test_cmp() {
547547
#[test]
548548
fn test_vec_truncate_drop() {
549549
static mut DROPS: u32 = 0;
550-
struct Elem(i32);
550+
struct Elem(#[allow(dead_code)] i32);
551551
impl Drop for Elem {
552552
fn drop(&mut self) {
553553
unsafe {
@@ -1089,7 +1089,7 @@ fn test_into_iter_advance_by() {
10891089

10901090
#[test]
10911091
fn test_into_iter_drop_allocator() {
1092-
struct ReferenceCountedAllocator<'a>(DropCounter<'a>);
1092+
struct ReferenceCountedAllocator<'a>(#[allow(dead_code)] DropCounter<'a>);
10931093

10941094
unsafe impl Allocator for ReferenceCountedAllocator<'_> {
10951095
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, core::alloc::AllocError> {
@@ -2407,7 +2407,7 @@ fn test_vec_dedup_multiple_ident() {
24072407
#[test]
24082408
fn test_vec_dedup_partialeq() {
24092409
#[derive(Debug)]
2410-
struct Foo(i32, i32);
2410+
struct Foo(i32, #[allow(dead_code)] i32);
24112411

24122412
impl PartialEq for Foo {
24132413
fn eq(&self, other: &Foo) -> bool {

library/core/benches/slice.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fn binary_search_l3_worst_case(b: &mut Bencher) {
9191
}
9292

9393
#[derive(Clone)]
94-
struct Rgb(u8, u8, u8);
94+
struct Rgb(#[allow(dead_code)] u8, #[allow(dead_code)] u8, #[allow(dead_code)] u8);
9595

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

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

library/core/tests/any.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ fn any_unsized() {
122122
fn distinct_type_names() {
123123
// https://github.com/rust-lang/rust/issues/84666
124124

125-
struct Velocity(f32, f32);
125+
struct Velocity(#[allow(dead_code)] f32, #[allow(dead_code)] f32);
126126

127127
fn type_name_of_val<T>(_: T) -> &'static str {
128128
type_name::<T>()

library/core/tests/array.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ fn array_default_impl_avoids_leaks_on_panic() {
262262
use core::sync::atomic::{AtomicUsize, Ordering::Relaxed};
263263
static COUNTER: AtomicUsize = AtomicUsize::new(0);
264264
#[derive(Debug)]
265-
struct Bomb(usize);
265+
struct Bomb(#[allow(dead_code)] usize);
266266

267267
impl Default for Bomb {
268268
fn default() -> Bomb {

library/core/tests/atomic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ fn ptr_bitops() {
188188
#[cfg(any(not(target_arch = "arm"), target_os = "linux"))] // Missing intrinsic in compiler-builtins
189189
fn ptr_bitops_tagging() {
190190
#[repr(align(16))]
191-
struct Tagme(u128);
191+
struct Tagme(#[allow(dead_code)] u128);
192192

193193
let tagme = Tagme(1000);
194194
let ptr = &tagme as *const Tagme as *mut Tagme;

library/core/tests/intrinsics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::intrinsics::assume;
44
#[test]
55
fn test_typeid_sized_types() {
66
struct X;
7-
struct Y(u32);
7+
struct Y(#[allow(dead_code)] u32);
88

99
assert_eq!(TypeId::of::<X>(), TypeId::of::<X>());
1010
assert_eq!(TypeId::of::<Y>(), TypeId::of::<Y>());
@@ -14,8 +14,8 @@ fn test_typeid_sized_types() {
1414
#[test]
1515
fn test_typeid_unsized_types() {
1616
trait Z {}
17-
struct X(str);
18-
struct Y(dyn Z + 'static);
17+
struct X(#[allow(dead_code)] str);
18+
struct Y(#[allow(dead_code)] dyn Z + 'static);
1919

2020
assert_eq!(TypeId::of::<X>(), TypeId::of::<X>());
2121
assert_eq!(TypeId::of::<Y>(), TypeId::of::<Y>());

0 commit comments

Comments
 (0)