feat(natu): detect single-use let bindings

This commit was merged in pull request #116.
This commit is contained in:
2026-09-30 10:43:05 -04:00
parent 2265dfc558
commit 78492739f3
7 changed files with 153 additions and 16 deletions

View File

@@ -69,7 +69,7 @@ fn natu_reports_out_of_order_let_bindings() -> googletest::Result<()> {
fn natu_preserves_suppressed_bindings() -> googletest::Result<()> {
let report = natu::future_sight(vec![SourceFile {
path: PathBuf::from("testdata/nolint.nix"),
source: "{ config, ... }: let\n cfg = config.example; # NOLINT(let_order)\n value = cfg;\nin value\n".into(),
source: "{ config, ... }: let\n cfg = config.example; # NOLINT(let_order, single_use_binding)\n value = cfg; # NOLINT(single_use_binding)\nin value\n".into(),
}])
.or_fail()?;
@@ -159,6 +159,49 @@ transform value
Ok(())
}
#[gtest]
fn natu_accepts_value_captured_by_reused_lambda() -> googletest::Result<()> {
let report = natu::future_sight(vec![SourceFile {
path: PathBuf::from("testdata/captured_value.nix"),
source: r#"
let
mapper = _: captured;
captured = 1;
in
[ (mapper 1) (mapper 2) ]
"#
.into(),
}])
.or_fail()?;
expect_that!(report.is_clean(), is_true());
Ok(())
}
#[gtest]
fn natu_reports_single_use_binding() -> googletest::Result<()> {
let report = natu::future_sight(vec![SourceFile {
path: PathBuf::from("testdata/single_use_binding.nix"),
source: r#"
let
single = 1;
recursive = recursive;
in
{ inherit single recursive; }
"#
.into(),
}])
.or_fail()?;
let count = report
.diagnostics
.iter()
.filter(|diagnostic| diagnostic.finding.rule_id == "single_use_binding")
.count();
expect_that!(count, eq(1));
Ok(())
}
#[gtest]
fn natu_reports_list_to_attrs_after_map() -> googletest::Result<()> {
let report = natu::future_sight(vec![SourceFile {