Skip to content

Commit

Permalink
给lint的未使用量查找改为忽略同行中的使用
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Mar 29, 2024
1 parent 3737172 commit 06824bd
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 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.15.5"
version = "0.15.6"
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.3"
version = "0.1.4"
edition = "2021"

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

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

use lints::get_useds;
use tag_code::mdt_logic_split_unwraped;
Expand Down Expand Up @@ -66,21 +66,32 @@ impl<'a> Line<'a> {
#[derive(Debug)]
pub struct Source<'a> {
lines: Vec<Line<'a>>,
used_vars: HashSet<&'a str>,
used_vars: HashMap<&'a str, Vec<Var<'a>>>,
}
impl<'a> Source<'a> {
pub fn from_str(s: &'a str) -> Self {
let env_assignables = &[
"@counter",
][..];
let lines = s.lines().enumerate()
.map(|(lineno, line)| Line::from_line(lineno, line))
.collect::<Vec<_>>();

let used_vars = FromIterator::from_iter(lines.iter()
let mut used_vars: HashMap<&str, Vec<Var<'_>>> = HashMap::new();
lines.iter()
.filter_map(get_useds)
.flatten()
.filter_map(|used| used.as_read().map(Var::value))
.chain([
"@counter",
]));
.filter_map(|used| (
used.as_read().map(Var::value)?,
*used.as_read().unwrap()
).into())
.chain(env_assignables.iter()
.map(|&var| (var, Var::new_nonlocation(var))))
.for_each(|(key, var)| {
used_vars.entry(key)
.or_default()
.push(var)
});

Self {
lines,
Expand Down Expand Up @@ -147,6 +158,10 @@ impl<'a> Var<'a> {
Self { lineno, arg_idx, value }
}

pub fn new_nonlocation(value: &'a str) -> Self {
Self { lineno: usize::MAX, arg_idx: 0, value }
}

pub fn value(&self) -> &'a str {
self.value
}
Expand Down
16 changes: 12 additions & 4 deletions tools/logic_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl VarUsedMethod {
}
}

#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub struct VarUsed<'a> {
method: VarUsedMethod,
var: Var<'a>,
Expand Down Expand Up @@ -230,9 +230,17 @@ fn check_assign_var<'a>(
VarType::Var(_) => {
let mut lints = Vec::new();
lints.extend(check_var(src, line, var));
if !src.used_vars.contains(var.value())
&& !regex_is_match!(r"^_(?:$|[^_])", var.value())
{
'x: {
if let Some(useds) = src.used_vars.get(var.value()) {
if useds.iter()
.any(|used| used.lineno() != var.lineno())
{
break 'x;
}
}
if regex_is_match!(r"^_(?:$|[^_])", var.value()) {
break 'x;
}
lints.push(Lint::new(var, WarningLint::NeverUsed));
}
vec_optiter(lints.into())
Expand Down

0 comments on commit 06824bd

Please sign in to comment.