Skip to content

Commit ceeba5c

Browse files
Fix misc unused opens analyzer bugs
1 parent ed10ddd commit ceeba5c

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/Compiler/Service/ServiceAnalysis.fs

+19-5
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ module UnusedOpens =
5757
for field in entity.FSharpFields do
5858
if field.IsStatic && field.IsLiteral then
5959
field
60+
61+
if
62+
entity.IsFSharpUnion
63+
&& not (entity.HasAttribute<RequireQualifiedAccessAttribute>())
64+
then
65+
for unionCase in entity.UnionCases do
66+
unionCase
6067
|]
6168

6269
HashSet<_>(symbols, symbolHash)
@@ -75,7 +82,7 @@ module UnusedOpens =
7582
[|
7683
yield OpenedModule(modul, isNestedAutoOpen)
7784
for ent in modul.NestedEntities do
78-
if ent.IsFSharpModule && ent.HasAttribute<AutoOpenAttribute>() then
85+
if ent.HasAttribute<AutoOpenAttribute>() then
7986
yield! getModuleAndItsAutoOpens true ent
8087
|]
8188

@@ -192,7 +199,7 @@ module UnusedOpens =
192199
not (
193200
usedModules.BagExistsValueForKey(
194201
openedEntity.Entity,
195-
fun scope -> rangeContainsRange scope openStatement.AppliedScope
202+
fun scope -> rangeContainsRange openStatement.AppliedScope scope
196203
)
197204
))
198205

@@ -271,7 +278,7 @@ module UnusedOpens =
271278

272279
/// Filter out the open statements whose contents are referred to somewhere in the symbol uses.
273280
/// Async to allow cancellation.
274-
let filterOpenStatements (symbolUses1: FSharpSymbolUse[], symbolUses2: FSharpSymbolUse[]) openStatements =
281+
let filterOpenStatements (symbolUses1: FSharpSymbolUse[], symbolUses2: FSharpSymbolUse[]) (openStatements: OpenStatement[]) =
275282
async {
276283
// the key is a namespace or module, the value is a list of FSharpSymbolUse range of symbols defined in the
277284
// namespace or module. So, it's just symbol uses ranges grouped by namespace or module where they are _defined_.
@@ -284,18 +291,25 @@ module UnusedOpens =
284291
match f.DeclaringEntity with
285292
| Some entity when entity.IsNamespace || entity.IsFSharpModule ->
286293
symbolUsesRangesByDeclaringEntity.BagAdd(entity, symbolUse.Range)
294+
295+
if f.ApparentEnclosingEntity <> entity then
296+
symbolUsesRangesByDeclaringEntity.BagAdd(f.ApparentEnclosingEntity, symbolUse.Range)
287297
| _ -> ()
288298
| _ -> ()
289299

300+
// Check the open statements in reverse order to account for shadowing.
301+
let reversedOpenStatements = openStatements |> List.ofArray |> List.rev
302+
290303
let! results =
291304
filterOpenStatementsIncremental
292305
symbolUses2
293306
symbolUsesRangesByDeclaringEntity
294-
(List.ofArray openStatements)
307+
reversedOpenStatements
295308
(Dictionary(entityHash))
296309
[]
297310

298-
return results |> List.map (fun os -> os.Range)
311+
// Re-reverse the results.
312+
return results |> List.map _.Range |> List.rev
299313
}
300314

301315
/// Get the open statements whose contents are not referred to anywhere in the symbol uses.

0 commit comments

Comments
 (0)