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

Quick fix for removing _ when using a union case with not data #1034

Closed
edgarfgp opened this issue Oct 26, 2022 · 3 comments · Fixed by #1142
Closed

Quick fix for removing _ when using a union case with not data #1034

edgarfgp opened this issue Oct 26, 2022 · 3 comments · Fixed by #1142
Labels
enhancement help wanted type:codefix Work that adds or updates a codefix in the product

Comments

@edgarfgp
Copy link
Contributor

I recently fixed dotnet/fsharp#13851

type X = X

 let x: X = X

 let myVal =
     match x with
     | X _ -> ()

let myFunc(X x) = 5+5
 let myDiscardedArgFunc(X _)

This will not longer be valid and a new warning error will be reported: Pattern discard is not allowed for union case that takes no data.
I'm an ionide user and I would love to contribute with some guidance on how to add a quick fix :)

@baronfel
Copy link
Contributor

baronfel commented Oct 26, 2022

I would look at https://github.com/fsharp/FsAutoComplete/blob/680a95b9d02c330d5b8a3a459271cc532efb368c/src/FsAutoComplete/CodeFixes/RenameUnusedValue.fs as a good example for this.

Generally speaking, a codefix has a trigger, which is often a specific error code, and one or more textEdits. In your case, you know the error code. The textEdit you want to perform is to remove some text and replace it with the empty string. Knowing this, you could make a new codefix for this by

  • Adding a new file to the CodeFixes folder of the FsAutoComplete project named for your codefix.
  • Fill your new file with this code as a base:
module FsAutoComplete.CodeFix.RenameUnusedValue

open FsToolkit.ErrorHandling
open FsAutoComplete.CodeFix
open FsAutoComplete.CodeFix.Types
open FsAutoComplete
open FsAutoComplete.LspHelpers
open FSharp.Compiler.Symbols
open FSharp.UMX
open FSharp.Compiler.Syntax
open FSharp.Compiler.Text

/// describe your fix in short
let fix: CodeFix =
  Run.ifDiagnosticByCode (Set.ofList [ YOUR_ERROR_CODE_HERE ]) (fun diagnostic codeActionParams ->
    asyncResult {
      // logic goes here - return an array of Fix items
    })
  • 'register' your codefix by adding it to the codefixes arrays for the standard LSP server here and the adaptive LSP server here
  • implement your codefix. you'll probably want to look at the other fixes for guidelines, but in general, you'll want to use the diagnostic's range to find the DU pattern being matched, then get the range of the _ token for that pattern. Once you have it, you can return a Fix item with a textEdit that assigns the empty string to that range.
  • Once you've implemented the codefix, test it! Add new tests to the codefix tests. they all follow a pattern of:
    • make a code sample that should trigger the problem
    • make some assertions about the fixes returned for the code sample
    • assert that the code after applying the fix looks correct

Hopefully that should give you enough to get started :)

@edgarfgp
Copy link
Contributor Author

Thanks for the detailed explanation. I will look into this :)

@edgarfgp
Copy link
Contributor Author

Once #1043 is completed we should be able to consume the new warning FS3548

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement help wanted type:codefix Work that adds or updates a codefix in the product
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants