Skip to content

Commit

Permalink
Fix unused open type declaration detection (dotnet#10510)
Browse files Browse the repository at this point in the history
  • Loading branch information
asik authored and nosami committed Feb 22, 2021
1 parent b6dd7ce commit 5c17e26
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/fsharp/service/ServiceAnalysis.fs
Expand Up @@ -67,9 +67,9 @@ module UnusedOpens =
yield! getModuleAndItsAutoOpens true ent |]
{ OpenedModules = getModuleAndItsAutoOpens false modul }

/// Represents single open statement.
/// Represents a single open statement
type OpenStatement =
{ /// All namespaces and modules which this open declaration effectively opens, including the AutoOpen ones
{ /// All namespaces, modules and types which this open declaration effectively opens, including the AutoOpen ones
OpenedGroups: OpenedModuleGroup list

/// The range of open statement itself
Expand All @@ -90,7 +90,8 @@ module UnusedOpens =
if firstId.idText = MangledGlobalName then
None
else
Some { OpenedGroups = openDecl.Modules |> List.map OpenedModuleGroup.Create
let openedModulesAndTypes = List.concat [openDecl.Modules; openDecl.Types |> List.map(fun ty -> ty.TypeDefinition)]
Some { OpenedGroups = openedModulesAndTypes |> List.map OpenedModuleGroup.Create
Range = range
AppliedScope = openDecl.AppliedScope }
| _ -> None)
Expand Down
47 changes: 46 additions & 1 deletion vsintegration/tests/UnitTests/UnusedOpensTests.fs
Expand Up @@ -808,4 +808,49 @@ open Nested
let _ = f 1
"""
=> []
=> []

[<Test>]
let ``used open C# type``() =
"""
open type System.Console
WriteLine("Hello World")
"""
=> []

[<Test>]
let ``unused open C# type``() =
"""
open type System.Console
printfn "%s" "Hello World"
"""
=> [2, (10, 24)]

[<Test>]
let ``used open type from module``() =
"""
module MyModule =
type Thingy =
static member Thing = ()
open type MyModule.Thingy
printfn "%A" Thing
"""
=> []

[<Test>]
let ``unused open type from module``() =
"""
module MyModule =
type Thingy =
static member Thing = ()
open type MyModule.Thingy
printfn "%A" MyModule.Thingy.Thing
"""
=> [6, (10, 25)]

0 comments on commit 5c17e26

Please sign in to comment.