Skip to content

Commit

Permalink
给lint也检查未知的命令参数
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Jan 17, 2025
1 parent f54f8d4 commit c493710
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mindustry_logic_bang_lang"
version = "0.17.13"
version = "0.17.14"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
2 changes: 1 addition & 1 deletion tools/logic_lint/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "logic_lint"
version = "0.1.9"
version = "0.1.10"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
42 changes: 41 additions & 1 deletion tools/logic_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl<'a> Source<'a> {
}
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Var<'a> {
lineno: usize,
arg_idx: usize,
Expand Down Expand Up @@ -192,3 +192,43 @@ impl<'a> Var<'a> {
self.lineno
}
}


#[cfg(test)]
mod tests {
use lints::WarningLint;

use super::*;

#[test]
fn do_works() {
let s = "set x _1";
let src = Source::from_str(s);
assert_eq!(src.lint(), vec![
Lint::new(
&Var::new(0, 1, "x"),
WarningLint::NeverUsed,
),
Lint::new(
&Var::new(0, 2, "_1"),
WarningLint::UsedRawArgs,
),
]);
}

#[test]
fn unknown_cmds() {
let s = "foo _0 A";
let src = Source::from_str(s);
assert_eq!(src.lint(), vec![
Lint::new(
&Var::new(0, 1, "_0"),
WarningLint::UsedRawArgs,
),
Lint::new(
&Var::new(0, 2, "A"),
WarningLint::SuspectedConstant,
),
]);
}
}
30 changes: 14 additions & 16 deletions tools/logic_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ macro_rules! make_lints {
},
)*
[] => unreachable!(),
[cmd, ..] => {
if let Some(lint) = check_cmd($src, $line, cmd) {
$lints.push(lint)
}
[cmd, args @ ..] => {
$lints.extend(check_cmd($src, $line, cmd, args))
},
}
$lints
Expand Down Expand Up @@ -278,15 +276,15 @@ fn check_vars<'a>(
.filter_map(|var| check_var(src, line, var))
}
fn check_cmd<'a>(
_src: &'a crate::Source<'a>,
_line: &'a crate::Line<'a>,
src: &'a crate::Source<'a>,
line: &'a crate::Line<'a>,
var: &'a Var<'a>,
) -> Option<Lint<'a>> {
if regex_is_match!(r"^__", var) && !regex_is_match!(r".__$", var) {
return Some(Lint::new(var, WarningLint::SuspectedVarCmd))
}

None
args: &'a [Var<'a>],
) -> impl Iterator<Item = Lint<'a>> + 'a {
(regex_is_match!(r"^__", var) && !regex_is_match!(r".__$", var))
.then(|| Lint::new(var, WarningLint::SuspectedVarCmd))
.into_iter()
.chain(check_vars(src, line, args))
}
#[must_use]
fn check_argc<'a>(
Expand Down Expand Up @@ -490,7 +488,7 @@ pub trait ShowLint {
) -> fmt::Result;
}

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub struct Lint<'a> {
arg: &'a Var<'a>,
msg: LintType,
Expand Down Expand Up @@ -536,7 +534,7 @@ impl ShowLint for Lint<'_> {
}
}

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub enum LintType {
Warning(WarningLint),
Error(ErrorLint),
Expand Down Expand Up @@ -572,7 +570,7 @@ impl ShowLint for LintType {
}
}

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub enum WarningLint {
/// 显式使用双下划线名称
UsedDoubleUnderline,
Expand Down Expand Up @@ -621,7 +619,7 @@ impl ShowLint for WarningLint {
}
}

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub enum ErrorLint {
InvalidOper {
expected: &'static [&'static str],
Expand Down

0 comments on commit c493710

Please sign in to comment.