Skip to main content

cargo/diagnostics/
mod.rs

1use cargo_util_schemas::manifest::RustVersion;
2use cargo_util_schemas::manifest::TomlToolLints;
3
4use crate::core::Workspace;
5use crate::core::{Edition, Features, MaybePackage, Package};
6
7mod lint;
8mod report;
9
10pub mod rules;
11
12pub use lint::{Lint, LintGroup, LintLevel, LintLevelSource};
13pub use report::{AsIndex, get_key_value, get_key_value_span, rel_cwd_manifest_path};
14pub use rules::{LINT_GROUPS, LINTS};
15
16/// Scope at which a lint runs: package-level or workspace-level.
17pub enum ManifestFor<'a> {
18    /// Lint runs for a specific package.
19    Package(&'a Package),
20    /// Lint runs for workspace-level config.
21    Workspace {
22        ws: &'a Workspace<'a>,
23        maybe_pkg: &'a MaybePackage,
24    },
25}
26
27impl ManifestFor<'_> {
28    fn lint_level(&self, pkg_lints: &TomlToolLints, lint: &Lint) -> (LintLevel, LintLevelSource) {
29        lint.level(pkg_lints, self.rust_version(), self.unstable_features())
30    }
31
32    pub fn rust_version(&self) -> Option<&RustVersion> {
33        match self {
34            ManifestFor::Package(p) => p.rust_version(),
35            ManifestFor::Workspace { ws, maybe_pkg: _ } => ws.lowest_rust_version(),
36        }
37    }
38
39    pub fn contents(&self) -> Option<&str> {
40        match self {
41            ManifestFor::Package(p) => p.manifest().contents(),
42            ManifestFor::Workspace { ws: _, maybe_pkg } => maybe_pkg.contents(),
43        }
44    }
45
46    pub fn document(&self) -> Option<&toml::Spanned<toml::de::DeTable<'static>>> {
47        match self {
48            ManifestFor::Package(p) => p.manifest().document(),
49            ManifestFor::Workspace { ws: _, maybe_pkg } => maybe_pkg.document(),
50        }
51    }
52
53    pub fn edition(&self) -> Edition {
54        match self {
55            ManifestFor::Package(p) => p.manifest().edition(),
56            ManifestFor::Workspace { ws: _, maybe_pkg } => maybe_pkg.edition(),
57        }
58    }
59
60    pub fn unstable_features(&self) -> &Features {
61        match self {
62            ManifestFor::Package(p) => p.manifest().unstable_features(),
63            ManifestFor::Workspace { ws: _, maybe_pkg } => maybe_pkg.unstable_features(),
64        }
65    }
66}
67
68impl<'a> From<&'a Package> for ManifestFor<'a> {
69    fn from(value: &'a Package) -> ManifestFor<'a> {
70        ManifestFor::Package(value)
71    }
72}
73
74impl<'a> From<(&'a Workspace<'a>, &'a MaybePackage)> for ManifestFor<'a> {
75    fn from((ws, maybe_pkg): (&'a Workspace<'a>, &'a MaybePackage)) -> ManifestFor<'a> {
76        ManifestFor::Workspace { ws, maybe_pkg }
77    }
78}