cargo/diagnostics/rules/
unused_dependencies.rs1use std::path::Path;
2
3use cargo_util_schemas::manifest::TomlPackageBuild;
4use cargo_util_schemas::manifest::TomlToolLints;
5use cargo_util_terminal::report::AnnotationKind;
6use cargo_util_terminal::report::Group;
7use cargo_util_terminal::report::Level;
8use cargo_util_terminal::report::Origin;
9use cargo_util_terminal::report::Patch;
10use cargo_util_terminal::report::Snippet;
11use tracing::instrument;
12
13use super::STYLE;
14use crate::CargoResult;
15use crate::GlobalContext;
16use crate::core::Package;
17use crate::diagnostics::DiagnosticStats;
18use crate::diagnostics::Lint;
19use crate::diagnostics::LintLevel;
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 fn unused_build_dependencies_no_build_rs(
88 pkg: &Package,
89 manifest_path: &Path,
90 cargo_lints: &TomlToolLints,
91 stats: &mut DiagnosticStats,
92 gctx: &GlobalContext,
93) -> CargoResult<()> {
94 let (lint_level, reason) = LINT.level(
95 cargo_lints,
96 pkg.rust_version(),
97 pkg.manifest().unstable_features(),
98 );
99
100 if lint_level == LintLevel::Allow {
101 return Ok(());
102 }
103
104 let manifest_path = rel_cwd_manifest_path(manifest_path, gctx);
105
106 let manifest = pkg.manifest();
107 let Some(package) = &manifest.normalized_toml().package else {
108 return Ok(());
109 };
110 if package.build != Some(TomlPackageBuild::Auto(false)) {
111 return Ok(());
112 }
113
114 let document = manifest.document();
115 let contents = manifest.contents();
116
117 for (i, dep_name) in manifest
118 .normalized_toml()
119 .build_dependencies()
120 .iter()
121 .flat_map(|m| m.keys())
122 .enumerate()
123 {
124 let level = lint_level.to_diagnostic_level();
125 let emitted_source = LINT.emitted_source(lint_level, reason);
126
127 let mut primary = Group::with_title(level.primary_title(LINT.desc));
128 if let Some(document) = document
129 && let Some(contents) = contents
130 && let Some(span) = get_key_value_span(document, &["build-dependencies", dep_name])
131 {
132 let span = span.key.start..span.value.end;
133 primary = primary.element(
134 Snippet::source(contents)
135 .path(&manifest_path)
136 .annotation(AnnotationKind::Primary.span(span)),
137 );
138 } else {
139 primary = primary.element(Origin::path(&manifest_path));
140 }
141 if i == 0 {
142 primary = primary.element(Level::NOTE.message(emitted_source));
143 }
144 let mut report = vec![primary];
145 if let Some(document) = document
146 && let Some(contents) = contents
147 && let Some(span) = get_key_value_span(document, &["build-dependencies", dep_name])
148 {
149 let span = span.key.start..span.value.end;
150 let mut help = Group::with_title(Level::HELP.secondary_title("remove the dependency"));
151 help = help.element(
152 Snippet::source(contents)
153 .path(&manifest_path)
154 .patch(Patch::new(span, "")),
155 );
156 report.push(help);
157 }
158
159 stats.record_lint(lint_level);
160 gctx.shell().print_report(&report, lint_level.force())?;
161 }
162
163 Ok(())
164}