diff --git a/cargo-dylint/fixtures/depinfo_dylint_libs/Cargo.toml b/cargo-dylint/fixtures/depinfo_dylint_libs/Cargo.toml new file mode 100644 index 000000000..114339ad3 --- /dev/null +++ b/cargo-dylint/fixtures/depinfo_dylint_libs/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "depinfo_dylint_libs" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +anyhow = "1.0" + +[workspace] + +[workspace.metadata.dylint] +libraries = [ + { path = "../../../examples/restriction/question_mark_in_expression" }, + { path = "../../../examples/restriction/try_io_result" }, +] diff --git a/cargo-dylint/fixtures/depinfo_dylint_libs/src/main.rs b/cargo-dylint/fixtures/depinfo_dylint_libs/src/main.rs new file mode 100644 index 000000000..ec1db2072 --- /dev/null +++ b/cargo-dylint/fixtures/depinfo_dylint_libs/src/main.rs @@ -0,0 +1,10 @@ +use anyhow::Result; +use std::{ + io::{stdout, Write}, + str::from_utf8, +}; + +fn main() -> Result<()> { + write!(stdout(), "{}", from_utf8(b"Hello, world!")?)?; + Ok(()) +} diff --git a/cargo-dylint/tests/depinfo_dylint_libs.rs b/cargo-dylint/tests/depinfo_dylint_libs.rs new file mode 100644 index 000000000..b5168f2d6 --- /dev/null +++ b/cargo-dylint/tests/depinfo_dylint_libs.rs @@ -0,0 +1,31 @@ +use assert_cmd::prelude::*; +use dylint_internal::env; +use predicates::prelude::*; +use std::{env::set_var, process::Command}; + +#[ctor::ctor] +fn initialize() { + set_var(env::CARGO_TERM_COLOR, "never"); +} + +#[test] +fn depinfo_dylint_libs() { + Command::cargo_bin("cargo-dylint") + .unwrap() + .current_dir("fixtures/depinfo_dylint_libs") + .args(["dylint", "--lib", "question_mark_in_expression"]) + .assert() + .stderr(predicate::str::contains( + "\nwarning: using the `?` operator within an expression\n", + )); + + Command::cargo_bin("cargo-dylint") + .unwrap() + .current_dir("fixtures/depinfo_dylint_libs") + .args(["dylint", "--lib", "try_io_result"]) + .assert() + .stderr(predicate::str::contains( + "\nwarning: returning a `std::io::Result` could discard relevant context (e.g., files \ + or paths involved)\n", + )); +} diff --git a/driver/src/lib.rs b/driver/src/lib.rs index 79e3d78f5..e266f4870 100644 --- a/driver/src/lib.rs +++ b/driver/src/lib.rs @@ -195,12 +195,17 @@ impl rustc_driver::Callbacks for Callbacks { previous(sess, lint_store); } + let dylint_libs = env::var(env::DYLINT_LIBS).ok(); let dylint_metadata = env::var(env::DYLINT_METADATA).ok(); let dylint_no_deps = env::var(env::DYLINT_NO_DEPS).ok(); let dylint_no_deps_enabled = dylint_no_deps.as_ref().map_or(false, |value| value != "0"); let cargo_primary_package_is_set = env::var(env::CARGO_PRIMARY_PACKAGE).is_ok(); + sess.parse_sess.env_depinfo.lock().insert(( + rustc_span::Symbol::intern(env::DYLINT_LIBS), + dylint_libs.as_deref().map(rustc_span::Symbol::intern), + )); sess.parse_sess.env_depinfo.lock().insert(( rustc_span::Symbol::intern(env::DYLINT_METADATA), dylint_metadata.as_deref().map(rustc_span::Symbol::intern),