Skip to content

Commit 4f403f3

Browse files
committed
clippy_dev: Validate lint name format during argument parsing.
1 parent 813eb08 commit 4f403f3

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

clippy_dev/src/deprecate_lint.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ use std::{fs, io};
1515
///
1616
/// If a file path could not read from or written to
1717
pub fn deprecate(clippy_version: Version, name: &str, reason: &str) {
18-
if let Some((prefix, _)) = name.split_once("::") {
19-
panic!("`{name}` should not contain the `{prefix}` prefix");
20-
}
21-
2218
let mut lints = find_lint_decls();
2319
let (mut deprecated_lints, renamed_lints) = read_deprecated_lints();
2420

clippy_dev/src/main.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use clippy_dev::{
77
ClippyInfo, UpdateMode, deprecate_lint, dogfood, fmt, lint, new_lint, release, rename_lint, serve, setup, sync,
88
update_lints,
99
};
10-
use std::convert::Infallible;
1110
use std::env;
1211

1312
fn main() {
@@ -95,6 +94,20 @@ fn main() {
9594
}
9695
}
9796

97+
fn lint_name(name: &str) -> Result<String, String> {
98+
let name = name.replace('-', "_");
99+
if let Some((pre, _)) = name.split_once("::") {
100+
Err(format!("lint name should not contain the `{pre}` prefix"))
101+
} else if name
102+
.bytes()
103+
.any(|x| !matches!(x, b'_' | b'0'..=b'9' | b'a'..=b'z' | b'A'..=b'Z'))
104+
{
105+
Err("lint name contains invalid characters".to_owned())
106+
} else {
107+
Ok(name)
108+
}
109+
}
110+
98111
#[derive(Parser)]
99112
#[command(name = "dev", about)]
100113
struct Dev {
@@ -150,7 +163,7 @@ enum DevCommand {
150163
#[arg(
151164
short,
152165
long,
153-
value_parser = |name: &str| Ok::<_, Infallible>(name.replace('-', "_")),
166+
value_parser = lint_name,
154167
)]
155168
/// Name of the new lint in snake case, ex: `fn_too_long`
156169
name: String,
@@ -223,8 +236,12 @@ enum DevCommand {
223236
/// Rename a lint
224237
RenameLint {
225238
/// The name of the lint to rename
239+
#[arg(value_parser = lint_name)]
226240
old_name: String,
227-
#[arg(required_unless_present = "uplift")]
241+
#[arg(
242+
required_unless_present = "uplift",
243+
value_parser = lint_name,
244+
)]
228245
/// The new name of the lint
229246
new_name: Option<String>,
230247
#[arg(long)]
@@ -234,6 +251,7 @@ enum DevCommand {
234251
/// Deprecate the given lint
235252
Deprecate {
236253
/// The name of the lint to deprecate
254+
#[arg(value_parser = lint_name)]
237255
name: String,
238256
#[arg(long, short)]
239257
/// The reason for deprecation

clippy_dev/src/rename_lint.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ use std::path::Path;
2626
/// * If `old_name` names a deprecated or renamed lint.
2727
#[expect(clippy::too_many_lines)]
2828
pub fn rename(clippy_version: Version, old_name: &str, new_name: &str, uplift: bool) {
29-
if let Some((prefix, _)) = old_name.split_once("::") {
30-
panic!("`{old_name}` should not contain the `{prefix}` prefix");
31-
}
32-
if let Some((prefix, _)) = new_name.split_once("::") {
33-
panic!("`{new_name}` should not contain the `{prefix}` prefix");
34-
}
35-
3629
let mut updater = FileUpdater::default();
3730
let mut lints = find_lint_decls();
3831
let (deprecated_lints, mut renamed_lints) = read_deprecated_lints();

0 commit comments

Comments
 (0)