Skip to content

Commit 0b17ca8

Browse files
authored
fix(warnings): count reads after exported branch bindings
Remove false unused-variable warnings for branch bindings read afterward, including nested branches and captures. Keep warnings for unread values while respecting local scopes, shadowing and conditional-pattern comparisons. Add warning regressions backed by native and BEAM execution.
1 parent dc14c2c commit 0b17ca8

7 files changed

Lines changed: 545 additions & 118 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ All notable milestones for the nera project are documented in this file.
4343
- `fun_catch_class` checks matching and mismatching classes, nested and named
4444
funs, comprehension helpers and fun-head shadowing natively and on BEAM.
4545

46+
## Branch exports count later reads as uses
47+
48+
- Names bound in `case`, `if` and `receive` branches no longer get false
49+
unused-variable warnings when code reads them after the construct, including
50+
nested branches, clause patterns, receive timeouts and later fun captures.
51+
- The unused-variable pass checks direct matches consistently in branch bodies
52+
and keeps warnings for genuinely unread or fun-local bindings. A later fun
53+
that shadows a name does not count as reading the exported value.
54+
- Later reads inside comprehensions and `maybe`, including conditional-pattern
55+
comparisons, and key/size reads in match patterns reach the bindings they
56+
consume without leaking across local scopes.
57+
- Warning regressions cover the existing `nested_case_exports` fixture and
58+
`branch_export_warnings`, shared by native and BEAM execution.
59+
4660
## Regression coverage for case exports inside maybe bodies
4761

4862
- A diagnostic regression protects names bound in every `case` branch and read

‎crates/nera-ir/src/warnings/ast_walkers.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,9 @@ fn collect_refs(expr: &ast::Expr, refs: &mut HashSet<AtomId>, scoping: RefScopin
459459
match item {
460460
ast::MaybeExpr::Expr(e) => collect_refs(e, refs, scoping),
461461
ast::MaybeExpr::CondMatch(pat, e, _) => {
462-
if scoping == RefScoping::Lexical {
463-
collect_pattern_bindings(pat, refs);
464-
}
462+
// As with an ordinary match, an already-bound value
463+
// name is read for equality, not rebound.
464+
collect_pattern_bindings(pat, refs);
465465
collect_pattern_refs(pat, refs);
466466
collect_refs(e, refs, scoping);
467467
}

‎crates/nera-ir/src/warnings/tests.rs‎

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,37 +1470,76 @@ fn comprehension_filter_bindings_are_available_to_later_qualifiers() {
14701470

14711471
#[test]
14721472
fn comprehension_filter_bindings_reach_scope_sensitive_diagnostics() {
1473+
// A later fun head or generator shadows N, so the earlier filter's N
1474+
// is genuinely unused as well as shadowed (erlc -Wall).
1475+
let cases: &[(&str, &str, &[&str])] = &[
1476+
(
1477+
"case V of N -> <<1>>; _ -> <<0>> end",
1478+
"X <- [1], begin N = X, true end",
1479+
&[],
1480+
),
1481+
(
1482+
"begin case V of true -> N = 1; false -> ok end, <<N>> end",
1483+
"X <- [1], begin N = X, true end",
1484+
&[],
1485+
),
1486+
(
1487+
"begin case V of true -> N = 1; false -> N = 1 end, <<N>> end",
1488+
"X <- [1], begin N = X, true end",
1489+
&[],
1490+
),
1491+
(
1492+
"begin F = fun(N) -> <<N>> end, F(V) end",
1493+
"X <- [1], begin N = X, true end",
1494+
&["unused_variable", "shadowed_variable"],
1495+
),
1496+
(
1497+
"<<N>>",
1498+
"X <- [1], begin N = X, true end, N <- [V]",
1499+
&["unused_variable", "shadowed_variable"],
1500+
),
1501+
(
1502+
"<<_N>>",
1503+
"X <- [V], begin _N = X, true end, begin _N = 1, true end",
1504+
&["match_underscore_var"],
1505+
),
1506+
(
1507+
"<<_N>>",
1508+
"X <- [true], X orelse begin _N = 1, true end, _N <- [V]",
1509+
&[],
1510+
),
1511+
("<<N>>", "_ <- [V], {N, N} <- [{1, 1}]", &[]),
1512+
];
14731513
for (open, close) in [("[", "]"), ("<<", ">>")] {
1474-
for (template, qualifiers, expected) in [
1475-
("case V of N -> <<1>>; _ -> <<0>> end",
1476-
"X <- [1], begin N = X, true end", None),
1477-
("begin case V of true -> N = 1; false -> ok end, <<N>> end",
1478-
"X <- [1], begin N = X, true end", None),
1479-
("begin case V of true -> N = 1; false -> N = 1 end, <<N>> end",
1480-
"X <- [1], begin N = X, true end", None),
1481-
("begin F = fun(N) -> <<N>> end, F(V) end",
1482-
"X <- [1], begin N = X, true end", Some("shadowed_variable")),
1483-
("<<N>>", "X <- [1], begin N = X, true end, N <- [V]",
1484-
Some("shadowed_variable")),
1485-
("<<_N>>", "X <- [V], begin _N = X, true end, begin _N = 1, true end",
1486-
Some("match_underscore_var")),
1487-
("<<_N>>", "X <- [true], X orelse begin _N = 1, true end, _N <- [V]", None),
1488-
("<<N>>", "_ <- [V], {N, N} <- [{1, 1}]", None),
1489-
] {
1490-
let source = format!("-module(test). -export([f/1]). f(V) ->
1491-
{open} ({template}) || {qualifiers} {close}.");
1514+
for (template, qualifiers, expected) in cases {
1515+
let source = format!(
1516+
"-module(test). -export([f/1]). f(V) ->
1517+
{open} ({template}) || {qualifiers} {close}."
1518+
);
14921519
let (module, names) = parse_module(&source);
14931520
let diagnostics = analyze_module(&module, &names, None);
14941521
assert!(
1495-
!diagnostics.iter().any(|d| d.severity == nera_syntax::error::Severity::Error),
1522+
!diagnostics
1523+
.iter()
1524+
.any(|d| d.severity == nera_syntax::error::Severity::Error),
14961525
"{source}\n{diagnostics:?}"
14971526
);
1498-
let relevant: Vec<_> = diagnostics.iter().filter_map(|d| d.code.as_deref())
1499-
.filter(|code| ["unsafe_variable", "exported_var", "unreachable_clause",
1500-
"unused_variable", "shadowed_variable", "match_underscore_var"].contains(code))
1527+
let relevant: Vec<_> = diagnostics
1528+
.iter()
1529+
.filter_map(|d| d.code.as_deref())
1530+
.filter(|code| {
1531+
[
1532+
"unsafe_variable",
1533+
"exported_var",
1534+
"unreachable_clause",
1535+
"unused_variable",
1536+
"shadowed_variable",
1537+
"match_underscore_var",
1538+
]
1539+
.contains(code)
1540+
})
15011541
.collect();
1502-
assert_eq!(relevant, expected.into_iter().collect::<Vec<_>>(),
1503-
"{source}\n{diagnostics:?}");
1542+
assert_eq!(relevant, *expected, "{source}\n{diagnostics:?}");
15041543
}
15051544
}
15061545
}

0 commit comments

Comments
 (0)