From 56a6e35957eadc425f9b474848db7619f70d957f Mon Sep 17 00:00:00 2001 From: A4-Tacks Date: Tue, 22 Oct 2024 10:31:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=B9=E7=96=91=E4=BC=BC?= =?UTF-8?q?=E5=B0=86=E5=8C=BF=E5=90=8D=E5=8F=98=E9=87=8F=E4=BD=9C=E4=B8=BA?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E6=89=A7=E8=A1=8C=E7=9A=84lint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- tools/logic_lint/Cargo.toml | 2 +- tools/logic_lint/src/lints.rs | 22 +++++++++++++++++++++- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3e3e434..51b65a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -292,7 +292,7 @@ checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" [[package]] name = "logic_lint" -version = "0.1.7" +version = "0.1.8" dependencies = [ "lazy-regex", "tag_code", @@ -307,7 +307,7 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "mindustry_logic_bang_lang" -version = "0.17.5" +version = "0.17.6" dependencies = [ "display_source", "logic_lint", diff --git a/Cargo.toml b/Cargo.toml index 571e9e6..fb72224 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mindustry_logic_bang_lang" -version = "0.17.5" +version = "0.17.6" edition = "2021" authors = ["A4-Tacks "] diff --git a/tools/logic_lint/Cargo.toml b/tools/logic_lint/Cargo.toml index 4fc8611..d3e5a4b 100644 --- a/tools/logic_lint/Cargo.toml +++ b/tools/logic_lint/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "logic_lint" -version = "0.1.7" +version = "0.1.8" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/tools/logic_lint/src/lints.rs b/tools/logic_lint/src/lints.rs index 508d75d..73e6f9d 100644 --- a/tools/logic_lint/src/lints.rs +++ b/tools/logic_lint/src/lints.rs @@ -39,7 +39,11 @@ macro_rules! make_lints { }, )* [] => unreachable!(), - [_, ..] => (), + [cmd, ..] => { + if let Some(lint) = check_cmd($src, $line, cmd) { + $lints.push(lint) + } + }, } $lints } @@ -270,6 +274,17 @@ fn check_vars<'a>( vars.into_iter() .filter_map(|var| check_var(src, line, var)) } +fn check_cmd<'a>( + _src: &'a crate::Source<'a>, + _line: &'a crate::Line<'a>, + var: &'a Var<'a>, +) -> Option> { + if regex_is_match!(r"^__", var) && !regex_is_match!(r".__$", var) { + return Some(Lint::new(var, WarningLint::SuspectedVarCmd)) + } + + None +} #[must_use] fn check_argc<'a>( _src: &'a crate::Source<'a>, @@ -546,6 +561,8 @@ pub enum WarningLint { AssignLiteral, /// 从命名来看疑似是未被替换的常量 SuspectedConstant, + /// 从命名来看疑似将变量作为命令执行 + SuspectedVarCmd, /// 未被使用 NeverUsed, NoTargetJump, @@ -568,6 +585,9 @@ impl ShowLint for WarningLint { WarningLint::SuspectedConstant => { write!(f, "命名疑似未被替换的常量")? }, + WarningLint::SuspectedVarCmd => { + write!(f, "命令疑似将变量作为命令执行")? + }, WarningLint::NeverUsed => write!(f, "未被使用到的量")?, WarningLint::NoTargetJump => write!(f, "没有目标的跳转")?, }