cargo/diagnostics/rules/
missing_lints_features.rs1use std::path::Path;
2
3use cargo_util_schemas::manifest::TomlToolLints;
4use cargo_util_terminal::report::AnnotationKind;
5use cargo_util_terminal::report::Group;
6use cargo_util_terminal::report::Level;
7use cargo_util_terminal::report::Snippet;
8use tracing::instrument;
9
10use super::find_lint_or_group;
11use crate::CargoResult;
12use crate::GlobalContext;
13use crate::core::Feature;
14use crate::diagnostics::DiagnosticStats;
15use crate::diagnostics::ManifestFor;
16use crate::diagnostics::get_key_value_span;
17use crate::diagnostics::rel_cwd_manifest_path;
18
19#[instrument(skip_all)]
20pub fn missing_lints_features(
21 manifest: ManifestFor<'_>,
22 manifest_path: &Path,
23 cargo_lints: &TomlToolLints,
24 stats: &mut DiagnosticStats,
25 gctx: &GlobalContext,
26) -> CargoResult<()> {
27 let manifest_path = rel_cwd_manifest_path(manifest_path, gctx);
28 for lint_name in cargo_lints.keys().map(|name| name) {
29 let Some((name, default_level, feature_gate)) = find_lint_or_group(lint_name) else {
30 continue;
31 };
32
33 let (_, source, _) =
34 crate::diagnostics::lint::level_priority(name, *default_level, cargo_lints);
35
36 if !source.is_user_specified() {
38 continue;
39 }
40
41 if let Some(feature_gate) = feature_gate
43 && !manifest.unstable_features().is_enabled(feature_gate)
44 {
45 report_feature_not_enabled(name, feature_gate, &manifest, &manifest_path, stats, gctx)?;
46 }
47 }
48
49 Ok(())
50}
51
52fn report_feature_not_enabled(
53 lint_name: &str,
54 feature_gate: &Feature,
55 manifest: &ManifestFor<'_>,
56 manifest_path: &str,
57 stats: &mut DiagnosticStats,
58 gctx: &GlobalContext,
59) -> CargoResult<()> {
60 let dash_feature_name = feature_gate.name().replace("_", "-");
61
62 let mut error = Group::with_title(
63 Level::ERROR.primary_title(format!("use of unstable lint `{lint_name}`")),
64 );
65
66 if let Some(document) = manifest.document()
67 && let Some(contents) = manifest.contents()
68 {
69 let key_path = match manifest {
70 ManifestFor::Package(_) => &["lints", "cargo", lint_name][..],
71 ManifestFor::Workspace { .. } => &["workspace", "lints", "cargo", lint_name][..],
72 };
73 let Some(span) = get_key_value_span(document, key_path) else {
74 return Ok(());
76 };
77
78 error = error.element(Snippet::source(contents).path(manifest_path).annotation(
79 AnnotationKind::Primary.span(span.key).label(format!(
80 "this is behind `{dash_feature_name}`, which is not enabled"
81 )),
82 ))
83 }
84
85 let report = [error.element(Level::HELP.message(format!(
86 "consider adding `cargo-features = [\"{dash_feature_name}\"]` to the top of the manifest"
87 )))];
88
89 stats.record_error();
90 gctx.shell().print_report(&report, true)?;
91
92 Ok(())
93}