Skip to content

Commit 51c8286

Browse files
authored
Report dead inline record fields: never used to read a label (#858)
* Report dead inline record fields: never used to read a label * Add test for destructuring an field from an inline record. * Update CHANGELOG.md
1 parent 394f437 commit 51c8286

File tree

5 files changed

+87
-3
lines changed

5 files changed

+87
-3
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
- Change end position of cursor when completing `Some(<fieldName>)` in patterns. https://github.com/rescript-lang/rescript-vscode/pull/857
1818

19+
20+
#### :bug: Bug Fix
21+
22+
- Add support for detecting dead fields inside inline records. https://github.com/rescript-lang/rescript-vscode/pull/858
23+
1924
## 1.28.0
2025

2126
#### :bug: Bug Fix

analysis/reanalyze/examples/deadcode/expected/deadcode.txt

+59-2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@
8787
addValueDeclaration +version DeadMl.ml:29:8 path:+DeadMl.Bs_version
8888
addValueDeclaration +header DeadMl.ml:30:8 path:+DeadMl.Bs_version
8989
addValueDeclaration +package_name DeadMl.ml:31:8 path:+DeadMl.Bs_version
90+
addRecordLabelDeclaration Lfunction.arity DeadMl.ml:35:21 path:+DeadMl.l
91+
addRecordLabelDeclaration Lfunction.params DeadMl.ml:36:21 path:+DeadMl.l
92+
addRecordLabelDeclaration Lfunction.body DeadMl.ml:37:21 path:+DeadMl.l
9093
addVariantCaseDeclaration Lfunction DeadMl.ml:35:2 path:+DeadMl.l
9194
addRecordLabelDeclaration module_name DeadMl.ml:41:2 path:+DeadMl.module_info
9295
addRecordLabelDeclaration case DeadMl.ml:42:2 path:+DeadMl.module_info
@@ -172,6 +175,7 @@
172175
addValueDeclaration +globallyLive3 DeadTest.res:154:6 path:+DeadTest.GloobLive
173176
addValueDeclaration +funWithInnerVars DeadTest.res:169:4 path:+DeadTest
174177
addValueDeclaration +deadIncorrect DeadTest.res:178:4 path:+DeadTest
178+
addValueDeclaration +ira DeadTest.res:184:4 path:+DeadTest
175179
addValueReference DeadTest.res:1:15 --> ImmutableArray.resi:9:0
176180
addValueReference DeadTest.res:8:7 --> DeadTest.res:7:4
177181
addValueReference DeadTest.res:11:7 --> DeadTest.res:10:4
@@ -249,6 +253,18 @@
249253
addRecordLabelDeclaration a DeadTest.res:175:11 path:+DeadTest.rc
250254
addValueDeclaration +_ DeadTest.res:180:0 path:+DeadTest
251255
addValueReference DeadTest.res:180:8 --> DeadTest.res:178:4
256+
addRecordLabelDeclaration IR.a DeadTest.res:182:24 path:+DeadTest.inlineRecord
257+
addRecordLabelDeclaration IR.b DeadTest.res:182:32 path:+DeadTest.inlineRecord
258+
addRecordLabelDeclaration IR.c DeadTest.res:182:40 path:+DeadTest.inlineRecord
259+
addVariantCaseDeclaration IR DeadTest.res:182:20 path:+DeadTest.inlineRecord
260+
addValueDeclaration +_ DeadTest.res:185:0 path:+DeadTest
261+
addTypeReference DeadTest.res:187:20 --> DeadTest.res:182:20
262+
addValueReference DeadTest.res:187:27 --> DeadTest.res:184:4
263+
addTypeReference DeadTest.res:187:35 --> DeadTest.res:182:32
264+
addValueReference DeadTest.res:187:35 --> DeadTest.res:187:7
265+
addValueReference DeadTest.res:187:43 --> DeadTest.res:187:8
266+
addTypeReference DeadTest.res:187:7 --> DeadTest.res:182:40
267+
addValueReference DeadTest.res:186:9 --> DeadTest.res:185:8
252268
addValueReference DeadTest.res:28:2 --> DeadTest.res:31:6
253269
addValueReference DeadTest.res:36:2 --> DeadTest.res:39:6
254270
addValueReference DeadTest.res:60:2 --> DeadTest.res:64:6
@@ -1665,6 +1681,7 @@
16651681
addVariantCaseDeclaration A Unboxed.res:5:10 path:+Unboxed.v2
16661682
addValueReference Unboxed.res:8:4 --> Unboxed.res:8:14
16671683
addRecordLabelDeclaration x Unboxed.res:11:11 path:+Unboxed.r1
1684+
addRecordLabelDeclaration B.g Unboxed.res:14:13 path:+Unboxed.r2
16681685
addVariantCaseDeclaration B Unboxed.res:14:10 path:+Unboxed.r2
16691686
addValueReference Unboxed.res:17:4 --> Unboxed.res:17:14
16701687
Scanning Uncurried.cmt Source:Uncurried.res
@@ -1984,6 +2001,9 @@ File References
19842001
Dead Value +DeadMl.+map_split_opt: 0 references () [0]
19852002
Dead RecordLabel +DeadMl.module_info.case: 0 references () [0]
19862003
Dead RecordLabel +DeadMl.module_info.module_name: 0 references () [0]
2004+
Dead RecordLabel +DeadMl.l.Lfunction.body: 0 references () [0]
2005+
Dead RecordLabel +DeadMl.l.Lfunction.params: 0 references () [0]
2006+
Dead RecordLabel +DeadMl.l.Lfunction.arity: 0 references () [0]
19872007
Dead VariantCase +DeadMl.l.Lfunction: 0 references () [0]
19882008
Dead Value +DeadMl.Bs_version.+package_name: 0 references () [1]
19892009
Dead Value +DeadMl.Bs_version.+package_name: 0 references () [0]
@@ -2005,6 +2025,12 @@ File References
20052025
Live VariantCase +DeadRT.moduleAccessPath.Root: 1 references (DeadRT.resi:2:2) [0]
20062026
Live VariantCase DeadRT.moduleAccessPath.Kaboom: 1 references (DeadRT.res:3:2) [0]
20072027
Dead Value +DeadTest.+_: 0 references () [0]
2028+
Live Value +DeadTest.+ira: 1 references (DeadTest.res:187:27) [0]
2029+
Live RecordLabel +DeadTest.inlineRecord.IR.c: 1 references (DeadTest.res:187:7) [0]
2030+
Live RecordLabel +DeadTest.inlineRecord.IR.b: 1 references (DeadTest.res:187:35) [0]
2031+
Dead RecordLabel +DeadTest.inlineRecord.IR.a: 0 references () [0]
2032+
Live VariantCase +DeadTest.inlineRecord.IR: 1 references (DeadTest.res:187:20) [0]
2033+
Dead Value +DeadTest.+_: 0 references () [0]
20082034
Live Value +DeadTest.+deadIncorrect: 1 references (DeadTest.res:180:8) [0]
20092035
Dead RecordLabel +DeadTest.rc.a: 0 references () [0]
20102036
Dead Value +DeadTest.+funWithInnerVars: 0 references () [1]
@@ -2350,6 +2376,7 @@ File References
23502376
Live Value +Types.+map: 0 references () [0]
23512377
Live Value +Types.+someIntList: 0 references () [0]
23522378
Live Value +Unboxed.+r2Test: 0 references () [0]
2379+
Dead RecordLabel +Unboxed.r2.B.g: 0 references () [0]
23532380
Dead VariantCase +Unboxed.r2.B: 0 references () [0]
23542381
Dead RecordLabel +Unboxed.r1.x: 0 references () [0]
23552382
Live Value +Unboxed.+testV1: 0 references () [0]
@@ -2838,6 +2865,24 @@ File References
28382865
<-- line 35
28392866
} [@dead "l.Lfunction"]
28402867

2868+
Warning Dead Type
2869+
DeadMl.ml:35:22-34
2870+
l.Lfunction.arity is a record label never used to read a value
2871+
<-- line 35
2872+
| Lfunction of { arity : int ; [@dead "l.Lfunction.arity"]
2873+
2874+
Warning Dead Type
2875+
DeadMl.ml:36:22-40
2876+
l.Lfunction.params is a record label never used to read a value
2877+
<-- line 36
2878+
params : int list ; [@dead "l.Lfunction.params"]
2879+
2880+
Warning Dead Type
2881+
DeadMl.ml:37:22-34
2882+
l.Lfunction.body is a record label never used to read a value
2883+
<-- line 37
2884+
body : string [@dead "l.Lfunction.body"]
2885+
28412886
Warning Dead Type
28422887
DeadMl.ml:41:3-22
28432888
module_info.module_name is a record label never used to read a value
@@ -3064,6 +3109,12 @@ File References
30643109
<-- line 175
30653110
type rc = {@dead("rc.a") a: int}
30663111

3112+
Warning Dead Type
3113+
DeadTest.res:182:25-30
3114+
inlineRecord.IR.a is a record label never used to read a value
3115+
<-- line 182
3116+
type inlineRecord = IR({@dead("inlineRecord.IR.a") a: int, b: int, c: string})
3117+
30673118
Warning Dead Module
30683119
DeadTestBlacklist.res:0:1
30693120
DeadTestBlacklist is a dead module as all its items are dead.
@@ -4448,7 +4499,13 @@ File References
44484499
Unboxed.res:14:11-24
44494500
r2.B is a variant case which is never constructed
44504501
<-- line 14
4451-
type r2 = | @dead("r2.B") B({g: string})
4502+
type r2 = | @dead("r2.B") B({@dead("r2.B.g") g: string})
4503+
4504+
Warning Dead Type
4505+
Unboxed.res:14:14-22
4506+
r2.B.g is a record label never used to read a value
4507+
<-- line 14
4508+
type r2 = | @dead("r2.B") B({@dead("r2.B.g") g: string})
44524509

44534510
Warning Dead Type
44544511
Variants.res:95:14-39
@@ -4528,4 +4585,4 @@ File References
45284585
<-- line 96
45294586
type variant1Object = | @dead("variant1Object.R") R(payload)
45304587

4531-
Analysis reported 334 issues (Incorrect Dead Annotation:1, Warning Dead Exception:2, Warning Dead Module:25, Warning Dead Type:89, Warning Dead Value:199, Warning Dead Value With Side Effects:2, Warning Redundant Optional Argument:5, Warning Unused Argument:11)
4588+
Analysis reported 339 issues (Incorrect Dead Annotation:1, Warning Dead Exception:2, Warning Dead Module:25, Warning Dead Type:94, Warning Dead Value:199, Warning Dead Value With Side Effects:2, Warning Redundant Optional Argument:5, Warning Unused Argument:11)

analysis/reanalyze/examples/deadcode/src/DeadTest.bs.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

analysis/reanalyze/examples/deadcode/src/DeadTest.res

+8
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,11 @@ type rc = {a: int}
178178
let deadIncorrect = 34
179179

180180
let _ = deadIncorrect
181+
182+
type inlineRecord = IR({a: int, b: int, c: string})
183+
184+
let ira = 10
185+
let _ = ir =>
186+
switch ir {
187+
| IR({c} as r) => IR({a: ira, b: r.b, c: c})
188+
}

analysis/reanalyze/src/DeadType.ml

+12-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,18 @@ let addDeclaration ~(typeId : Ident.t) ~(typeKind : Types.type_kind) =
102102
l
103103
| Type_variant decls ->
104104
List.iteri
105-
(fun i {Types.cd_id; cd_loc} ->
105+
(fun i {Types.cd_id; cd_loc; cd_args} ->
106+
let _handle_inline_records =
107+
match cd_args with
108+
| Cstr_record lbls ->
109+
List.iter
110+
(fun {Types.ld_id; ld_loc} ->
111+
Ident.name cd_id ^ "." ^ Ident.name ld_id
112+
|> Name.create
113+
|> processTypeLabel ~declKind:RecordLabel ~loc:ld_loc)
114+
lbls
115+
| Cstr_tuple _ -> ()
116+
in
106117
let posAdjustment =
107118
(* In Res the variant loc can include the | and spaces after it *)
108119
if WriteDeadAnnotations.posLanguage cd_loc.loc_start = Res then

0 commit comments

Comments
 (0)