Skip to content

Code action to remove redundant imports is sometimes not created #4220

New issue

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

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

Already on GitHub? # to your account

Closed
jhrcek opened this issue May 10, 2024 · 2 comments
Closed

Code action to remove redundant imports is sometimes not created #4220

jhrcek opened this issue May 10, 2024 · 2 comments
Labels
Hackathon This issue is suitable for hackathon sessions level: easy The issue is suited for beginners type: bug Something isn't right: doesn't work as intended, documentation is missing/outdated, etc..

Comments

@jhrcek
Copy link
Collaborator

jhrcek commented May 10, 2024

Using hls 2.7.0.0 in vscode with ghc 9.4.8, I sometimes don't get "remove redundant imports" code action.

Here's a one-module reproducer (assuming you have a cabal project with postgresql-simple dependency):

module Main where

import Database.PostgreSQL.Simple.Types (In (..), PGArray (..), Values (..), fromIdentifier, fromQuery)

main :: IO ()
main = do
    let array = PGArray []
        in_ = In []
        values = Values [] []
    putStrLn "Hello, Haskell!"

Although warning is generated and displayed on hover:

The import of ‘Identifier(fromIdentifier), Query(fromQuery)’
from module ‘Database.PostgreSQL.Simple.Types’ is redundanttypecheck(-Wunused-imports)

The usual code action to remove unused import is not created.
Not a big deal, but it's breaking my flow, as now I have to focus on what the warning is exactly saying and fix it manually 😄

@jhrcek jhrcek added type: bug Something isn't right: doesn't work as intended, documentation is missing/outdated, etc.. status: needs triage labels May 10, 2024
@jhrcek jhrcek changed the title Code action to remove redundant imports is sometimes missing Code action to remove redundant imports is sometimes not created May 10, 2024
@fendor fendor added Hackathon This issue is suitable for hackathon sessions and removed status: needs triage labels May 10, 2024
@jhrcek
Copy link
Collaborator Author

jhrcek commented May 12, 2024

Hint for anyone looking into fixing this
This seems to happen when the unused field is a record field selector (for both data and newtypes). When these are unused, GHC warning actually mentions `RecordConstructor(recordFieldName)` in the diagnostic.

The place where this breaks down is in this function:

rangesForBinding' :: String -> LIE GhcPs -> [SrcSpan]
rangesForBinding' b (L (locA -> l) (IEVar _ nm))
| L _ (IEPattern _ (L _ b')) <- nm
, T.unpack (printOutputable b') == b
= [l]
rangesForBinding' b (L (locA -> l) x@IEVar{})
| T.unpack (printOutputable x) == b = [l]
rangesForBinding' b (L (locA -> l) x@IEThingAbs{}) | T.unpack (printOutputable x) == b = [l]
rangesForBinding' b (L (locA -> l) (IEThingAll _ x)) | T.unpack (printOutputable x) == b = [l]
rangesForBinding' b (L (locA -> l) (IEThingWith _ thing _ inners))
| T.unpack (printOutputable thing) == b = [l]
| otherwise =
[ locA l' | L l' x <- inners, T.unpack (printOutputable x) == b]
rangesForBinding' _ _ = []

which gets passed b::String like RecordConstructor(recordFieldName), tries to compare it with printOutputable b' which at this point is just recordFieldName, which are not equal and so no code action is generated for these.

@michaelpj michaelpj added the level: easy The issue is suited for beginners label May 15, 2024
@battermann
Copy link
Contributor

Looking into this

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Hackathon This issue is suitable for hackathon sessions level: easy The issue is suited for beginners type: bug Something isn't right: doesn't work as intended, documentation is missing/outdated, etc..
Projects
Development

No branches or pull requests

4 participants