Skip to content

dead_code lint misses re-exported type aliases #14421

Closed
@pnkfelix

Description

@pnkfelix

Summary: A library might want to have a single private type-parametric struct that is only exposed via a collection of named instantiations of that struct. The dead_code lint is issuing a false warning about dead code that is in fact reachable from the library via the re-exported type alias.

Consider the following library and client-code pair:

bug_lib.rs:

#![crate_type="lib"]

pub use src::aliases::B;
pub use src::hidden_core::make;

mod src {
    pub mod aliases {
        use super::hidden_core::A;
        pub type B = A<f32>;
    }

    pub mod hidden_core {
        use super::aliases::B;

        pub struct A<T>;

        pub fn make() -> B { A }

        impl<T> A<T> {
            pub fn foo(&mut self) { println!("called foo"); }
        }
    }
}

bug_client.rs

extern crate bug_lib;

use bug_lib::B;
use bug_lib::make;

fn main() {
    let mut an_A : B  = make();
    an_A.foo();
}

Transcript of rustc invocation illustrated false warning (and the fact that code is not dead):

% ./x86_64-apple-darwin/stage2/bin/rustc --version
rustc 0.11.0-pre (e402e75 2014-05-22 13:31:24 -0700)
host: x86_64-apple-darwin
% ./x86_64-apple-darwin/stage2/bin/rustc --out-dir /tmp /tmp/bug_lib.rs && ./x86_64-apple-darwin/stage2/bin/rustc -L /tmp/ /tmp/bug_client.rs
/tmp/bug_lib.rs:20:13: 20:62 warning: code is never used: `foo`, #[warn(dead_code)] on by default
/tmp/bug_lib.rs:20             pub fn foo(&mut self) { println!("called foo"); }
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% ./bug_client
called foo
% 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions