Skip to content

Commit 8c06287

Browse files
committed
docs(guide): Switch from third-party to first-party unused deps detection
With rust-lang#16938 merged, the quality is high enough to generally recommend, even to dig through potential false positives.
1 parent bbc9162 commit 8c06287

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

src/doc/src/guide/build-performance.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,35 @@ Trade-offs:
151151

152152
### Removing unused dependencies
153153

154-
Recommendation: Periodically review unused dependencies for removal using third-party tools like
155-
[cargo-machete](https://crates.io/crates/cargo-machete),
156-
[cargo-udeps](https://crates.io/crates/cargo-udeps),
157-
[cargo-shear](https://crates.io/crates/cargo-shear).
154+
Recommendation: periodically review unused dependencies for removal using:
155+
```console
156+
$ cargo +nightly check -Zcargo-lints --workspace --all-targets
157+
```
158+
This may have false positives from:
159+
- when a dependency's use is dynamically controlled by a `build.rs` or `RUSTFLAGS`
160+
161+
Also, periodically review hidden [`cargo::unused_dependencies`] results:
162+
```console
163+
$ CARGO_LOG=cargo::core::compiler::unused_deps=debug cargo +nightly check -Zcargo-lints --workspace --all-targets
164+
```
165+
This will show potential unused dependencies for
166+
- registry and git dependencies
167+
- when your [`package.rust-version`] is too old to use `[lints.cargo]`
168+
- when your dependency *might* be used to constrain a version on a transitive dependency (instead use `[target."cfg(false)".dependencies]`)
169+
- when your dependency *might* be used to activate features on a transitive dependency
170+
- your `[dev-dependencies]` as there is not a way yet to ensure all consumers of these are built
158171

159172
When changing code,
160173
it can be easy to miss that a dependency is no longer used and can be removed.
161174

162-
> **Note:** native support for this in Cargo is being tracked in [#15813](https://github.com/rust-lang/cargo/issues/15813).
163-
164175
Trade-offs:
165176
- ✅ Faster full build and link times
166-
- ❌ May incorrectly flag dependencies as unused or miss some
177+
-**Requires using nightly Rust and an [unstable Cargo feature][cargo-lints] when reviewing unused dependencies**
178+
- ❌ It takes effort to identifier unused dependencies from among the false positives
179+
180+
[cargo-lints]: ../reference/unstable.md#lintscargo
181+
[`cargo::unused_dependencies`]: ../reference/lints.md#unused_dependencies
182+
[`package.rust-version`]: ../reference/rust-version.md
167183

168184
### Removing unused features from dependencies
169185

0 commit comments

Comments
 (0)