Skip to content

Missing redundant import depending on import order and reexported symbols

Summary

The unused-import warning may appear or not depending on the order of the imports and reexported symbols.

Steps to reproduce

module A.hs:

module A (foo, bar) where

foo = 10

bar = 20

module B.hs:

module B (foo) where

import A

module C.hs:

{-# OPTIONS -Wall #-}
module C where

import A
import B

main :: IO ()
main = do
  print foo
  print bar

This leads to a warning when compiling C.hs:

C.hs:5:1: warning: [-Wunused-imports]
    The import of ‘B’ is redundant
      except perhaps to import instances from ‘B’
    To import instances alone, use: import B()
  |
5 | import B
  | ^^^^^^^^

However, if C.hs is instead:

{-# OPTIONS -Wall #-}
module C where

import B
import A

main :: IO ()
main = do
  print foo
  print bar

(Note that import B and import A have been swapped).

With this file, the warning does not appear.

Expected behavior

I would like the warning to not depend on the order of the imports and to be always there.

This surprising behavior have multiples impacts for me:

  • After applying a formatter which changed the import order, new warnings may appears, which can break the build (if -Werror).
  • Not having this warning is a missed opportunity to reduce dependencies footprint.

Environment

  • GHC version used: 9.6.1

Optional:

  • Operating System:
  • System Architecture:
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information