Skip to content

Commit 4bfcdc1

Browse files
auduchinokbaronfel
authored andcommitted
Fix unused opens false positive for record fields (#6846)
* Fix unused opens false positive for record fields * Add record check
1 parent 8dea2d8 commit 4bfcdc1

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/fsharp/service/ServiceAnalysis.fs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,27 @@ module UnusedOpens =
9999
| :? FSharpMemberOrFunctionOrValue as fv when fv.IsExtensionMember ->
100100
// Extension members should be taken into account even though they have a prefix (as they do most of the time)
101101
true
102+
102103
| :? FSharpMemberOrFunctionOrValue as fv when not fv.IsModuleValueOrMember ->
103104
// Local values can be ignored
104105
false
106+
105107
| :? FSharpMemberOrFunctionOrValue when su.IsFromDefinition ->
106108
// Value definitions should be ignored
107109
false
110+
108111
| :? FSharpGenericParameter ->
109112
// Generic parameters can be ignored, they never come into scope via 'open'
110113
false
114+
111115
| :? FSharpUnionCase when su.IsFromDefinition ->
112116
false
117+
118+
| :? FSharpField as field when
119+
field.DeclaringEntity.IsSome && field.DeclaringEntity.Value.IsFSharpRecord ->
120+
// Record fields are used in name resolution
121+
true
122+
113123
| _ ->
114124
// For the rest of symbols we pick only those which are the first part of a long ident, because it's they which are
115125
// contained in opened namespaces / modules. For example, we pick `IO` from long ident `IO.File.OpenWrite` because

tests/service/ProjectAnalysisTests.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5523,6 +5523,14 @@ type UseTheThings(i:int) =
55235523
member x.UseSomeUsedModuleContainingActivePattern(ActivePattern g) = g
55245524
member x.UseSomeUsedModuleContainingExtensionMember() = (3).Q
55255525
member x.UseSomeUsedModuleContainingUnion() = A
5526+
5527+
module M1 =
5528+
type R = { Field: int }
5529+
5530+
module M2 =
5531+
open M1
5532+
5533+
let foo x = x.Field
55265534
"""
55275535
let fileSource1 = FSharp.Compiler.Text.SourceText.ofString fileSource1Text
55285536
File.WriteAllText(fileName1, fileSource1Text)

0 commit comments

Comments
 (0)