Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cargo/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub mod passes;
pub mod rules;

pub use lint::{Lint, LintGroup, LintLevel, LintLevelProduct, LintLevelSource};
pub use report::{AsIndex, get_key_value, get_key_value_span, rel_cwd_manifest_path};
pub use report::{AsIndex, cwd_rel_path, get_key_value, get_key_value_span, workspace_rel_path};
pub use rules::{LINT_GROUPS, LINTS};

pub struct GlobalDiagnosticStats {
Expand Down
23 changes: 19 additions & 4 deletions src/cargo/diagnostics/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ pub enum ParsePassRule<'r> {
},
}

type FnDiagnosticManifest =
fn(ManifestFor<'_>, &Path, &mut ScopedDiagnosticStats<'_>, &GlobalContext) -> CargoResult<()>;
type FnDiagnosticManifest = fn(
&Workspace<'_>,
ManifestFor<'_>,
&Path,
&mut ScopedDiagnosticStats<'_>,
&GlobalContext,
) -> CargoResult<()>;

type FnDiagnosticWorkspace = fn(
&Workspace<'_>,
Expand All @@ -59,6 +64,7 @@ type FnDiagnosticPackage = fn(
) -> CargoResult<()>;

type FnLintManifest = fn(
&Workspace<'_>,
manifest: ManifestFor<'_>,
manifest_path: &Path,
LintLevelProduct,
Expand Down Expand Up @@ -127,14 +133,21 @@ fn emit_parse_pkg_diagnostics(
match rule {
ParsePassRule::DiagnosticManifest { rule } => {
let manifest = pkg.into();
rule(manifest, &path, &mut pkg_stats, workspace.gctx())?;
rule(workspace, manifest, &path, &mut pkg_stats, workspace.gctx())?;
}
ParsePassRule::LintManifest { rule, lint } => {
if workspace.gctx().cli_unstable().cargo_lints {
let manifest: ManifestFor<'_> = pkg.into();
let level = manifest.lint_level(&cargo_lints, lint);
if level.level != LintLevel::Allow {
rule(manifest, &path, level, &mut pkg_stats, workspace.gctx())?;
rule(
workspace,
manifest,
&path,
level,
&mut pkg_stats,
workspace.gctx(),
)?;
}
}
}
Expand Down Expand Up @@ -203,6 +216,7 @@ fn emit_parse_ws_diagnostics(
ParsePassRule::DiagnosticManifest { rule } => {
let manifest = (workspace, workspace.root_maybe()).into();
rule(
workspace,
manifest,
workspace.root_manifest(),
&mut pkg_stats,
Expand All @@ -215,6 +229,7 @@ fn emit_parse_ws_diagnostics(
let level = manifest.lint_level(&cargo_lints, lint);
if level.level != LintLevel::Allow {
rule(
workspace,
manifest,
workspace.root_manifest(),
level,
Expand Down
29 changes: 26 additions & 3 deletions src/cargo/diagnostics/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,36 @@ use std::borrow::Cow;
use std::ops::Range;
use std::path::Path;

use cargo_util::paths::normalize_path;
use pathdiff::diff_paths;

use crate::GlobalContext;
use crate::core::Workspace;

/// Gets the relative path to a manifest from the current working directory, or
/// the absolute path of the manifest if a relative path cannot be constructed
pub fn rel_cwd_manifest_path(path: &Path, gctx: &GlobalContext) -> String {
/// Display path, generally relative to the workspace
///
/// Mirrors [`crate::util::path_args`]
pub fn workspace_rel_path(ws: &Workspace<'_>, path: &Path) -> String {
// Determine which path we make this relative to: usually it's the workspace root,
// but this can be overwritten with a `-Z` flag.
let root = match &ws.gctx().cli_unstable().root_dir {
None => ws.root().to_owned(),
Some(root_dir) => normalize_path(&ws.gctx().cwd().join(root_dir)),
};
if let Ok(path) = path.strip_prefix(&root) {
path
} else {
path
}
.display()
.to_string()
}

/// Display path, generally relative to cwd
///
/// Prefer [`workspace_rel_path`].
/// This is for when there is no workspace available.
pub fn cwd_rel_path(path: &Path, gctx: &GlobalContext) -> String {
diff_paths(path, gctx.cwd())
.unwrap_or_else(|| path.to_path_buf())
.display()
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/diagnostics/rules/blanket_hint_mostly_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::diagnostics::Lint;
use crate::diagnostics::LintLevelProduct;
use crate::diagnostics::ScopedDiagnosticStats;
use crate::diagnostics::get_key_value_span;
use crate::diagnostics::rel_cwd_manifest_path;
use crate::diagnostics::workspace_rel_path;

pub static LINT: &Lint = &Lint {
name: "blanket_hint_mostly_unused",
Expand Down Expand Up @@ -57,7 +57,7 @@ hint-mostly-unused = true

#[instrument(skip_all)]
pub(crate) fn lint_workspace(
_ws: &Workspace<'_>,
ws: &Workspace<'_>,
maybe_pkg: &MaybePackage,
path: &Path,
level: LintLevelProduct,
Expand All @@ -70,7 +70,7 @@ pub(crate) fn lint_workspace(
} = level;

let level = lint_level.to_diagnostic_level();
let manifest_path = rel_cwd_manifest_path(path, gctx);
let manifest_path = workspace_rel_path(ws, path);
let mut paths = Vec::new();

if let Some(profiles) = maybe_pkg.profiles() {
Expand Down
6 changes: 4 additions & 2 deletions src/cargo/diagnostics/rules/deferred_parse_diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use tracing::instrument;
use crate::CargoResult;
use crate::GlobalContext;
use crate::core::MaybePackage;
use crate::core::Workspace;
use crate::diagnostics::ManifestFor;
use crate::diagnostics::ScopedDiagnosticStats;
use crate::diagnostics::rel_cwd_manifest_path;
use crate::diagnostics::workspace_rel_path;

#[instrument(skip_all)]
pub(crate) fn diagnose_manifest(
ws: &Workspace<'_>,
manifest: ManifestFor<'_>,
manifest_path: &Path,
pkg_stats: &mut ScopedDiagnosticStats<'_>,
Expand All @@ -31,7 +33,7 @@ pub(crate) fn diagnose_manifest(
}
};

let manifest_path = rel_cwd_manifest_path(manifest_path, gctx);
let manifest_path = workspace_rel_path(ws, manifest_path);
for warning in warnings {
let msg = format!("{manifest_path}: {}", warning.message);
if warning.is_critical {
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/diagnostics/rules/im_a_teapot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::diagnostics::Lint;
use crate::diagnostics::LintLevelProduct;
use crate::diagnostics::ScopedDiagnosticStats;
use crate::diagnostics::get_key_value_span;
use crate::diagnostics::rel_cwd_manifest_path;
use crate::diagnostics::workspace_rel_path;

/// This lint is only to be used for testing purposes
pub static LINT: &Lint = &Lint {
Expand All @@ -31,7 +31,7 @@ pub static LINT: &Lint = &Lint {

#[instrument(skip_all)]
pub(crate) fn lint_package(
_ws: &Workspace<'_>,
ws: &Workspace<'_>,
pkg: &Package,
path: &Path,
level: LintLevelProduct,
Expand All @@ -50,7 +50,7 @@ pub(crate) fn lint_package(
.is_some_and(|p| p.im_a_teapot.is_some())
{
let level = lint_level.to_diagnostic_level();
let manifest_path = rel_cwd_manifest_path(path, gctx);
let manifest_path = workspace_rel_path(ws, path);
let emitted_source = LINT.emitted_source(lint_level, source);

let mut desc = Group::with_title(level.primary_title(LINT.desc));
Expand Down
10 changes: 5 additions & 5 deletions src/cargo/diagnostics/rules/implicit_minimum_version_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::diagnostics::LintLevelProduct;
use crate::diagnostics::LintLevelSource;
use crate::diagnostics::ScopedDiagnosticStats;
use crate::diagnostics::get_key_value;
use crate::diagnostics::rel_cwd_manifest_path;
use crate::diagnostics::workspace_rel_path;
use crate::util::OptVersionReq;

pub static LINT: &Lint = &Lint {
Expand Down Expand Up @@ -86,7 +86,7 @@ serde = "1.0.219"

#[instrument(skip_all)]
pub(crate) fn lint_package(
_ws: &Workspace<'_>,
ws: &Workspace<'_>,
pkg: &Package,
manifest_path: &Path,
level: LintLevelProduct,
Expand All @@ -98,7 +98,7 @@ pub(crate) fn lint_package(
source,
} = level;

let manifest_path = rel_cwd_manifest_path(manifest_path, gctx);
let manifest_path = workspace_rel_path(ws, manifest_path);

let manifest = pkg.manifest();

Expand Down Expand Up @@ -147,7 +147,7 @@ pub(crate) fn lint_package(

#[instrument(skip_all)]
pub(crate) fn lint_workspace(
_ws: &Workspace<'_>,
ws: &Workspace<'_>,
maybe_pkg: &MaybePackage,
manifest_path: &Path,
level: LintLevelProduct,
Expand All @@ -159,7 +159,7 @@ pub(crate) fn lint_workspace(
source,
} = level;

let manifest_path = rel_cwd_manifest_path(manifest_path, gctx);
let manifest_path = workspace_rel_path(ws, manifest_path);

let document = maybe_pkg.document();
let contents = maybe_pkg.contents();
Expand Down
11 changes: 7 additions & 4 deletions src/cargo/diagnostics/rules/missing_lints_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ use crate::CargoResult;
use crate::GlobalContext;
use crate::core::Feature;
use crate::core::MaybePackage;
use crate::core::Workspace;
use crate::diagnostics::ManifestFor;
use crate::diagnostics::ScopedDiagnosticStats;
use crate::diagnostics::get_key_value_span;
use crate::diagnostics::rel_cwd_manifest_path;
use crate::diagnostics::workspace_rel_path;

#[instrument(skip_all)]
pub(crate) fn diagnose_manifest(
ws: &Workspace<'_>,
manifest: ManifestFor<'_>,
manifest_path: &Path,
pkg_stats: &mut ScopedDiagnosticStats<'_>,
Expand Down Expand Up @@ -51,23 +53,24 @@ pub(crate) fn diagnose_manifest(
.and_then(|lints| lints.get("cargo"));

if let Some(cargo_lints) = ws_lints {
diagnose_manifest_inner(&manifest, manifest_path, cargo_lints, pkg_stats, gctx)?;
diagnose_manifest_inner(ws, &manifest, manifest_path, cargo_lints, pkg_stats, gctx)?;
}
if let Some(cargo_lints) = pkg_lints {
diagnose_manifest_inner(&manifest, manifest_path, cargo_lints, pkg_stats, gctx)?;
diagnose_manifest_inner(ws, &manifest, manifest_path, cargo_lints, pkg_stats, gctx)?;
}

Ok(())
}

fn diagnose_manifest_inner(
ws: &Workspace<'_>,
manifest: &ManifestFor<'_>,
manifest_path: &Path,
cargo_lints: &manifest::TomlToolLints,
pkg_stats: &mut ScopedDiagnosticStats<'_>,
gctx: &GlobalContext,
) -> CargoResult<()> {
let manifest_path = rel_cwd_manifest_path(manifest_path, gctx);
let manifest_path = workspace_rel_path(ws, manifest_path);
for lint_name in cargo_lints.keys().map(|name| name) {
let Some((name, default_level, feature_gate)) = find_lint_or_group(lint_name) else {
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/diagnostics/rules/missing_lints_inheritance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::core::Workspace;
use crate::diagnostics::Lint;
use crate::diagnostics::LintLevelProduct;
use crate::diagnostics::ScopedDiagnosticStats;
use crate::diagnostics::rel_cwd_manifest_path;
use crate::diagnostics::workspace_rel_path;

pub static LINT: &Lint = &Lint {
name: "missing_lints_inheritance",
Expand Down Expand Up @@ -87,7 +87,7 @@ pub(crate) fn lint_package(
let contents = manifest.contents();
let level = lint_level.to_diagnostic_level();
let emitted_source = LINT.emitted_source(lint_level, source);
let manifest_path = rel_cwd_manifest_path(manifest_path, gctx);
let manifest_path = workspace_rel_path(ws, manifest_path);

let mut primary = Group::with_title(level.primary_title(LINT.desc));
primary = primary.element(Origin::path(&manifest_path));
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/diagnostics/rules/non_kebab_case_bins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::diagnostics::LintLevelProduct;
use crate::diagnostics::LintLevelSource;
use crate::diagnostics::ScopedDiagnosticStats;
use crate::diagnostics::get_key_value_span;
use crate::diagnostics::rel_cwd_manifest_path;
use crate::diagnostics::workspace_rel_path;

pub static LINT: &Lint = &Lint {
name: "non_kebab_case_bins",
Expand Down Expand Up @@ -77,7 +77,7 @@ pub(crate) fn lint_package(
source,
} = level;

let manifest_path = rel_cwd_manifest_path(manifest_path, gctx);
let manifest_path = workspace_rel_path(ws, manifest_path);

lint_package_inner(ws, pkg, &manifest_path, lint_level, source, pkg_stats, gctx)
}
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/diagnostics/rules/non_kebab_case_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::diagnostics::LintLevelProduct;
use crate::diagnostics::LintLevelSource;
use crate::diagnostics::ScopedDiagnosticStats;
use crate::diagnostics::get_key_value_span;
use crate::diagnostics::rel_cwd_manifest_path;
use crate::diagnostics::workspace_rel_path;

pub static LINT: &Lint = &Lint {
name: "non_kebab_case_features",
Expand Down Expand Up @@ -60,7 +60,7 @@ foo-bar = []

#[instrument(skip_all)]
pub(crate) fn lint_package(
_ws: &Workspace<'_>,
ws: &Workspace<'_>,
pkg: &Package,
manifest_path: &Path,
level: LintLevelProduct,
Expand All @@ -72,7 +72,7 @@ pub(crate) fn lint_package(
source,
} = level;

let manifest_path = rel_cwd_manifest_path(manifest_path, gctx);
let manifest_path = workspace_rel_path(ws, manifest_path);

lint_package_inner(pkg, &manifest_path, lint_level, source, pkg_stats, gctx)
}
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/diagnostics/rules/non_kebab_case_packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::diagnostics::LintLevelProduct;
use crate::diagnostics::LintLevelSource;
use crate::diagnostics::ScopedDiagnosticStats;
use crate::diagnostics::get_key_value_span;
use crate::diagnostics::rel_cwd_manifest_path;
use crate::diagnostics::workspace_rel_path;

pub static LINT: &Lint = &Lint {
name: "non_kebab_case_packages",
Expand Down Expand Up @@ -60,7 +60,7 @@ name = "foo-bar"

#[instrument(skip_all)]
pub(crate) fn lint_package(
_ws: &Workspace<'_>,
ws: &Workspace<'_>,
pkg: &Package,
manifest_path: &Path,
level: LintLevelProduct,
Expand All @@ -72,7 +72,7 @@ pub(crate) fn lint_package(
source,
} = level;

let manifest_path = rel_cwd_manifest_path(manifest_path, gctx);
let manifest_path = workspace_rel_path(ws, manifest_path);

lint_package_inner(pkg, &manifest_path, lint_level, source, pkg_stats, gctx)
}
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/diagnostics/rules/non_snake_case_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::diagnostics::LintLevelProduct;
use crate::diagnostics::LintLevelSource;
use crate::diagnostics::ScopedDiagnosticStats;
use crate::diagnostics::get_key_value_span;
use crate::diagnostics::rel_cwd_manifest_path;
use crate::diagnostics::workspace_rel_path;

pub static LINT: &Lint = &Lint {
name: "non_snake_case_features",
Expand Down Expand Up @@ -60,7 +60,7 @@ foo_bar = []

#[instrument(skip_all)]
pub(crate) fn lint_package(
_ws: &Workspace<'_>,
ws: &Workspace<'_>,
pkg: &Package,
manifest_path: &Path,
level: LintLevelProduct,
Expand All @@ -72,7 +72,7 @@ pub(crate) fn lint_package(
source,
} = level;

let manifest_path = rel_cwd_manifest_path(manifest_path, gctx);
let manifest_path = workspace_rel_path(ws, manifest_path);

lint_package_inner(pkg, &manifest_path, lint_level, source, pkg_stats, gctx)
}
Expand Down
Loading
Loading