5.7. Warnings and sanity-checking

GHC has a number of options that select which types of non-fatal error messages, otherwise known as warnings, can be generated during compilation. By default, you get a standard set of warnings which are generally likely to indicate bugs in your program. These are: -fwarn-overlapping-patterns, -fwarn-deprecations, -fwarn-duplicate-exports, -fwarn-missing-fields, and -fwarn-missing-methods. The following flags are simple ways to select standard “packages” of warnings:

-W:

Provides the standard warnings plus -fwarn-incomplete-patterns, -fwarn-dodgy-imports, -fwarn-unused-matches, -fwarn-unused-imports, and -fwarn-unused-binds.

-Wall:

Turns on all warning options that indicate potentially suspicious code. The warnings that are not enabled by -Wall are -fwarn-simple-patterns, -fwarn-tabs, -fwarn-incomplete-record-updates, -fwarn-monomorphism-restriction, and -fwarn-implicit-prelude.

-w:

Turns off all warnings, including the standard ones and those that -Wall doesn't enable.

-Werror:

Makes any warning into a fatal error. Useful so that you don't miss warnings when doing batch compilation.

-Wwarn:

Warnings are treated only as warnings, not as errors. This is the default, but can be useful to negate a -Werror flag.

The full set of warning options is described below. To turn off any warning, simply give the corresponding -fno-warn-... option on the command line.

-fwarn-deprecations:

Causes a warning to be emitted when a deprecated function or type is used. Entities can be marked as deprecated using a pragma, see Section 8.12.4, “DEPRECATED pragma”.

This option is on by default.

-fwarn-dodgy-imports:

Causes a warning to be emitted when a a datatype T is imported with all constructors, i.e. T(..), but has been exported abstractly, i.e. T.

-fwarn-duplicate-exports:

Have the compiler warn about duplicate entries in export lists. This is useful information if you maintain large export lists, and want to avoid the continued export of a definition after you've deleted (one) mention of it in the export list.

This option is on by default.

-fwarn-hi-shadowing:

Causes the compiler to emit a warning when a module or interface file in the current directory is shadowing one with the same module name in a library or other directory.

-fwarn-implicit-prelude:

Have the compiler warn if the Prelude is implicitly imported. This happens unless either the Prelude module is explicitly imported with an import ... Prelude ... line, or this implicit import is disabled (either by -fno-implicit-prelude or a LANGUAGE NoImplicitPrelude pragma).

Note that no warning is given for syntax that implicitly refers to the Prelude, even if -fno-implicit-prelude would change whether it refers to the Prelude. For example, no warning is given when 368 means Prelude.fromInteger (368::Prelude.Integer) (where Prelude refers to the actual Prelude module, regardless of the imports of the module being compiled).

This warning is off by default.

-fwarn-incomplete-patterns:

Similarly for incomplete patterns, the function g below will fail when applied to non-empty lists, so the compiler will emit a warning about this when -fwarn-incomplete-patterns is enabled.

g [] = 2

This option isn't enabled by default because it can be a bit noisy, and it doesn't always indicate a bug in the program. However, it's generally considered good practice to cover all the cases in your functions.

-fwarn-incomplete-record-updates:

The function f below will fail when applied to Bar, so the compiler will emit a warning about this when -fwarn-incomplete-record-updates is enabled.

data Foo = Foo { x :: Int }
         | Bar

f :: Foo -> Foo
f foo = foo { x = 6 }

This option isn't enabled by default because it can be very noisy, and it often doesn't indicate a bug in the program.

-fwarn-missing-fields:

This option is on by default, and warns you whenever the construction of a labelled field constructor isn't complete, missing initializers for one or more fields. While not an error (the missing fields are initialised with bottoms), it is often an indication of a programmer error.

-fwarn-missing-methods:

This option is on by default, and warns you whenever an instance declaration is missing one or more methods, and the corresponding class declaration has no default declaration for them.

The warning is suppressed if the method name begins with an underscore. Here's an example where this is useful:

	      class C a where
	        _simpleFn :: a -> String
	        complexFn :: a -> a -> String
	        complexFn x y = ... _simpleFn ...
	      

The idea is that: (a) users of the class will only call complexFn; never _simpleFn; and (b) instance declarations can define either complexFn or _simpleFn.

-fwarn-missing-signatures:

If you would like GHC to check that every top-level function/value has a type signature, use the -fwarn-missing-signatures option. As part of the warning GHC also reports the inferred type. The option is off by default.

-fwarn-name-shadowing:

This option causes a warning to be emitted whenever an inner-scope value has the same name as an outer-scope value, i.e. the inner value shadows the outer one. This can catch typographical errors that turn into hard-to-find bugs, e.g., in the inadvertent capture of what would be a recursive call in f = ... let f = id in ... f ....

-fwarn-orphans:

This option causes a warning to be emitted whenever the module contains an "orphan" instance declaration or rewrite rule. An instance declaration is an orphan if it appears in a module in which neither the class nor the type being instanced are declared in the same module. A rule is an orphan if it is a rule for a function declared in another module. A module containing any orphans is called an orphan module.

The trouble with orphans is that GHC must pro-actively read the interface files for all orphan modules, just in case their instances or rules play a role, whether or not the module's interface would otherwise be of any use. Other things being equal, avoid orphan modules.

-fwarn-overlapping-patterns:

By default, the compiler will warn you if a set of patterns are overlapping, e.g.,

f :: String -> Int
f []     = 0
f (_:xs) = 1
f "2"    = 2

where the last pattern match in f won't ever be reached, as the second pattern overlaps it. More often than not, redundant patterns is a programmer mistake/error, so this option is enabled by default.

-fwarn-simple-patterns:

Causes the compiler to warn about lambda-bound patterns that can fail, eg. \(x:xs)->.... Normally, these aren't treated as incomplete patterns by -fwarn-incomplete-patterns.

“Lambda-bound patterns” includes all places where there is a single pattern, including list comprehensions and do-notation. In these cases, a pattern-match failure is quite legitimate, and triggers filtering (list comprehensions) or the monad fail operation (monads). For example:

	      f :: [Maybe a] -> [a]
	      f xs = [y | Just y <- xs]
	      

Switching on -fwarn-simple-patterns will elicit warnings about these probably-innocent cases, which is why the flag is off by default.

-fwarn-tabs:

Have the compiler warn if there are tabs in your source file.

This warning is off by default.

-fwarn-type-defaults:

Have the compiler warn/inform you where in your source the Haskell defaulting mechanism for numeric types kicks in. This is useful information when converting code from a context that assumed one default into one with another, e.g., the ‘default default’ for Haskell 1.4 caused the otherwise unconstrained value 1 to be given the type Int, whereas Haskell 98 defaults it to Integer. This may lead to differences in performance and behaviour, hence the usefulness of being non-silent about this.

This warning is off by default.

-fwarn-monomorphism-restriction:

Have the compiler warn/inform you where in your source the Haskell Monomorphism Restriction is applied. If applied silently the MR can give rise to unexpected behaviour, so it can be helpful to have an explicit warning that it is being applied.

This warning is off by default.

-fwarn-unused-binds:

Report any function definitions (and local bindings) which are unused. For top-level functions, the warning is only given if the binding is not exported.

A definition is regarded as "used" if (a) it is exported, or (b) it is mentioned in the right hand side of another definition that is used, or (c) the function it defines begins with an underscore. The last case provides a way to suppress unused-binding warnings selectively.

Notice that a variable is reported as unused even if it appears in the right-hand side of another unused binding.

-fwarn-unused-imports:

Report any modules that are explicitly imported but never used. However, the form import M() is never reported as an unused import, because it is a useful idiom for importing instance declarations, which are anonymous in Haskell.

-fwarn-unused-matches:

Report all unused variables which arise from pattern matches, including patterns consisting of a single variable. For instance f x y = [] would report x and y as unused. The warning is suppressed if the variable name begins with an underscore, thus:

	       f _x = True
	    

If you're feeling really paranoid, the -dcore-lint option is a good choice. It turns on heavyweight intra-pass sanity-checking within GHC. (It checks GHC's sanity, not yours.)