Skip to content
This repository has been archived by the owner on Jan 2, 2021. It is now read-only.

Code action add default type annotation to remove -Wtype-defaults warning #680

Merged

Conversation

serhiip
Copy link
Contributor

@serhiip serhiip commented Jul 1, 2020

This PR adds a code action to add default type annotation suggested by GHC when -Wtype-defaults is enabled.

I noticed that during development, when I use traceShow from Debug.Trace I often get annoyed by sudden warning displayed, so I usually urged to add that default type annotation manually. These changes add a way to do that via a hotkey.

I hope this is useful to someone else.

These changes handle only literals, not expressions. For example for binding f = 2 + 2 (without type signature defined) the warning will be shown, but no code action produced.

I've tried to handle all the cases of the appearance of this warning, and I am interested in finding more examples to handle (if there is some).

If this doesn't seem like a useful addition, I am fine with not merging it

     this is useful when using `traceShow` with with OverloadedStrings
     and type-defaults warning enabled

     Handle the following cases:

            - there is one literal and one contraint to be satisfied

            - there are mulitple literals and/or multiple constraints

     Adding type annotations to expressions that trigger type-defaults
     warning is not part of this changes
actions <- getCodeActions doc range
let matches = sequence
[ listToMaybe
[ action
| CACodeAction action@CodeAction { _title = actionTitle } <- actions
, actionTitle == expectedTitle ]
, expectedTitle `op` actionTitle]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I swapped the order of application of arguments. That matters only for T.isPrefixOf (the version which I added), not for ==

@serhiip serhiip changed the title Code action add default type annotation Code action add default type annotation to remove -Wtype-defaults warning Jul 1, 2020
Copy link
Collaborator

@cocreature cocreature left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thank you

@cocreature cocreature merged commit 9272bfe into haskell:master Jul 10, 2020
@serhiip serhiip deleted the code-action-add-default-type-annotation branch July 10, 2020 14:46
pepeiborra pushed a commit to pepeiborra/ide that referenced this pull request Dec 29, 2020
…arning (haskell/ghcide#680)

* Code action to add default type annotation to satisfy the contraints

     this is useful when using `traceShow` with with OverloadedStrings
     and type-defaults warning enabled

     Handle the following cases:

            - there is one literal and one contraint to be satisfied

            - there are mulitple literals and/or multiple constraints

     Adding type annotations to expressions that trigger type-defaults
     warning is not part of this changes

* Simplify older test

* Fix hlint issue
pepeiborra pushed a commit to pepeiborra/ide that referenced this pull request Dec 29, 2020
…arning (haskell/ghcide#680)

* Code action to add default type annotation to satisfy the contraints

     this is useful when using `traceShow` with with OverloadedStrings
     and type-defaults warning enabled

     Handle the following cases:

            - there is one literal and one contraint to be satisfied

            - there are mulitple literals and/or multiple constraints

     Adding type annotations to expressions that trigger type-defaults
     warning is not part of this changes

* Simplify older test

* Fix hlint issue
pepeiborra pushed a commit to pepeiborra/ide that referenced this pull request Dec 29, 2020
…arning (haskell/ghcide#680)

* Code action to add default type annotation to satisfy the contraints

     this is useful when using `traceShow` with with OverloadedStrings
     and type-defaults warning enabled

     Handle the following cases:

            - there is one literal and one contraint to be satisfied

            - there are mulitple literals and/or multiple constraints

     Adding type annotations to expressions that trigger type-defaults
     warning is not part of this changes

* Simplify older test

* Fix hlint issue
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants