dotnet / fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
https://dotnet.microsoft.com/languages/fsharp
MIT License
3.92k stars 785 forks source link

FS0760 suggests change that breaks code (Quick Action) #6381

Open bjornbaverfjord opened 5 years ago

bjornbaverfjord commented 5 years ago

This working code triggers:

FS0760: It is recommended that objects supporting the IDisposable interface are created using the syntax 'new Type(args)', rather than 'Type(args)' or 'Type' as a function value representing the constructor, to indicate that resources may be owned by the generated value

open System.Media
open System.IO
let s = [|1uy; 2uy|]
let sp = new SoundPlayer(MemoryStream s) 
sp.PlaySync()

After applying suggested change in Visual Studio the result is:

open System.Media
open System.IO
let s = [|1uy; 2uy|]
let sp = new SoundPlayer(new MemoryStream s) 
sp.PlaySync()

This code is broken and gives compile error:

error FS0010: Unexpected symbol ')' in expression

The relevant source is here: CodeFix/AddNewKeywordToDisposableConstructorInvocation.fs

Microsoft Visual Studio Community 2017 Version 15.9.10 VisualStudio.15.Release/15.9.10+28307.557 Microsoft .NET Framework Version 4.7.03190 redsquiggle

cartermp commented 5 years ago

The correct approach here is the code fix needs to understand that curried parameters will cause an ambiguity and add parentheses:

MemoryStream s ->new MemoryStream(s)