refactor(natu): remove single-use binding rule

This commit was merged in pull request #123.
This commit is contained in:
2026-10-04 14:08:13 -04:00
parent 11b490d1e8
commit c91094234f
10 changed files with 16 additions and 222 deletions

View File

@@ -1,46 +0,0 @@
use crate::{
ast,
scope_graph::{Binding, LetScope, ScopeGraph},
};
pub struct Finding {
pub range: ast::SourceRange,
pub name: String,
}
pub fn check(graph: &ScopeGraph) -> Vec<Finding> {
graph
.let_scopes()
.iter()
.flat_map(|let_scope| {
let_scope.bindings.iter().filter_map(|binding| {
is_single_use(graph, let_scope, binding).then(|| Finding {
range: binding.range,
name: binding.name.clone(),
})
})
})
.collect()
}
fn is_single_use(graph: &ScopeGraph, let_scope: &LetScope, binding: &Binding) -> bool {
let self_references = binding
.references
.iter()
.filter(|reference| graph.resolve(**reference).contains(&binding.declaration))
.count();
let external_references = let_scope
.bindings
.iter()
.filter(|candidate| candidate.declaration != binding.declaration)
.flat_map(|candidate| candidate.references.iter())
.chain(let_scope.entry_references.iter())
.filter(|reference| graph.resolve(**reference).contains(&binding.declaration))
.collect::<Vec<_>>();
!binding.is_import
&& self_references == 0
&& external_references.len() == 1
&& (binding.is_lambda
|| !graph.crosses_lambda_scope(*external_references[0], binding.declaration))
}