Address misc feedback

This commit is contained in:
Chinedu Francis Nwafili 2023-10-02 08:59:31 -04:00
parent feb8002502
commit 67379c4006
No known key found for this signature in database
GPG Key ID: 3C85D8F6538D8AD9
3 changed files with 20 additions and 53 deletions

View File

@ -1,6 +1,7 @@
// This implements the dead-code warning pass. It follows crate::reachable // This implements the dead-code warning pass.
// closely. The idea is that all reachable symbols are live, codes called // All reachable symbols are live, code called from live code is live, code with certain lint
// from live codes are live, and everything else is dead. // expectations such as `#[expect(unused)]` and `#[expect(dead_code)]` is live, and everything else
// is dead.
use hir::def_id::{LocalDefIdMap, LocalDefIdSet}; use hir::def_id::{LocalDefIdMap, LocalDefIdSet};
use itertools::Itertools; use itertools::Itertools;
@ -685,7 +686,7 @@ fn live_symbols_and_ignored_derived_traits(
(symbol_visitor.live_symbols, symbol_visitor.ignored_derived_traits) (symbol_visitor.live_symbols, symbol_visitor.ignored_derived_traits)
} }
struct DeadVariant { struct DeadItem {
def_id: LocalDefId, def_id: LocalDefId,
name: Symbol, name: Symbol,
level: lint::Level, level: lint::Level,
@ -729,7 +730,7 @@ fn should_warn_about_field(&mut self, field: &ty::FieldDef) -> ShouldWarnAboutFi
// first local def id. // first local def id.
// Prefer calling `Self.warn_dead_code` or `Self.warn_dead_code_grouped_by_lint_level` // Prefer calling `Self.warn_dead_code` or `Self.warn_dead_code_grouped_by_lint_level`
// since those methods group by lint level before calling this method. // since those methods group by lint level before calling this method.
fn warn_multiple_dead_codes( fn lint_at_single_level(
&self, &self,
dead_codes: &[LocalDefId], dead_codes: &[LocalDefId],
participle: &str, participle: &str,
@ -832,23 +833,23 @@ fn warn_multiple_dead_codes(
self.tcx.emit_spanned_lint(lint, first_hir_id, MultiSpan::from_spans(spans), diag); self.tcx.emit_spanned_lint(lint, first_hir_id, MultiSpan::from_spans(spans), diag);
} }
fn warn_dead_code_grouped_by_lint_level( fn warn_multiple(
&self, &self,
def_id: LocalDefId, def_id: LocalDefId,
participle: &str, participle: &str,
dead_codes: Vec<DeadVariant>, dead_codes: Vec<DeadItem>,
is_positional: bool, is_positional: bool,
) { ) {
let mut dead_codes = dead_codes let mut dead_codes = dead_codes
.iter() .iter()
.filter(|v| !v.name.as_str().starts_with('_')) .filter(|v| !v.name.as_str().starts_with('_'))
.collect::<Vec<&DeadVariant>>(); .collect::<Vec<&DeadItem>>();
if dead_codes.is_empty() { if dead_codes.is_empty() {
return; return;
} }
dead_codes.sort_by_key(|v| v.level); dead_codes.sort_by_key(|v| v.level);
for (_, group) in &dead_codes.into_iter().group_by(|v| v.level) { for (_, group) in &dead_codes.into_iter().group_by(|v| v.level) {
self.warn_multiple_dead_codes( self.lint_at_single_level(
&group.map(|v| v.def_id).collect::<Vec<_>>(), &group.map(|v| v.def_id).collect::<Vec<_>>(),
participle, participle,
Some(def_id), Some(def_id),
@ -858,7 +859,7 @@ fn warn_dead_code_grouped_by_lint_level(
} }
fn warn_dead_code(&mut self, id: LocalDefId, participle: &str) { fn warn_dead_code(&mut self, id: LocalDefId, participle: &str) {
self.warn_multiple_dead_codes(&[id], participle, None, false); self.lint_at_single_level(&[id], participle, None, false);
} }
fn check_definition(&mut self, def_id: LocalDefId) { fn check_definition(&mut self, def_id: LocalDefId) {
@ -907,13 +908,13 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalDefId) {
let def_id = item.id.owner_id.def_id; let def_id = item.id.owner_id.def_id;
if !visitor.is_live_code(def_id) { if !visitor.is_live_code(def_id) {
let name = tcx.item_name(def_id.to_def_id()); let name = tcx.item_name(def_id.to_def_id());
let hir = tcx.hir().local_def_id_to_hir_id(def_id); let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let level = tcx.lint_level_at_node(lint::builtin::DEAD_CODE, hir).0; let level = tcx.lint_level_at_node(lint::builtin::DEAD_CODE, hir_id).0;
dead_items.push(DeadVariant { def_id, name, level }) dead_items.push(DeadItem { def_id, name, level })
} }
} }
visitor.warn_dead_code_grouped_by_lint_level( visitor.warn_multiple(
item.owner_id.def_id, item.owner_id.def_id,
"used", "used",
dead_items, dead_items,
@ -942,7 +943,7 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalDefId) {
// Record to group diagnostics. // Record to group diagnostics.
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let level = tcx.lint_level_at_node(lint::builtin::DEAD_CODE, hir_id).0; let level = tcx.lint_level_at_node(lint::builtin::DEAD_CODE, hir_id).0;
dead_variants.push(DeadVariant { def_id, name: variant.name, level }); dead_variants.push(DeadItem { def_id, name: variant.name, level });
continue; continue;
} }
@ -967,13 +968,13 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalDefId) {
hir_id, hir_id,
) )
.0; .0;
Some(DeadVariant { def_id, name: field.name, level }) Some(DeadItem { def_id, name: field.name, level })
} else { } else {
None None
} }
}) })
.collect(); .collect();
visitor.warn_dead_code_grouped_by_lint_level( visitor.warn_multiple(
def_id, def_id,
"read", "read",
dead_fields, dead_fields,
@ -981,7 +982,7 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalDefId) {
) )
} }
visitor.warn_dead_code_grouped_by_lint_level( visitor.warn_multiple(
item.owner_id.def_id, item.owner_id.def_id,
"constructed", "constructed",
dead_variants, dead_variants,

View File

@ -1,32 +0,0 @@
// revisions: rpass1
//
// The corresponding ui test can be found in
// `tests/ui/lint/rfc-2383-lint-reason/expect_unused_inside_impl_block.rs`
#![feature(lint_reasons)]
#![warn(unused)]
struct OneUnused;
struct TwoUnused;
impl OneUnused {
#[expect(unused)]
fn unused() {}
}
impl TwoUnused {
#[expect(unused)]
fn unused1(){}
// This unused method has `#[expect(unused)]`, so the compiler should not emit a warning.
// This ui test was added after a regression in the compiler where it did not recognize multiple
// `#[expect(unused)]` annotations inside of impl blocks.
// issue 114416
#[expect(unused)]
fn unused2(){}
}
fn main() {
let _ = OneUnused;
let _ = TwoUnused;
}

View File

@ -1,7 +1,5 @@
// check-pass // check-pass
// // incremental
// The corresponding incremental compilation test can be found in
// `tests/incremental/issue-114416-expect-unused-inside-impl-block.rs`
#![feature(lint_reasons)] #![feature(lint_reasons)]
#![warn(unused)] #![warn(unused)]