Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dead-code-lint: de-dup multiple unused assoc functions #110277

Merged
merged 4 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,13 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalDefId) {
let module_items = tcx.hir_module_items(module);

for item in module_items.items() {
if let hir::ItemKind::Impl(impl_item) = tcx.hir().item(item).kind {
for item in impl_item.items {
visitor.check_definition(item.id.owner_id.def_id);
}
continue;
Ezrashaw marked this conversation as resolved.
Show resolved Hide resolved
}

if !live_symbols.contains(&item.owner_id.def_id) {
let parent = tcx.local_parent(item.owner_id.def_id);
if parent != module && !live_symbols.contains(&parent) {
Expand Down Expand Up @@ -900,10 +907,6 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalDefId) {
}
}

for impl_item in module_items.impl_items() {
visitor.check_definition(impl_item.owner_id.def_id);
}

for foreign_item in module_items.foreign_items() {
visitor.check_definition(foreign_item.owner_id.def_id);
}
Expand Down
40 changes: 20 additions & 20 deletions tests/ui/lint/dead-code/issue-85255.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,6 @@ note: the lint level is defined here
LL | #![warn(dead_code)]
| ^^^^^^^^^

warning: fields `a` and `b` are never read
--> $DIR/issue-85255.rs:19:5
|
LL | pub(crate) struct Foo1 {
| ---- fields in this struct
LL | a: i32,
| ^
LL | pub b: i32,
| ^

warning: fields `a` and `b` are never read
--> $DIR/issue-85255.rs:31:5
|
LL | pub(crate) struct Foo2 {
| ---- fields in this struct
LL | a: i32,
| ^
LL | pub b: i32,
| ^

warning: method `a` is never used
--> $DIR/issue-85255.rs:14:8
|
Expand All @@ -46,6 +26,16 @@ warning: method `b` is never used
LL | pub fn b(&self) -> i32 { 6 }
| ^

warning: fields `a` and `b` are never read
--> $DIR/issue-85255.rs:19:5
|
LL | pub(crate) struct Foo1 {
| ---- fields in this struct
LL | a: i32,
| ^
LL | pub b: i32,
| ^

warning: method `a` is never used
--> $DIR/issue-85255.rs:26:8
|
Expand All @@ -58,6 +48,16 @@ warning: method `b` is never used
LL | pub fn b(&self) -> i32 { 6 }
| ^

warning: fields `a` and `b` are never read
--> $DIR/issue-85255.rs:31:5
|
LL | pub(crate) struct Foo2 {
| ---- fields in this struct
LL | a: i32,
| ^
LL | pub b: i32,
| ^

warning: method `a` is never used
--> $DIR/issue-85255.rs:38:8
|
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/lint/dead-code/lint-dead-code-3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ note: the lint level is defined here
LL | #![deny(dead_code)]
| ^^^^^^^^^

error: method `foo` is never used
--> $DIR/lint-dead-code-3.rs:16:8
|
LL | fn foo(&self) {
| ^^^

error: function `bar` is never used
--> $DIR/lint-dead-code-3.rs:21:4
|
Expand All @@ -34,12 +40,6 @@ error: function `blah` is never used
LL | fn blah() {}
| ^^^^

error: method `foo` is never used
--> $DIR/lint-dead-code-3.rs:16:8
|
LL | fn foo(&self) {
| ^^^

error: function `free` is never used
--> $DIR/lint-dead-code-3.rs:62:8
|
Expand Down
27 changes: 27 additions & 0 deletions tests/ui/lint/dead-code/unused-assoc-fns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#![deny(unused)]

struct Foo;

impl Foo {
fn one() {}
//~^ ERROR associated function `one` is never used [dead_code]

fn two(&self) {}
//~^ ERROR method `two` is never used [dead_code]

// seperation between functions
// ...
// ...

fn used() {}

fn three(&self) {
//~^ ERROR method `three` is never used [dead_code]
Foo::one();
// ...
}
}

fn main() {
Foo::used();
}
27 changes: 27 additions & 0 deletions tests/ui/lint/dead-code/unused-assoc-fns.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error: associated function `one` is never used
--> $DIR/unused-assoc-fns.rs:6:8
|
LL | fn one() {}
| ^^^
|
note: the lint level is defined here
--> $DIR/unused-assoc-fns.rs:1:9
|
LL | #![deny(unused)]
| ^^^^^^
= note: `#[deny(dead_code)]` implied by `#[deny(unused)]`

error: method `two` is never used
--> $DIR/unused-assoc-fns.rs:9:8
|
LL | fn two(&self) {}
| ^^^

error: method `three` is never used
--> $DIR/unused-assoc-fns.rs:18:8
|
LL | fn three(&self) {
| ^^^^^

error: aborting due to 3 previous errors