C0009 dead-pub¶
Rule: Public items of workspace libraries are used outside their crate.
| Code | C0009 (stable, never reused) |
| Id | dead-pub |
| Category | dead-code |
| Source | spirit_level |
| Fix | by hand |
Level per tier¶
| Tier | Level | Reason |
|---|---|---|
| small | off | |
| small-workspace | off | |
| medium | off | |
| big | warn |
rustc can't see pub items no other crate uses; in a big workspace they pile up (candidates, never fail) |
| ai | off | |
| embedded | off | |
| ui | off | |
| egui | off | |
| units-strict | off | |
| units-paranoid | off |
Parameters¶
| Name | Type | Default | Meaning |
|---|---|---|---|
count-tests |
boolean | false |
uses in test code (tests/, benches/, examples/, #[cfg(test)]) of other crates count |
skip |
list of strings | [] |
library crates whose public API is meant for users outside the workspace |
skip-published |
boolean | true |
skip crates that are published (no publish = false) |
Why¶
rustc's dead_code lint stops at the crate boundary: a pub item reachable from a library's root may be used by any other crate, so rustc never reports it. In a workspace that is the common case. An item made pub for one caller stays when the caller goes, and the library carries code nobody runs, tests for it, and docs for it. Its public surface only grows (see pub-surface).
This lint closes the gap for workspace libraries: it reports public items that no other crate of the workspace names.
What is checked¶
- Every library crate of the workspace that some other member depends on (or that its own package's binary uses). A library nothing in the workspace builds on is skipped: it is an API for outside users, or dead as a whole.
- Its public items:
pubitems of modules reachable from the root throughpub mod,pub useand globs, items re-exported by name, and exportedmacro_rules!. - An item is used when its name appears as an identifier in another crate's code, macro bodies included, or in the public face (signature, fields, variants, methods) of a used item of its crate. A type that only appears as the return type of a used function counts as used.
- Test code of other crates counts only with
count-tests = true. - Published crates (
publishnotfalse) are skipped unlessskip-published = false; list other crates whose API is meant for outside users inskip.
Why candidates¶
The check is by name, without type information. A different item with the same name hides a dead one. An item reached only through a macro, a derive, a trait method or a string (#[serde(with = "...")]) is reported although it is used. So every finding is a candidate: a place to look at, never a failed run.
Example¶
// crates/store/src/lib.rs
pub fn open(path: &Path) -> Store { ... } // used by the app crate
pub fn migrate_v1(path: &Path) { ... } // its caller went two months ago: reported
How to fix¶
- Nothing uses it: delete it, with its tests.
- Only its own crate uses it: make it
pub(crate)(or private), so rustc'sdead_codewatches it from now on. - Users outside the workspace need it: add the crate to
skip, or mark the crate published.
Configure¶
[lints.dead-pub]
level = "deny"
count-tests = false
skip = []
skip-published = true
reason = "why this project wants it at this level"
Allow one finding in the code with // spirit-level: allow(dead-pub) <reason> (waivers); the code works there too, as in [lints.C0009].