4.8. 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-overlpapping-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-unused-matches, -fwarn-unused-imports, -fwarn-misc, and -fwarn-unused-binds.

-w:

Turns off all warnings, including the standard ones.

-Wall:

Turns on all warning options.

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 7.15.7.

-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-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 be 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-misc:

Turns on warnings for various harmless but untidy things. This currently includes: importing a type with (..) when the export is abstract, and listing duplicate class assertions in a qualified type.

-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.

-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. This 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 cyclic definition let x = ... x ... in.

Consequently, this option does will complain about cyclic recursive definitions.

-fwarn-overlapping-patterns:

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

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.

-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-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.

-fwarn-unused-imports:

Report any objects that are explicitly imported but never used.

-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. To eliminate the warning, all unused variables can be replaced with wildcards.

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.)