Skip to content

Commit 1f1ee49

Browse files
Detect open type use correctly for enums (#17628)
* Detect `open type` use correctly for enums * Consider `open type` used when the type is an enum and any of the enum cases is used unqualified. * Update release notes
1 parent 138593b commit 1f1ee49

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

docs/release-notes/.FSharp.Compiler.Service/9.0.100.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* Fixed checking failure when `global` namespace is involved with enabled GraphBasedChecking ([PR #17553](https://github.com/dotnet/fsharp/pull/17553))
1515
* Add missing byte chars notations, enforce limits in decimal notation in byte char & string (Issues [#15867](https://github.com/dotnet/fsharp/issues/15867), [#15868](https://github.com/dotnet/fsharp/issues/15868), [#15869](https://github.com/dotnet/fsharp/issues/15869), [PR #15898](https://github.com/dotnet/fsharp/pull/15898))
1616
* Parentheses analysis: keep extra parentheses around unit & tuples in method definitions. ([PR #17618](https://github.com/dotnet/fsharp/pull/17618))
17+
* Consider `open type` used when the type is an enum and any of the enum cases is used unqualified. ([PR #17628](https://github.com/dotnet/fsharp/pull/17628))
1718

1819
### Added
1920

src/Compiler/Service/ServiceAnalysis.fs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ module UnusedOpens =
5151
if not entity.IsNamespace && not entity.IsFSharpModule then
5252
for fv in entity.MembersFunctionsAndValues do
5353
fv
54+
55+
if entity.IsEnum then
56+
for field in entity.FSharpFields do
57+
if field.IsStatic && field.IsLiteral then
58+
field
5459
|]
5560

5661
HashSet<_>(symbols, symbolHash)

tests/FSharp.Compiler.Service.Tests/ProjectAnalysisTests.fs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5603,13 +5603,25 @@ open SomeUsedModuleContainingFunction
56035603
open SomeUsedModuleContainingExtensionMember
56045604
open SomeUsedModuleContainingActivePattern
56055605
open SomeUsedModuleContainingUnion
5606+
open type System.DayOfWeek // Used, should not appear.
5607+
open type System.DateTimeKind // Unused, should appear.
5608+
5609+
type FSharpEnum1 = X = 1 | Y = (1 <<< 1) | Z = (1 <<< 2)
5610+
type FSharpEnum2 = H = 1 | I = (1 <<< 1) | J = (1 <<< 2)
5611+
5612+
open type FSharpEnum1 // Used, should not appear.
5613+
open type FSharpEnum2 // Unused, should appear.
56065614
56075615
type UseTheThings(i:int) =
56085616
member x.Value = Dictionary<int,int>() // use something from System.Collections.Generic, as a constructor
56095617
member x.UseSomeUsedModuleContainingFunction() = g 3
56105618
member x.UseSomeUsedModuleContainingActivePattern(ActivePattern g) = g
56115619
member x.UseSomeUsedModuleContainingExtensionMember() = (3).Q
56125620
member x.UseSomeUsedModuleContainingUnion() = A
5621+
member x.UseEnumCase = Monday // Use an enum case from System.DayOfWeek.
5622+
member x.UseEnumCaseQualified = System.DateTimeKind.Utc // Use a qualified enum case.
5623+
member x.UseFSharpEnumCase = Y // Use an enum case from FSharpEnum1.
5624+
member x.UseFSharpEnumCaseQualified = FSharpEnum2.J // Use a qualified enum case.
56135625
"""
56145626
let fileSource1 = SourceText.ofString fileSource1Text
56155627
FileSystem.OpenFileForWriteShim(fileName1).Write(fileSource1Text)
@@ -5637,7 +5649,9 @@ type UseTheThings(i:int) =
56375649
(((6, 5), (6, 19)), "open FSharp.Control // unused");
56385650
(((7, 5), (7, 16)), "open FSharp.Data // unused");
56395651
(((8, 5), (8, 25)), "open System.Globalization // unused");
5640-
(((25, 5), (25, 21)), "open SomeUnusedModule")]
5652+
(((25, 5), (25, 21)), "open SomeUnusedModule");
5653+
(((31, 10), (31, 29)), "open type System.DateTimeKind // Unused, should appear.")
5654+
(((37, 10), (37, 21)), "open type FSharpEnum2 // Unused, should appear.")]
56415655
unusedOpensData |> shouldEqual expected
56425656

56435657
[<Theory>]

0 commit comments

Comments
 (0)