Skip to content

Commit b235680

Browse files
cknittclaude
andcommitted
Fix unused-pattern warnings for variant spreads without a location
A variant spread ...t is expanded into an or-pattern of t's constructors. The constructors were marked so that the ones earlier cases already match aren't reported, but the or-patterns grouping them weren't, and had no location: when earlier cases matched several constructors, warning 12 was reported without a location. Mark them too. The root of the expansion now gets the spread's location instead, so a spread that is unused as a whole is reported at the spread, also for a single-constructor variant, which wasn't reported at all. Signed-off-by: Christoph Knittel <christoph@knittel.cc> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 30d64b5 commit b235680

4 files changed

Lines changed: 64 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#### :bug: Bug fix
2525

26+
- Fix unused-pattern warnings for variant spread patterns (`...t`) reported without a location: constructors of a spread that earlier cases already match are no longer reported, and a spread that is unused as a whole is reported at the spread, also when its variant has a single constructor. https://github.com/rescript-lang/rescript/pull/8744
2627
- Keep `@warning("-4")` on `if` expressions when formatting; the formatter dropped it. https://github.com/rescript-lang/rescript/pull/8741
2728
- Fix the formatter dropping a `@res.ternary` attribute written in source, e.g. `let b = @res.ternary x`, and print `@JSX` and `@res.ternary` with the same parens as other attributes, e.g. `(@JSX x) + 1`. https://github.com/rescript-lang/rescript/pull/8740
2829
- Respect `@warning` attributes on the module in `module type of`, e.g. `module type of @warning("-3") DeprecatedModule`, which still reported the warning. https://github.com/rescript-lang/rescript/pull/8738

‎compiler/ml/typecore.ml‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,8 +683,20 @@ let build_ppat_or_for_variant_spread pat env expected_ty =
683683
let pat =
684684
match synthetic_or_patterns with
685685
| [] -> pat
686-
| pat :: pats ->
687-
List.fold_left (fun p1 p2 -> Ast_helper.Pat.or_ p1 p2) pat pats
686+
| p :: ps ->
687+
(* The constructors and the or-patterns grouping them are marked, so
688+
that the ones earlier cases already match aren't reported as
689+
unused. The root stands for the spread the user wrote: it's
690+
reported, at the spread, when all of it is unused. *)
691+
let root =
692+
List.fold_left
693+
(fun p1 p2 ->
694+
Ast_helper.Pat.or_ ~loc:lident.loc
695+
~attrs:[Variant_type_spread.mk_pat_from_variant_spread_attr ()]
696+
p1 p2)
697+
p ps
698+
in
699+
{root with ppat_loc = pat.ppat_loc; ppat_attributes = []}
688700
in
689701
Some (pat, ty)
690702
| _ -> None
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
Warning number 12
3+
/.../fixtures/warning_12_variant_spread.res:19:5-8
4+
5+
17 ┆ switch x {
6+
18 ┆ | One | Two(_) => 0
7+
19 ┆ | ...a | Three => 1
8+
20 ┆ }
9+
21 ┆
10+
11+
this sub-pattern is unused.
12+
13+
14+
Warning number 12
15+
/.../fixtures/warning_12_variant_spread.res:25:5-10
16+
17+
23 ┆ switch x {
18+
24 ┆ | Only => 0
19+
25 ┆ | ...one | Other => 1
20+
26 ┆ }
21+
27 ┆
22+
23+
this sub-pattern is unused.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
type a = One | Two(int)
2+
type b = | ...a | Three
3+
type c = | ...b | Four
4+
type one = Only
5+
type d = | ...one | Other
6+
7+
// Constructors of a spread that earlier cases already match aren't reported
8+
let f = (x: c) =>
9+
switch x {
10+
| Two(_) | Three => 0
11+
| ...b => 1
12+
| Four => 2
13+
}
14+
15+
// A spread that is unused as a whole is reported at the spread
16+
let g = (x: b) =>
17+
switch x {
18+
| One | Two(_) => 0
19+
| ...a | Three => 1
20+
}
21+
22+
let h = (x: d) =>
23+
switch x {
24+
| Only => 0
25+
| ...one | Other => 1
26+
}

0 commit comments

Comments
 (0)