Skip to content

Commit 923596e

Browse files
committed
Document -Ztreat-pub-as-pub-crate
1 parent edd0db4 commit 923596e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# `treat-pub-as-pub-crate`
2+
3+
The `-Ztreat-pub-as-pub-crate` flag causes the compiler to treat all `pub` items in the crate as if they were `pub(crate)`. This is useful for finding dead code in binary crates where public items are not intended to be exported to other crates.
4+
5+
When this flag is enabled, the dead code lint will warn about public items that are not used within the crate (unless they are the entry point, like `main`).
6+
7+
## Example
8+
9+
Consider the following code in a binary crate:
10+
11+
```rust
12+
pub fn unused_pub_fn() {}
13+
14+
fn main() {
15+
println!("Hello, world!");
16+
}
17+
```
18+
19+
By default, `rustc` assumes that `unused_pub_fn` might be used by other crates linking to this binary, so it does not warn about it being unused.
20+
21+
With `-Ztreat-pub-as-pub-crate`, `rustc` will emit a warning:
22+
23+
```text
24+
warning: function `unused_pub_fn` is never used
25+
--> src/main.rs:1:8
26+
|
27+
1 | pub fn unused_pub_fn() {}
28+
| ^^^^^^^^^^^^^
29+
|
30+
= note: `#[warn(dead_code)]` on by default
31+
```

0 commit comments

Comments
 (0)