cargo/diagnostics/rules/
unused_dependencies.rs1use std::path::Path;
2
3use cargo_util_schemas::manifest::TomlPackageBuild;
4use cargo_util_terminal::report::AnnotationKind;
5use cargo_util_terminal::report::Group;
6use cargo_util_terminal::report::Level;
7use cargo_util_terminal::report::Origin;
8use cargo_util_terminal::report::Patch;
9use cargo_util_terminal::report::Snippet;
10use tracing::instrument;
11
12use super::STYLE;
13use crate::CargoResult;
14use crate::GlobalContext;
15use crate::core::Package;
16use crate::core::Workspace;
17use crate::diagnostics::DiagnosticStats;
18use crate::diagnostics::Lint;
19use crate::diagnostics::LintLevelProduct;
20use crate::diagnostics::get_key_value_span;
21use crate::diagnostics::rel_cwd_manifest_path;
22
23pub static LINT: &Lint = &Lint {
24 name: "unused_dependencies",
25 desc: "unused dependency",
26 primary_group: &STYLE,
27 msrv: Some(super::CARGO_LINTS_MSRV),
28 feature_gate: None,
29 docs: Some(
30 r#"
31### What it does
32
33Checks for dependencies that are not used by any of the cargo targets.
34
35### Why it is bad
36
37Slows down compilation time.
38
39### Drawbacks
40
41The lint is only emitted in specific circumstances as multiple cargo targets exist for the
42different dependencies tables and they must all be built to know if a dependency is unused.
43Currently, only the selected packages are checked and not all `path` dependencies like most lints.
44The cargo target selection flags,
45independent of which packages are selected, determine which dependencies tables are checked.
46As there is no way to select all cargo targets that use `[dev-dependencies]`,
47they are unchecked.
48
49Examples:
50- `cargo check` will lint `[build-dependencies]` and `[dependencies]`
51- `cargo check --all-targets` will still only lint `[build-dependencies]` and `[dependencies]` and not `[dev-dependencoes]`
52- `cargo check --bin foo` will not lint `[dependencies]` even if `foo` is the only bin though `[build-dependencies]` will be checked
53- `cargo check -p foo` will not lint any dependencies tables for the `path` dependency `bar` even if `bar` only has a `[lib]`
54
55There can be false positives when depending on a transitive dependency to activate a feature.
56
57For false positives from pinning the version of a transitive dependency in `Cargo.toml`,
58move the dependency to the `target."cfg(false)".dependencies` table.
59
60### Example
61
62```toml
63[package]
64name = "foo"
65
66[dependencies]
67unused = "1"
68```
69
70Should be written as:
71
72```toml
73[package]
74name = "foo"
75```
76"#,
77 ),
78};
79
80#[instrument(skip_all)]
87pub(crate) fn lint_package(
88 _ws: &Workspace<'_>,
89 pkg: &Package,
90 manifest_path: &Path,
91 level: LintLevelProduct,
92 stats: &mut DiagnosticStats,
93 gctx: &GlobalContext,
94) -> CargoResult<()> {
95 let LintLevelProduct {
96 level: lint_level,
97 source,
98 } = level;
99
100 let manifest_path = rel_cwd_manifest_path(manifest_path, gctx);
101
102 let manifest = pkg.manifest();
103 let Some(package) = &manifest.normalized_toml().package else {
104 return Ok(());
105 };
106 if package.build != Some(TomlPackageBuild::Auto(false)) {
107 return Ok(());
108 }
109
110 let document = manifest.document();
111 let contents = manifest.contents();
112
113 for (i, dep_name) in manifest
114 .normalized_toml()
115 .build_dependencies()
116 .iter()
117 .flat_map(|m| m.keys())
118 .enumerate()
119 {
120 let level = lint_level.to_diagnostic_level();
121 let emitted_source = LINT.emitted_source(lint_level, source);
122
123 let mut primary = Group::with_title(level.primary_title(LINT.desc));
124 if let Some(document) = document
125 && let Some(contents) = contents
126 && let Some(span) = get_key_value_span(document, &["build-dependencies", dep_name])
127 {
128 let span = span.key.start..span.value.end;
129 primary = primary.element(
130 Snippet::source(contents)
131 .path(&manifest_path)
132 .annotation(AnnotationKind::Primary.span(span)),
133 );
134 } else {
135 primary = primary.element(Origin::path(&manifest_path));
136 }
137 if i == 0 {
138 primary = primary.element(Level::NOTE.message(emitted_source));
139 }
140 let mut report = vec![primary];
141 if let Some(document) = document
142 && let Some(contents) = contents
143 && let Some(span) = get_key_value_span(document, &["build-dependencies", dep_name])
144 {
145 let span = span.key.start..span.value.end;
146 let mut help = Group::with_title(Level::HELP.secondary_title("remove the dependency"));
147 help = help.element(
148 Snippet::source(contents)
149 .path(&manifest_path)
150 .patch(Patch::new(span, "")),
151 );
152 report.push(help);
153 }
154
155 stats.record_lint(lint_level);
156 gctx.shell().print_report(&report, lint_level.force())?;
157 }
158
159 Ok(())
160}