Skip to content

Commit fc90578

Browse files
committed
Auto merge of #12174 - hi-rustin:rustin-patch-lints, r=epage
Automatically inherit workspace lints when running cargo new/init
2 parents 092db78 + 6573701 commit fc90578

File tree

11 files changed

+90
-0
lines changed

11 files changed

+90
-0
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,18 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> {
820820
workspace_package_keys,
821821
)
822822
}
823+
824+
// Try to inherit the workspace lints key if it exists.
825+
if config.cli_unstable().lints
826+
&& workspace_document
827+
.get("workspace")
828+
.and_then(|workspace| workspace.get("lints"))
829+
.is_some()
830+
{
831+
let mut table = toml_edit::Table::new();
832+
table["workspace"] = toml_edit::value(true);
833+
manifest["lints"] = toml_edit::Item::Table(table);
834+
}
823835
}
824836
}
825837

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[workspace]
2+
members = ["crates/*"]
3+
4+
[workspace.lints.rust]
5+
unsafe_code = "forbid"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pub fn add(left: usize, right: usize) -> usize {
2+
left + right
3+
}
4+
5+
#[cfg(test)]
6+
mod tests {
7+
use super::*;
8+
9+
#[test]
10+
fn it_works() {
11+
let result = add(2, 2);
12+
assert_eq!(result, 4);
13+
}
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use cargo_test_support::compare::assert_ui;
2+
use cargo_test_support::curr_dir;
3+
use cargo_test_support::CargoCommand;
4+
use cargo_test_support::ChannelChanger;
5+
use cargo_test_support::Project;
6+
7+
#[cargo_test]
8+
fn case() {
9+
let project = Project::from_template(curr_dir!().join("in"));
10+
let project_root = project.root();
11+
let cwd = &project_root;
12+
13+
snapbox::cmd::Command::cargo_ui()
14+
.arg("new")
15+
.args(["crates/foo", "-Zlints"])
16+
.current_dir(cwd)
17+
.masquerade_as_nightly_cargo(&["lints"])
18+
.assert()
19+
.success()
20+
.stdout_matches_path(curr_dir!().join("stdout.log"))
21+
.stderr_matches_path(curr_dir!().join("stderr.log"));
22+
23+
assert_ui().subset_matches(curr_dir!().join("out"), &project_root);
24+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[workspace]
2+
members = ["crates/*"]
3+
4+
[workspace.lints.rust]
5+
unsafe_code = "forbid"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "foo"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
10+
[lints]
11+
workspace = true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pub fn add(left: usize, right: usize) -> usize {
2+
left + right
3+
}
4+
5+
#[cfg(test)]
6+
mod tests {
7+
use super::*;
8+
9+
#[test]
10+
fn it_works() {
11+
let result = add(2, 2);
12+
assert_eq!(result, 4);
13+
}
14+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Created binary (application) `crates/foo` package

tests/testsuite/cargo_new/inherit_workspace_lints/stdout.log

Whitespace-only changes.

tests/testsuite/cargo_new/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod inherit_workspace_lints;
12
mod inherit_workspace_package_table;
23
mod inherit_workspace_package_table_with_edition;
34
mod inherit_workspace_package_table_with_registry;

0 commit comments

Comments
 (0)