Skip to content

Commit

Permalink
Auto merge of #12174 - hi-rustin:rustin-patch-lints, r=epage
Browse files Browse the repository at this point in the history
Automatically inherit workspace lints when running cargo new/init
  • Loading branch information
bors committed May 24, 2023
2 parents 092db78 + 6573701 commit fc90578
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,18 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> {
workspace_package_keys,
)
}

// Try to inherit the workspace lints key if it exists.
if config.cli_unstable().lints
&& workspace_document
.get("workspace")
.and_then(|workspace| workspace.get("lints"))
.is_some()
{
let mut table = toml_edit::Table::new();
table["workspace"] = toml_edit::value(true);
manifest["lints"] = toml_edit::Item::Table(table);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
members = ["crates/*"]

[workspace.lints.rust]
unsafe_code = "forbid"
14 changes: 14 additions & 0 deletions tests/testsuite/cargo_new/inherit_workspace_lints/in/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
24 changes: 24 additions & 0 deletions tests/testsuite/cargo_new/inherit_workspace_lints/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::curr_dir;
use cargo_test_support::CargoCommand;
use cargo_test_support::ChannelChanger;
use cargo_test_support::Project;

#[cargo_test]
fn case() {
let project = Project::from_template(curr_dir!().join("in"));
let project_root = project.root();
let cwd = &project_root;

snapbox::cmd::Command::cargo_ui()
.arg("new")
.args(["crates/foo", "-Zlints"])
.current_dir(cwd)
.masquerade_as_nightly_cargo(&["lints"])
.assert()
.success()
.stdout_matches_path(curr_dir!().join("stdout.log"))
.stderr_matches_path(curr_dir!().join("stderr.log"));

assert_ui().subset_matches(curr_dir!().join("out"), &project_root);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
members = ["crates/*"]

[workspace.lints.rust]
unsafe_code = "forbid"
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "foo"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[lints]
workspace = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
14 changes: 14 additions & 0 deletions tests/testsuite/cargo_new/inherit_workspace_lints/out/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Created binary (application) `crates/foo` package
Empty file.
1 change: 1 addition & 0 deletions tests/testsuite/cargo_new/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod inherit_workspace_lints;
mod inherit_workspace_package_table;
mod inherit_workspace_package_table_with_edition;
mod inherit_workspace_package_table_with_registry;
Expand Down

0 comments on commit fc90578

Please sign in to comment.