Skip to content

Commit 133da05

Browse files
committed
feat: Error check the lints table
1 parent 540a052 commit 133da05

File tree

2 files changed

+160
-3
lines changed

2 files changed

+160
-3
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2891,12 +2891,33 @@ impl TomlManifest {
28912891
}
28922892

28932893
fn verify_lints(lints: Option<&TomlLints>, features: &Features) -> CargoResult<()> {
2894-
if lints.is_none() {
2895-
return Ok(());
2896-
};
2894+
let Some(lints) = lints else { return Ok(()); };
28972895

28982896
features.require(Feature::lints())?;
28992897

2898+
for (tool, lints) in lints {
2899+
let supported = ["rust", "clippy", "rustdoc"];
2900+
if !supported.contains(&tool.as_str()) {
2901+
let supported = supported.join(", ");
2902+
anyhow::bail!("unsupported `{tool}` in `[lints]`, must be one of {supported}")
2903+
}
2904+
for name in lints.keys() {
2905+
if let Some((prefix, suffix)) = name.split_once("::") {
2906+
if tool == prefix {
2907+
anyhow::bail!(
2908+
"`lints.{tool}.{name}` is not valid lint name; try `lints.{prefix}.{suffix}`"
2909+
)
2910+
} else if tool == "rust" && supported.contains(&prefix) {
2911+
anyhow::bail!(
2912+
"`lints.{tool}.{name}` is not valid lint name; try `lints.{prefix}.{suffix}`"
2913+
)
2914+
} else {
2915+
anyhow::bail!("`lints.{tool}.{name}` is not a valid lint name")
2916+
}
2917+
}
2918+
}
2919+
}
2920+
29002921
Ok(())
29012922
}
29022923

tests/testsuite/lints.rs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,139 @@ Caused by:
6767
")
6868
.run();
6969
}
70+
71+
#[cargo_test]
72+
fn fail_on_invalid_tool() {
73+
let foo = project()
74+
.file(
75+
"Cargo.toml",
76+
r#"
77+
cargo-features = ["lints"]
78+
79+
[package]
80+
name = "foo"
81+
version = "0.0.1"
82+
authors = []
83+
84+
[workspace.lints.super-awesome-linter]
85+
unsafe_code = "forbid"
86+
"#,
87+
)
88+
.file("src/lib.rs", "")
89+
.build();
90+
91+
foo.cargo("check")
92+
.masquerade_as_nightly_cargo(&["lints"])
93+
.with_status(101)
94+
.with_stderr(
95+
"\
96+
[..]
97+
98+
Caused by:
99+
unsupported `super-awesome-linter` in `[lints]`, must be one of rust, clippy, rustdoc
100+
",
101+
)
102+
.run();
103+
}
104+
105+
#[cargo_test]
106+
fn fail_on_tool_injection() {
107+
let foo = project()
108+
.file(
109+
"Cargo.toml",
110+
r#"
111+
cargo-features = ["lints"]
112+
113+
[package]
114+
name = "foo"
115+
version = "0.0.1"
116+
authors = []
117+
118+
[workspace.lints.rust]
119+
"clippy::cyclomatic_complexity" = "warn"
120+
"#,
121+
)
122+
.file("src/lib.rs", "")
123+
.build();
124+
125+
foo.cargo("check")
126+
.masquerade_as_nightly_cargo(&["lints"])
127+
.with_status(101)
128+
.with_stderr(
129+
"\
130+
[..]
131+
132+
Caused by:
133+
`lints.rust.clippy::cyclomatic_complexity` is not valid lint name; try `lints.clippy.cyclomatic_complexity`
134+
",
135+
)
136+
.run();
137+
}
138+
139+
#[cargo_test]
140+
fn fail_on_redundant_tool() {
141+
let foo = project()
142+
.file(
143+
"Cargo.toml",
144+
r#"
145+
cargo-features = ["lints"]
146+
147+
[package]
148+
name = "foo"
149+
version = "0.0.1"
150+
authors = []
151+
152+
[workspace.lints.rust]
153+
"rust::unsafe_code" = "forbid"
154+
"#,
155+
)
156+
.file("src/lib.rs", "")
157+
.build();
158+
159+
foo.cargo("check")
160+
.masquerade_as_nightly_cargo(&["lints"])
161+
.with_status(101)
162+
.with_stderr(
163+
"\
164+
[..]
165+
166+
Caused by:
167+
`lints.rust.rust::unsafe_code` is not valid lint name; try `lints.rust.unsafe_code`
168+
",
169+
)
170+
.run();
171+
}
172+
173+
#[cargo_test]
174+
fn fail_on_conflicting_tool() {
175+
let foo = project()
176+
.file(
177+
"Cargo.toml",
178+
r#"
179+
cargo-features = ["lints"]
180+
181+
[package]
182+
name = "foo"
183+
version = "0.0.1"
184+
authors = []
185+
186+
[workspace.lints.rust]
187+
"super-awesome-tool::unsafe_code" = "forbid"
188+
"#,
189+
)
190+
.file("src/lib.rs", "")
191+
.build();
192+
193+
foo.cargo("check")
194+
.masquerade_as_nightly_cargo(&["lints"])
195+
.with_status(101)
196+
.with_stderr(
197+
"\
198+
[..]
199+
200+
Caused by:
201+
`lints.rust.super-awesome-tool::unsafe_code` is not a valid lint name
202+
",
203+
)
204+
.run();
205+
}

0 commit comments

Comments
 (0)