Commit 906fa6c
authored
feat(lints): Emit unused_dependencies lint (#16600)
*[View all
comments](https://triagebot.infra.rust-lang.org/gh-comments/rust-lang/cargo/pull/16600)*
### What does this PR try to resolve?
Fixes #15813
```toml
[lints.cargo]
unused_dependencies = "warn"
```
This checks only build and normal dependencies (see below for more
details). I would expect we'd open a new issue to track
dev-dependencies. I don't consider this a false-positive because there
technically isn't a way to ask for everything that uses dev-dependencies
to be built.
This only checks selected packages, and not all local packages, and only
if the build target selection flags are exhaustive without consideration
for the selected packages. See
#16600 (comment)
Known false positives:
- activating a feature on a transitive dependency
- pinning a transitive dependency (instead use
`target.cfg(false).dependencies`)
To handle false positives,
```toml
[lints.cargo]
unused_dependencies = { level = "warn", ignore = ["dep_name"] }
```
- Some prior art use `ignored` but I felt like `ignore` fit in better
with `allow`
- Per-dep-table is more complicated for users, tricky to get the design
right (`udeps` cares about dep kinds but not target cfgs), these
shouldn't be too common, and using dep names gives users control over
uniqueness
- For more background on alternatives, see
rust-lang/rfcs#3920
### How to test and review this PR?
Built-on #8437
This does nothing for dev-dependencies because there isn't really a way
to select all targets today without
- tracking selected dep kinds to check on a per-package basis
- checking the status of every bench to see if it can work as a test
because `cargo test` (no args) with benches set to test is the only
command today that can exercise all dev-dependencies as it is the only
one that will compile tests and doctests.
See also
-
https://blog.rust-lang.org/inside-rust/2024/10/01/this-development-cycle-in-cargo-1.82/#detecting-unused-dependencies
-
https://blog.rust-lang.org/inside-rust/2024/10/01/this-development-cycle-in-cargo-1.82/#all-targets-and-doc-tests
-
https://blog.rust-lang.org/inside-rust/2024/10/31/this-development-cycle-in-cargo-1.83/#target-and-target
As for the commits, I did something unusual for myself in that I made
changes with dead code so it is easier to understand what goes into one
step in this process without seeing the entire process at once and
getting confused.22 files changed
Lines changed: 2460 additions & 29 deletions
File tree
- src
- cargo
- core
- compiler
- build_context
- job_queue
- lints
- rules
- ops/cargo_compile
- util
- toml
- doc/src/reference
- tests/testsuite/lints
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
64 | 65 | | |
65 | 66 | | |
66 | 67 | | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
67 | 71 | | |
68 | 72 | | |
69 | 73 | | |
| |||
97 | 101 | | |
98 | 102 | | |
99 | 103 | | |
| 104 | + | |
100 | 105 | | |
101 | 106 | | |
102 | 107 | | |
| |||
118 | 123 | | |
119 | 124 | | |
120 | 125 | | |
| 126 | + | |
121 | 127 | | |
122 | 128 | | |
123 | 129 | | |
| |||
157 | 163 | | |
158 | 164 | | |
159 | 165 | | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
219 | 219 | | |
220 | 220 | | |
221 | 221 | | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
222 | 231 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
138 | 138 | | |
139 | 139 | | |
140 | 140 | | |
| 141 | + | |
141 | 142 | | |
142 | 143 | | |
143 | 144 | | |
| |||
187 | 188 | | |
188 | 189 | | |
189 | 190 | | |
| 191 | + | |
190 | 192 | | |
191 | 193 | | |
192 | 194 | | |
| |||
385 | 387 | | |
386 | 388 | | |
387 | 389 | | |
| 390 | + | |
388 | 391 | | |
389 | 392 | | |
390 | 393 | | |
| |||
504 | 507 | | |
505 | 508 | | |
506 | 509 | | |
| 510 | + | |
507 | 511 | | |
508 | 512 | | |
509 | 513 | | |
| |||
544 | 548 | | |
545 | 549 | | |
546 | 550 | | |
547 | | - | |
548 | | - | |
549 | | - | |
550 | | - | |
551 | | - | |
552 | | - | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
553 | 555 | | |
554 | 556 | | |
555 | 557 | | |
| |||
743 | 745 | | |
744 | 746 | | |
745 | 747 | | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
746 | 753 | | |
747 | 754 | | |
748 | 755 | | |
| |||
783 | 790 | | |
784 | 791 | | |
785 | 792 | | |
786 | | - | |
787 | | - | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
788 | 796 | | |
789 | 797 | | |
790 | 798 | | |
791 | 799 | | |
792 | 800 | | |
793 | | - | |
| 801 | + | |
794 | 802 | | |
795 | 803 | | |
796 | 804 | | |
| |||
834 | 842 | | |
835 | 843 | | |
836 | 844 | | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
837 | 857 | | |
838 | 858 | | |
839 | 859 | | |
| |||
874 | 894 | | |
875 | 895 | | |
876 | 896 | | |
877 | | - | |
| 897 | + | |
878 | 898 | | |
879 | 899 | | |
880 | 900 | | |
| |||
889 | 909 | | |
890 | 910 | | |
891 | 911 | | |
892 | | - | |
| 912 | + | |
893 | 913 | | |
894 | 914 | | |
895 | | - | |
| 915 | + | |
896 | 916 | | |
897 | 917 | | |
898 | 918 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| 54 | + | |
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
| |||
74 | 75 | | |
75 | 76 | | |
76 | 77 | | |
| 78 | + | |
77 | 79 | | |
78 | 80 | | |
79 | 81 | | |
| |||
832 | 834 | | |
833 | 835 | | |
834 | 836 | | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
835 | 843 | | |
836 | 844 | | |
837 | 845 | | |
| |||
1175 | 1183 | | |
1176 | 1184 | | |
1177 | 1185 | | |
1178 | | - | |
1179 | 1186 | | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
1180 | 1191 | | |
1181 | 1192 | | |
1182 | 1193 | | |
| |||
1186 | 1197 | | |
1187 | 1198 | | |
1188 | 1199 | | |
1189 | | - | |
1190 | 1200 | | |
1191 | 1201 | | |
1192 | 1202 | | |
1193 | | - | |
1194 | 1203 | | |
1195 | 1204 | | |
1196 | 1205 | | |
| |||
2376 | 2385 | | |
2377 | 2386 | | |
2378 | 2387 | | |
| 2388 | + | |
| 2389 | + | |
| 2390 | + | |
| 2391 | + | |
| 2392 | + | |
| 2393 | + | |
| 2394 | + | |
| 2395 | + | |
| 2396 | + | |
| 2397 | + | |
| 2398 | + | |
| 2399 | + | |
| 2400 | + | |
2379 | 2401 | | |
2380 | 2402 | | |
2381 | 2403 | | |
| |||
0 commit comments