Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect open type use correctly for enums #17628

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Compiler.Service/9.0.100.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Fixed checking failure when `global` namespace is involved with enabled GraphBasedChecking ([PR #17553](/~https://github.com/dotnet/fsharp/pull/17553))
* 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))
* Parentheses analysis: keep extra parentheses around unit & tuples in method definitions. ([PR #17618](/~https://github.com/dotnet/fsharp/pull/17618))
* 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))

### Added

Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/Service/ServiceAnalysis.fs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ module UnusedOpens =
if not entity.IsNamespace && not entity.IsFSharpModule then
for fv in entity.MembersFunctionsAndValues do
fv

if entity.IsEnum then
for field in entity.FSharpFields do
if field.IsStatic && field.IsLiteral then
field
|]

HashSet<_>(symbols, symbolHash)
Expand Down
16 changes: 15 additions & 1 deletion tests/FSharp.Compiler.Service.Tests/ProjectAnalysisTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5603,13 +5603,25 @@ open SomeUsedModuleContainingFunction
open SomeUsedModuleContainingExtensionMember
open SomeUsedModuleContainingActivePattern
open SomeUsedModuleContainingUnion
open type System.DayOfWeek // Used, should not appear.
open type System.DateTimeKind // Unused, should appear.

type FSharpEnum1 = X = 1 | Y = (1 <<< 1) | Z = (1 <<< 2)
type FSharpEnum2 = H = 1 | I = (1 <<< 1) | J = (1 <<< 2)

open type FSharpEnum1 // Used, should not appear.
open type FSharpEnum2 // Unused, should appear.

type UseTheThings(i:int) =
member x.Value = Dictionary<int,int>() // use something from System.Collections.Generic, as a constructor
member x.UseSomeUsedModuleContainingFunction() = g 3
member x.UseSomeUsedModuleContainingActivePattern(ActivePattern g) = g
member x.UseSomeUsedModuleContainingExtensionMember() = (3).Q
member x.UseSomeUsedModuleContainingUnion() = A
member x.UseEnumCase = Monday // Use an enum case from System.DayOfWeek.
member x.UseEnumCaseQualified = System.DateTimeKind.Utc // Use a qualified enum case.
member x.UseFSharpEnumCase = Y // Use an enum case from FSharpEnum1.
member x.UseFSharpEnumCaseQualified = FSharpEnum2.J // Use a qualified enum case.
"""
let fileSource1 = SourceText.ofString fileSource1Text
FileSystem.OpenFileForWriteShim(fileName1).Write(fileSource1Text)
Expand Down Expand Up @@ -5637,7 +5649,9 @@ type UseTheThings(i:int) =
(((6, 5), (6, 19)), "open FSharp.Control // unused");
(((7, 5), (7, 16)), "open FSharp.Data // unused");
(((8, 5), (8, 25)), "open System.Globalization // unused");
(((25, 5), (25, 21)), "open SomeUnusedModule")]
(((25, 5), (25, 21)), "open SomeUnusedModule");
(((31, 10), (31, 29)), "open type System.DateTimeKind // Unused, should appear.")
(((37, 10), (37, 21)), "open type FSharpEnum2 // Unused, should appear.")]
unusedOpensData |> shouldEqual expected

[<Theory>]
Expand Down
Loading