clippy_dev: Validate lint name format during argument parsing.

This commit is contained in:
Jason Newcomb 2025-10-10 17:04:01 -04:00
parent 813eb082f3
commit 4f403f39b5
3 changed files with 21 additions and 14 deletions

View file

@ -15,10 +15,6 @@ use std::{fs, io};
///
/// If a file path could not read from or written to
pub fn deprecate(clippy_version: Version, name: &str, reason: &str) {
if let Some((prefix, _)) = name.split_once("::") {
panic!("`{name}` should not contain the `{prefix}` prefix");
}
let mut lints = find_lint_decls();
let (mut deprecated_lints, renamed_lints) = read_deprecated_lints();

View file

@ -7,7 +7,6 @@ use clippy_dev::{
ClippyInfo, UpdateMode, deprecate_lint, dogfood, fmt, lint, new_lint, release, rename_lint, serve, setup, sync,
update_lints,
};
use std::convert::Infallible;
use std::env;
fn main() {
@ -95,6 +94,20 @@ fn main() {
}
}
fn lint_name(name: &str) -> Result<String, String> {
let name = name.replace('-', "_");
if let Some((pre, _)) = name.split_once("::") {
Err(format!("lint name should not contain the `{pre}` prefix"))
} else if name
.bytes()
.any(|x| !matches!(x, b'_' | b'0'..=b'9' | b'a'..=b'z' | b'A'..=b'Z'))
{
Err("lint name contains invalid characters".to_owned())
} else {
Ok(name)
}
}
#[derive(Parser)]
#[command(name = "dev", about)]
struct Dev {
@ -150,7 +163,7 @@ enum DevCommand {
#[arg(
short,
long,
value_parser = |name: &str| Ok::<_, Infallible>(name.replace('-', "_")),
value_parser = lint_name,
)]
/// Name of the new lint in snake case, ex: `fn_too_long`
name: String,
@ -223,8 +236,12 @@ enum DevCommand {
/// Rename a lint
RenameLint {
/// The name of the lint to rename
#[arg(value_parser = lint_name)]
old_name: String,
#[arg(required_unless_present = "uplift")]
#[arg(
required_unless_present = "uplift",
value_parser = lint_name,
)]
/// The new name of the lint
new_name: Option<String>,
#[arg(long)]
@ -234,6 +251,7 @@ enum DevCommand {
/// Deprecate the given lint
Deprecate {
/// The name of the lint to deprecate
#[arg(value_parser = lint_name)]
name: String,
#[arg(long, short)]
/// The reason for deprecation

View file

@ -26,13 +26,6 @@ use std::path::Path;
/// * If `old_name` names a deprecated or renamed lint.
#[expect(clippy::too_many_lines)]
pub fn rename(clippy_version: Version, old_name: &str, new_name: &str, uplift: bool) {
if let Some((prefix, _)) = old_name.split_once("::") {
panic!("`{old_name}` should not contain the `{prefix}` prefix");
}
if let Some((prefix, _)) = new_name.split_once("::") {
panic!("`{new_name}` should not contain the `{prefix}` prefix");
}
let mut updater = FileUpdater::default();
let mut lints = find_lint_decls();
let (deprecated_lints, mut renamed_lints) = read_deprecated_lints();