From 8a69962213fb173a0636eb114fa3cf9356b9239f Mon Sep 17 00:00:00 2001 From: Andrew Gazelka Date: Wed, 27 May 2026 19:51:49 -0700 Subject: [PATCH] unit-graph: include resolved lint flags --- src/cargo/core/compiler/mod.rs | 2 +- src/cargo/core/compiler/unit_graph.rs | 11 ++- tests/testsuite/unit_graph.rs | 97 +++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 2 deletions(-) diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index ba18461078d..ab8582a6514 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -1632,7 +1632,7 @@ fn build_dir_remap(build_runner: &BuildRunner<'_, '_>) -> OsString { } /// Generates the `--check-cfg` arguments for the `unit`. -fn check_cfg_args(unit: &Unit) -> Vec { +pub(crate) fn check_cfg_args(unit: &Unit) -> Vec { // The routine below generates the --check-cfg arguments. Our goals here are to // enable the checking of conditionals and pass the list of declared features. // diff --git a/src/cargo/core/compiler/unit_graph.rs b/src/cargo/core/compiler/unit_graph.rs index 944b062cddf..56287f16e45 100644 --- a/src/cargo/core/compiler/unit_graph.rs +++ b/src/cargo/core/compiler/unit_graph.rs @@ -6,7 +6,7 @@ use cargo_util_schemas::core::PackageIdSpec; use crate::GlobalContext; use crate::core::Target; -use crate::core::compiler::Unit; +use crate::core::compiler::{self, Unit}; use crate::core::compiler::{CompileKind, CompileMode}; use crate::core::dependency::Dependency; use crate::core::profiles::{Profile, UnitFor}; @@ -67,6 +67,10 @@ struct SerializedUnit<'a> { platform: CompileKind, mode: CompileMode, features: &'a Vec, + /// Resolved rustc flags from the package's `[lints]` table. + #[serde(skip_serializing_if = "<[_]>::is_empty")] + lint_rustflags: &'a [String], + check_cfg_args: Vec, #[serde(skip_serializing_if = "std::ops::Not::not")] // hide for unstable build-std is_std: bool, dependencies: Vec, @@ -137,6 +141,11 @@ pub fn emit_serialized_unit_graph( platform: unit.kind, mode: unit.mode, features: &unit.features, + lint_rustflags: unit.pkg.manifest().lint_rustflags(), + check_cfg_args: compiler::check_cfg_args(unit) + .into_iter() + .map(|arg| arg.to_string_lossy().into_owned()) + .collect(), is_std: unit.is_std, dependencies, } diff --git a/tests/testsuite/unit_graph.rs b/tests/testsuite/unit_graph.rs index cead13bc6e8..286548893de 100644 --- a/tests/testsuite/unit_graph.rs +++ b/tests/testsuite/unit_graph.rs @@ -4,6 +4,7 @@ use crate::prelude::*; use cargo_test_support::project; use cargo_test_support::registry::Package; use cargo_test_support::str; +use serde_json::Value; #[cargo_test] fn gated() { @@ -56,6 +57,12 @@ fn simple() { ], "units": [ { + "check_cfg_args": [ + "--check-cfg", + "cfg(docsrs,test)", + "--check-cfg", + "cfg(feature, values(\"feata\"))" + ], "dependencies": [ { "extern_crate_name": "b", @@ -102,6 +109,12 @@ fn simple() { } }, { + "check_cfg_args": [ + "--check-cfg", + "cfg(docsrs,test)", + "--check-cfg", + "cfg(feature, values(\"featb\"))" + ], "dependencies": [ { "extern_crate_name": "c", @@ -148,6 +161,12 @@ fn simple() { } }, { + "check_cfg_args": [ + "--check-cfg", + "cfg(docsrs,test)", + "--check-cfg", + "cfg(feature, values(\"featc\"))" + ], "dependencies": [], "features": [ "featc" @@ -186,6 +205,12 @@ fn simple() { } }, { + "check_cfg_args": [ + "--check-cfg", + "cfg(docsrs,test)", + "--check-cfg", + "cfg(feature, values())" + ], "dependencies": [ { "extern_crate_name": "a", @@ -237,3 +262,75 @@ fn simple() { ) .run(); } + +#[cargo_test] +fn includes_resolved_lint_rustflags() { + let p = project() + .file( + "Cargo.toml", + r#" + [workspace] + members = ["app"] + + [workspace.lints.rust] + unsafe_code = "forbid" + unexpected_cfgs = { level = "warn", check-cfg = ["cfg(ix_test)"] } + + [workspace.lints.clippy] + all = { level = "deny", priority = -1 } + pedantic = "warn" + "#, + ) + .file( + "app/Cargo.toml", + r#" + [package] + name = "app" + version = "0.1.0" + edition = "2024" + + [features] + alpha = [] + beta = [] + + [lints] + workspace = true + "#, + ) + .file("app/src/lib.rs", "") + .build(); + + let output = p + .cargo("build --unit-graph -Zunstable-options") + .masquerade_as_nightly_cargo(&["unit-graph"]) + .exec_with_output() + .unwrap(); + let graph: Value = serde_json::from_slice(&output.stdout).unwrap(); + let app = graph["units"] + .as_array() + .unwrap() + .iter() + .find(|unit| unit["target"]["name"] == "app") + .unwrap(); + + assert_eq!( + app["lint_rustflags"], + serde_json::json!([ + "--deny=clippy::all", + "--forbid=unsafe_code", + "--warn=unexpected_cfgs", + "--warn=clippy::pedantic", + "--check-cfg", + "cfg(ix_test)" + ]) + ); + assert_eq!( + app["check_cfg_args"], + serde_json::json!([ + "--check-cfg", + "cfg(docsrs,test)", + "--check-cfg", + "cfg(feature, values(\"alpha\", \"beta\"))" + ]) + ); +}