haskell-language-server icon indicating copy to clipboard operation
haskell-language-server copied to clipboard

Code action to remove redundant imports is sometimes not created

Open jhrcek opened this issue 1 year ago • 1 comments

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 :smile:

jhrcek avatar May 10 '24 08:05 jhrcek

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: https://github.com/haskell/haskell-language-server/blob/23005f8b1dd6a6d8b9248138629a3c4561bd8843/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs#L1942-L1955 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.

jhrcek avatar May 12 '24 14:05 jhrcek

Looking into this

battermann avatar Jun 09 '24 10:06 battermann