Warnings about unused variables or imports

Simon Peyton-Jones simonpj at microsoft.com
Tue May 6 05:09:17 EDT 2008


[Moving to GHC users: this thread concerns GHC's warning messages about unused things.]

| But there are a few other inconvenient behaviors related only to
| warnings, for instance
|
| module A where
| foo x = x
|
| module B(foo) where
| import A
|
| module C (module A, module B) where
| import A
| import B
|
| %> ghc --make C.hs -W:
|
| C.hs:1:20: Warning: `foo' is exported by `module B' and `module A'
|
| That's certainly true, but it's the same 'foo'. Just a warning of
| course, and I'm not so sure I would argue that it shouldn't be there.
| It's the same kind of warning as with any other multiple export of a
| symbol I guess. But this pattern is not uncommon (in my code at
| least), so the warning is inconvenient. Then again, that particular
| warning can be turned off.

Here the original intention was to warn about
        module C( foo, foo ) where...
or perhaps
        module C( module A, foo ) where...
but I agree that in the situation you describe the warning is silly. File a ticket for it?


| Another, somewhat stranger, behavior:
|
| module A where
| foo x = x
|
| module B(foo) where
| import A
|
| module C(foo) where
| import A
|
| module D () where
| import B
| import C
| bar x = foo x
|
| %> ghc --make D.hs -W
|
| D.hs:2:0:
|     Warning: Module `B' is imported, but nothing from it is used,
|                except perhaps instances visible in `B'
|              To suppress this warning, use: import B()
|
| D.hs:3:0:
|     Warning: Module `C' is imported, but nothing from it is used,
|                except perhaps instances visible in `C'
|              To suppress this warning, use: import C()
|
| D.hs:4:0: Warning: Defined but not used: `bar'
|
| This might be said to be a bug, although it's a bug that would rarely
| bite you, since it requires that 'bar' is not exported (or used in
| something that is exported). If you export bar, then the warning talks
| only of B - which is a rather arbitrary choice.

There is something more substantial here.  Suppose we have

    module Foo( g ) where
        import Bar( f4 )
        f1 = f2
        f2 = f3
        f3 = f4
        g = 7

At the moment GHC takes the view that f1,f2,f3,f4, and the import are *all* unused.  If we said that just f1 was unused, then you'd delete it and get a new warning about f2; you delete that and get a new warning about f3; and so on.

Yet the current behaviour is clearly confusing, even to a clued-up user, let alone a beginner.

I'm starting to think that we should just do the obvious thing: report something as unused only if it physically does not occur. Any opinions?


Your message also objects to arbitrarily choosing one of the imports as redundant. But if there are two imports
        import B
        import C
and only one is needed, what would you like GHC to say?  It's pretty hard to say "only one of these is needed", because it might be "of these three import declarations, you need 1&2 or 2&3 or 1&3".


Simon




More information about the Glasgow-haskell-users mailing list