Skip to content

Commit

Permalink
添加了全局未使用量的lint, 添加了skip接块的条件省略
Browse files Browse the repository at this point in the history
- 添加了未连接的jump的lint
  • Loading branch information
A4-Tacks committed Feb 27, 2024
1 parent a9f5d86 commit a5ca9fd
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 31 deletions.
8 changes: 4 additions & 4 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.14.19"
version = "0.14.20"
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.0"
version = "0.1.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
34 changes: 24 additions & 10 deletions tools/logic_lint/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
pub mod lints;

use core::fmt;
use std::{borrow::Cow, ops::Deref};
use std::{borrow::Cow, collections::HashSet, ops::Deref};

use crate::lints::{ShowLint, Lint};
use lints::get_useds;
use tag_code::mdt_logic_split_unwraped;

use crate::lints::{Lint, ShowLint};

const LIGHT_ARGS_BEGIN: &str = "\x1b[7m";
const LIGHT_ARGS_END: &str = "\x1b[27m";

Expand All @@ -29,7 +31,7 @@ impl<'a> Line<'a> {
}

pub fn hint_args(&self, hints: &[usize]) -> Vec<Cow<'_, str>> {
self.args().into_iter()
self.args().iter()
.enumerate()
.map(|(i, arg)| if hints.contains(&i) {
format!(
Expand All @@ -52,23 +54,37 @@ impl<'a> Line<'a> {
self.args.first().unwrap().lineno
}

pub fn args(&self) -> &[Var<'_>] {
pub fn args(&self) -> &[Var<'a>] {
self.args.as_ref()
}

pub fn len(&self) -> usize {
self.args().len()
}
}

#[derive(Debug)]
pub struct Source<'a> {
lines: Vec<Line<'a>>,
used_vars: HashSet<&'a str>,
}
impl<'a> Source<'a> {
pub fn from_str(s: &'a str) -> Self {
let lines = s.lines().enumerate()
.map(|(lineno, line)| Line::from_line(lineno, line))
.collect();
.collect::<Vec<_>>();

let used_vars = FromIterator::from_iter(lines.iter()
.filter_map(get_useds)
.flatten()
.filter_map(|used| used.as_read().map(Var::value))
.chain([
"@counter",
]));

Self {
lines,
used_vars,
}
}

Expand All @@ -90,8 +106,7 @@ impl<'a> Source<'a> {

pub fn lint(&self) -> Vec<lints::Lint> {
self.lines.iter()
.map(|line| line.lint(self))
.flatten()
.flat_map(|line| line.lint(self))
.collect()
}

Expand All @@ -113,13 +128,12 @@ impl<'a> Source<'a> {
}
}

#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub struct Var<'a> {
lineno: usize,
arg_idx: usize,
value: &'a str,
}

impl<'a> Deref for Var<'a> {
type Target = str;

Expand All @@ -133,7 +147,7 @@ impl<'a> Var<'a> {
Self { lineno, arg_idx, value }
}

pub fn value(&self) -> &str {
pub fn value(&self) -> &'a str {
self.value
}

Expand Down
Loading

0 comments on commit a5ca9fd

Please sign in to comment.