using-warnings.rst 31.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
.. _options-sanity:

Warnings and sanity-checking
----------------------------

.. index::
   single: sanity-checking options
   single: warnings

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:

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
.. hlist::
    :columns: 3

    * :ghc-flag:`-Woverlapping-patterns`
    * :ghc-flag:`-Wwarnings-deprecations`
    * :ghc-flag:`-Wdeprecated-flags`
    * :ghc-flag:`-Wunrecognised-pragmas`
    * :ghc-flag:`-Wduplicate-constraints`
    * :ghc-flag:`-Wduplicate-exports`
    * :ghc-flag:`-Woverflowed-literals`
    * :ghc-flag:`-Wempty-enumerations`
    * :ghc-flag:`-Wmissing-fields`
    * :ghc-flag:`-Wmissing-methods`
    * :ghc-flag:`-Wwrong-do-bind`
    * :ghc-flag:`-Wunsupported-calling-conventions`
    * :ghc-flag:`-Wdodgy-foreign-imports`
    * :ghc-flag:`-Winline-rule-shadowing`
    * :ghc-flag:`-Wunsupported-llvm-version`
    * :ghc-flag:`-Wtabs`
34
    * :ghc-flag:`-Wunrecognised-warning-flags`
35 36 37 38 39

The following flags are simple ways to select standard "packages" of warnings:

.. ghc-flag:: -W

40 41 42 43 44 45 46
    Provides the standard warnings plus

    .. hlist::
        :columns: 3

        * :ghc-flag:`-Wunused-binds`
        * :ghc-flag:`-Wunused-matches`
47
        * :ghc-flag:`-Wunused-foralls`
48 49 50 51
        * :ghc-flag:`-Wunused-imports`
        * :ghc-flag:`-Wincomplete-patterns`
        * :ghc-flag:`-Wdodgy-exports`
        * :ghc-flag:`-Wdodgy-imports`
52 53

.. ghc-flag:: -Wall
54 55

    Turns on all warning options that indicate potentially suspicious
56
    code. The warnings that are *not* enabled by :ghc-flag:`-Wall` are
57 58 59 60 61 62 63 64 65 66 67 68

    .. hlist::
        :columns: 3

        * :ghc-flag:`-Wincomplete-uni-patterns`
        * :ghc-flag:`-Wincomplete-record-updates`
        * :ghc-flag:`-Wmonomorphism-restriction`
        * :ghc-flag:`-Wimplicit-prelude`
        * :ghc-flag:`-Wmissing-local-sigs`
        * :ghc-flag:`-Wmissing-exported-sigs`
        * :ghc-flag:`-Wmissing-import-lists`
        * :ghc-flag:`-Widentities`
69

70
.. ghc-flag:: -Wcompat
quchen's avatar
quchen committed
71 72 73 74 75 76

    Turns on warnings that will be enabled by default in the future, but remain
    off in normal compilations for the time being. This allows library authors
    eager to make their code future compatible to adapt to new features before
    they even generate warnings.

77 78 79 80 81
    This currently enables

    .. hlist::
        :columns: 3

82
        * :ghc-flag:`-Wmissing-monadfail-instances`
83 84
        * :ghc-flag:`-Wsemigroup`
        * :ghc-flag:`-Wnoncanonical-monoid-instances`
quchen's avatar
quchen committed
85

86
.. ghc-flag:: -Wno-compat
quchen's avatar
quchen committed
87

88
    Disables all warnings enabled by :ghc-flag:`-Wcompat`.
quchen's avatar
quchen committed
89

90
.. ghc-flag:: -w
91 92

    Turns off all warnings, including the standard ones and those that
93
    :ghc-flag:`-Wall` doesn't enable.
94

95
.. ghc-flag:: -Werror
96 97 98 99

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

100
.. ghc-flag:: -Wwarn
101 102

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

The full set of warning options is described below. To turn off any
106 107 108 109
warning, simply give the corresponding ``-Wno-...`` option on the
command line. For backwards compatibility with GHC versions prior to 8.0,
all these warnings can still be controlled with ``-f(no-)warn-*`` instead
of ``-W(no-)*``.
110

111 112 113 114 115 116 117
.. ghc-flag:: -Wunrecognised-warning-flags

    Enables warnings when the compiler encounters a ``-W...`` flag that is not
    recognised.

    This warning is on by default.

118
.. ghc-flag:: -Wtyped-holes
119 120 121 122 123 124 125

    Determines whether the compiler reports typed holes warnings. Has no
    effect unless typed holes errors are deferred until runtime. See
    :ref:`typed-holes` and :ref:`defer-type-errors`

    This warning is on by default.

126
.. ghc-flag:: -Wtype-errors
127 128 129 130 131 132

    Causes a warning to be reported when a type error is deferred until
    runtime. See :ref:`defer-type-errors`

    This warning is on by default.

133
.. ghc-flag:: -fdefer-type-errors
134

Ben Gamari's avatar
Ben Gamari committed
135 136
    :implies: :ghc-flag:`-fdefer-typed-holes`

137 138 139 140 141 142
    Defer as many type errors as possible until runtime. At compile time
    you get a warning (instead of an error). At runtime, if you use a
    value that depends on a type error, you get a runtime error; but you
    can run any type-correct parts of your code just fine. See
    :ref:`defer-type-errors`

143
.. ghc-flag:: -fdefer-typed-holes
144 145 146 147

    Defer typed holes errors until runtime. This will turn the errors
    produced by :ref:`typed holes <typed-holes>` into warnings. Using a value
    that depends on a typed hole produces a runtime error, the same as
148
    :ghc-flag:`-fdefer-type-errors` (which implies this option). See :ref:`typed-holes`
149 150
    and :ref:`defer-type-errors`.

151
    Implied by :ghc-flag:`-fdefer-type-errors`. See also :ghc-flag:`-Wtyped-holes`.
152

153
.. ghc-flag:: -Wpartial-type-signatures
154 155 156

    Determines whether the compiler reports holes in partial type
    signatures as warnings. Has no effect unless
157
    :ghc-flag:`-XPartialTypeSignatures` is enabled, which controls whether
158 159 160 161 162
    errors should be generated for holes in types or not. See
    :ref:`partial-type-signatures`.

    This warning is on by default.

163
.. ghc-flag:: -fhelpful-errors
164 165 166 167 168 169

    When a name or package is not found in scope, make suggestions for
    the name or package you might have meant instead.

    This option is on by default.

170
.. ghc-flag:: -Wunrecognised-pragmas
171 172 173 174 175 176 177 178

    Causes a warning to be emitted when a pragma that GHC doesn't
    recognise is used. As well as pragmas that GHC itself uses, GHC also
    recognises pragmas known to be used by other tools, e.g.
    ``OPTIONS_HUGS`` and ``DERIVE``.

    This option is on by default.

179 180
.. ghc-flag:: -Wmissed-specialisations
              -Wall-missed-specialisations
181

182 183 184 185 186 187 188 189 190
    Emits a warning if GHC cannot specialise an overloaded function, usually
    because the function needs an ``INLINEABLE`` pragma. The "all" form reports
    all such situations whereas the "non-all" form only reports when the
    situation arises during specialisation of an imported function.

    The "non-all" form is intended to catch cases where an imported function
    that is marked as ``INLINEABLE`` (presumably to enable specialisation) cannot
    be specialised as it calls other functions that are themselves not specialised.

191 192
    Note that these warnings will not throw errors if used with :ghc-flag:`-Werror`.

193
    These options are both off by default.
194

195 196
.. ghc-flag:: -Wwarnings-deprecations

197
    .. index::
198
       pair: deprecations; warnings
199 200 201 202 203 204 205

    Causes a warning to be emitted when a module, function or type with
    a ``WARNING`` or ``DEPRECATED pragma`` is used. See
    :ref:`warning-deprecated-pragma` for more details on the pragmas.

    This option is on by default.

206 207
.. ghc-flag:: -Wamp

208 209 210 211
    .. index::
       single: AMP
       single: Applicative-Monad Proposal

quchen's avatar
quchen committed
212
    This option is deprecated.
213

quchen's avatar
quchen committed
214 215 216
    Caused a warning to be emitted when a definition was in conflict with
    the AMP (Applicative-Monad proosal).

217
.. ghc-flag:: -Wnoncanonical-monad-instances
218 219 220 221 222 223 224 225 226 227 228 229

    Warn if noncanonical ``Applicative`` or ``Monad`` instances
    declarations are detected.

    When this warning is enabled, the following conditions are verified:

    In ``Monad`` instances declarations warn if any of the following
    conditions does not hold:

     * If ``return`` is defined it must be canonical (i.e. ``return = pure``).
     * If ``(>>)`` is defined it must be canonical (i.e. ``(>>) = (*>)``).

230
    Moreover, in ``Applicative`` instance declarations:
231 232 233 234 235 236

     * Warn if ``pure`` is defined backwards (i.e. ``pure = return``).
     * Warn if ``(*>)`` is defined backwards (i.e. ``(*>) = (>>)``).

    This option is off by default.

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
.. ghc-flag:: -Wnoncanonical-monadfail-instances

    Warn if noncanonical ``Monad`` or ``MonadFail`` instances
    declarations are detected.

    When this warning is enabled, the following conditions are verified:

    In ``Monad`` instances declarations warn if any of the following
    conditions does not hold:

     * If ``fail`` is defined it must be canonical
       (i.e. ``fail = Control.Monad.Fail.fail``).

    Moreover, in ``MonadFail`` instance declarations:

     * Warn if ``fail`` is defined backwards
       (i.e. ``fail = Control.Monad.fail``).

255
    See also :ghc-flag:`-Wmissing-monadfail-instances`.
256 257 258

    This option is off by default.

259
.. ghc-flag:: -Wnoncanonical-monoid-instances
260 261 262 263 264 265 266 267 268 269 270 271

    Warn if noncanonical ``Semigroup`` or ``Monoid`` instances
    declarations are detected.

    When this warning is enabled, the following conditions are verified:

    In ``Monoid`` instances declarations warn if any of the following
    conditions does not hold:

     * If ``mappend`` is defined it must be canonical
       (i.e. ``mappend = (Data.Semigroup.<>)``).

272
    Moreover, in ``Semigroup`` instance declarations:
273 274 275 276

     * Warn if ``(<>)`` is defined backwards (i.e. ``(<>) = mappend``).

    This warning is off by default. However, it is part of the
277 278
    :ghc-flag:`-Wcompat` option group.

279
.. ghc-flag:: -Wmissing-monadfail-instances
280

quchen's avatar
quchen committed
281 282 283 284 285 286 287
    .. index::
       single: MFP
       single: MonadFail Proposal

    Warn when a failable pattern is used in a do-block that does not have a
    ``MonadFail`` instance.

288 289
    See also :ghc-flag:`-Wnoncanonical-monadfail-instances`.

290
    Being part of the :ghc-flag:`-Wcompat` option group, this warning is off by
quchen's avatar
quchen committed
291
    default, but will be switched on in a future GHC release, as part of
292 293
    the `MonadFail Proposal (MFP)
    <https://prime.haskell.org/wiki/Libraries/Proposals/MonadFail>`__.
294

295 296
.. ghc-flag:: -Wsemigroup

297
    .. index::
298
       single: semigroup; warning
299 300 301 302

    Warn when definitions are in conflict with the future inclusion of
    ``Semigroup`` into the standard typeclasses.

303 304 305
     1. Instances of ``Monoid`` should also be instances of ``Semigroup``
     2. The ``Semigroup`` operator ``(<>)`` will be in ``Prelude``, which
        clashes with custom local definitions of such an operator
306

307
    Being part of the :ghc-flag:`-Wcompat` option group, this warning is off by
308 309
    default, but will be switched on in a future GHC release.

310 311
.. ghc-flag:: -Wdeprecated-flags

312
    .. index::
313
       single: deprecated flags
314 315 316 317 318 319

    Causes a warning to be emitted when a deprecated command-line flag
    is used.

    This option is on by default.

320
.. ghc-flag:: -Wunsupported-calling-conventions
321 322 323 324 325 326

    Causes a warning to be emitted for foreign declarations that use
    unsupported calling conventions. In particular, if the ``stdcall``
    calling convention is used on an architecture other than i386 then
    it will be treated as ``ccall``.

327
.. ghc-flag:: -Wdodgy-foreign-imports
328 329

    Causes a warning to be emitted for foreign imports of the following
330
    form: ::
331 332 333

        foreign import "f" f :: FunPtr t

334
    on the grounds that it probably should be ::
335 336 337 338 339 340 341 342 343 344

        foreign import "&f" f :: FunPtr t

    The first form declares that \`f\` is a (pure) C function that takes
    no arguments and returns a pointer to a C function with type \`t\`,
    whereas the second form declares that \`f\` itself is a C function
    with type \`t\`. The first declaration is usually a mistake, and one
    that is hard to debug because it results in a crash, hence this
    warning.

345
.. ghc-flag:: -Wdodgy-exports
346 347 348 349 350 351 352 353

    Causes a warning to be emitted when a datatype ``T`` is exported
    with all constructors, i.e. ``T(..)``, but is it just a type
    synonym.

    Also causes a warning to be emitted when a module is re-exported,
    but that module exports nothing.

354
.. ghc-flag:: -Wdodgy-imports
355 356 357 358 359 360 361 362 363

    Causes a warning to be emitted in the following cases:

    -  When a datatype ``T`` is imported with all constructors, i.e.
       ``T(..)``, but has been exported abstractly, i.e. ``T``.

    -  When an ``import`` statement hides an entity that is not
       exported.

364
.. ghc-flag:: -Woverflowed-literals
365 366 367 368

    Causes a warning to be emitted if a literal will overflow, e.g.
    ``300 :: Word8``.

369
.. ghc-flag:: -Wempty-enumerations
370 371 372 373

    Causes a warning to be emitted if an enumeration is empty, e.g.
    ``[5 .. 3]``.

374 375
.. ghc-flag:: -Wduplicate-constraints

376 377 378 379
    .. index::
       single: duplicate constraints, warning

    Have the compiler warn about duplicate constraints in a type
380
    signature. For example ::
381 382 383 384 385 386

        f :: (Eq a, Show a, Eq a) => a -> a

    The warning will indicate the duplicated ``Eq a`` constraint.

    This option is now deprecated in favour of
387 388 389
    :ghc-flag:`-Wredundant-constraints`.

.. ghc-flag:: -Wredundant-constraints
390 391 392 393 394 395 396

    .. index::
       single: redundant constraints, warning

    Have the compiler warn about redundant constraints in a type
    signature. In particular:

397
    -  A redundant constraint within the type signature itself: ::
398 399 400 401 402 403 404

            f :: (Eq a, Ord a) => a -> a

       The warning will indicate the redundant ``Eq a`` constraint: it
       is subsumed by the ``Ord a`` constraint.

    -  A constraint in the type signature is not used in the code it
405
       covers: ::
406 407 408 409 410 411 412 413 414 415 416

            f :: Eq a => a -> a -> Bool
            f x y = True

       The warning will indicate the redundant ``Eq a`` constraint: : it
       is not used by the definition of ``f``.)

    Similar warnings are given for a redundant constraint in an instance
    declaration.

    This option is on by default. As usual you can suppress it on a
417
    per-module basis with :ghc-flag:`-Wno-redundant-constraints`.
418 419 420 421
    Occasionally you may specifically want a function to have a more
    constrained signature than necessary, perhaps to leave yourself
    wiggle-room for changing the implementation without changing the
    API. In that case, you can suppress the warning on a per-function
422
    basis, using a call in a dead binding. For example: ::
423 424 425 426 427 428 429 430 431

        f :: Eq a => a -> a -> Bool
        f x y = True
        where
            _ = x == x  -- Suppress the redundant-constraint warning for (Eq a)

    Here the call to ``(==)`` makes GHC think that the ``(Eq a)``
    constraint is needed, so no warning is issued.

432 433
.. ghc-flag:: -Wduplicate-exports

434 435 436 437 438 439 440 441 442 443 444
    .. index::
       single: duplicate exports, warning
       single: export lists, duplicates

    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.

445 446
.. ghc-flag:: -Whi-shadowing

447 448 449 450 451 452 453
    .. index::
       single: shadowing; interface files

    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.

454
.. ghc-flag:: -Widentities
455 456

    Causes the compiler to emit a warning when a Prelude numeric
457
    conversion converts a type ``T`` to the same type ``T``; such calls are
458 459 460
    probably no-ops and can be omitted. The functions checked for are:
    ``toInteger``, ``toRational``, ``fromIntegral``, and ``realToFrac``.

461 462
.. ghc-flag:: -Wimplicit-prelude

463 464 465 466 467 468
    .. index::
       single: implicit prelude, warning

    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
469
    disabled (either by :ghc-flag:`-XNoImplicitPrelude` or a
470 471 472
    ``LANGUAGE NoImplicitPrelude`` pragma).

    Note that no warning is given for syntax that implicitly refers to
473
    the Prelude, even if :ghc-flag:`-XNoImplicitPrelude` would change whether it
474 475 476 477 478 479 480
    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.

481 482 483
.. ghc-flag:: -Wincomplete-patterns
              -Wincomplete-uni-patterns

484 485 486 487
    .. index::
       single: incomplete patterns, warning
       single: patterns, incomplete

488
    The option :ghc-flag:`-Wincomplete-patterns` warns about places where a
489 490
    pattern-match might fail at runtime. The function ``g`` below will
    fail when applied to non-empty lists, so the compiler will emit a
491
    warning about this when :ghc-flag:`-Wincomplete-patterns` is enabled. ::
492 493 494 495 496 497

        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
498
    functions, and it is switched on by :ghc-flag:`-W`.
499

500
    The flag :ghc-flag:`-Wincomplete-uni-patterns` is similar, except that
501
    it applies only to lambda-expressions and pattern bindings,
502
    constructs that only allow a single pattern: ::
503 504 505 506

        h = \[] -> 2
        Just k = f y

507 508
.. ghc-flag:: -Wincomplete-record-updates

509 510 511 512 513 514
    .. index::
       single: incomplete record updates, warning
       single: record updates, incomplete

    The function ``f`` below will fail when applied to ``Bar``, so the
    compiler will emit a warning about this when
515
    :ghc-flag:`-Wincomplete-record-updates` is enabled. ::
516 517 518 519 520 521 522 523 524 525

        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.

526 527
.. ghc-flag:: -Wmissing-fields

528 529 530 531 532 533 534 535 536 537
    .. index::
       single: missing fields, warning
       single: fields, missing

    This option is on by default, and warns you whenever the
    construction of a labelled field constructor isn't complete, missing
    initialisers 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.

538 539
.. ghc-flag:: -Wmissing-import-lists

540 541 542 543 544 545
    .. index::
       single: missing import lists, warning
       single: import lists, missing

    This flag warns if you use an unqualified ``import`` declaration
    that does not explicitly list the entities brought into scope. For
546
    example ::
547 548 549 550 551 552 553

        module M where
          import X( f )
          import Y
          import qualified Z
          p x = f x x

554 555 556 557 558 559 560
    The :ghc-flag:`-Wmissing-import-lists` flag will warn about the import of
    ``Y`` but not ``X`` If module ``Y`` is later changed to export (say) ``f``,
    then the reference to ``f`` in ``M`` will become ambiguous. No warning is
    produced for the import of ``Z`` because extending ``Z``\'s exports would be
    unlikely to produce ambiguity in ``M``.

.. ghc-flag:: -Wmissing-methods
561 562 563 564 565 566 567 568 569 570

    .. index::
       single: missing methods, warning
       single: methods, missing

    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
571
    underscore. Here's an example where this is useful: ::
572 573 574 575 576 577 578 579 580 581 582 583 584 585

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

    The ``MINIMAL`` pragma can be used to change which combination of
    methods will be required for instances of a particular class. See
    :ref:`minimal-pragma`.

586 587
.. ghc-flag:: -Wmissing-signatures

588 589 590 591
    .. index::
       single: type signatures, missing

    If you would like GHC to check that every top-level function/value
592
    has a type signature, use the :ghc-flag:`-Wmissing-signatures` option.
593 594 595
    As part of the warning GHC also reports the inferred type. The
    option is off by default.

596 597
.. ghc-flag:: -Wmissing-exported-sigs

598 599 600 601 602
    .. index::
       single: type signatures, missing

    If you would like GHC to check that every exported top-level
    function/value has a type signature, but not check unexported
603 604
    values, use the :ghc-flag:`-Wmissing-exported-sigs` option. This option
    takes precedence over :ghc-flag:`-Wmissing-signatures`. As part of the
605 606 607
    warning GHC also reports the inferred type. The option is off by
    default.

608 609
.. ghc-flag:: -Wmissing-local-sigs

610 611 612
    .. index::
       single: type signatures, missing

613
    If you use the :ghc-flag:`-Wmissing-local-sigs` flag GHC will warn you
614 615 616
    about any polymorphic local bindings. As part of the warning GHC
    also reports the inferred type. The option is off by default.

617
.. ghc-flag:: -Wmissing-pat-syn-signatures
618 619 620 621 622

    .. index::
         single: type signatures, missing, pattern synonyms

    If you would like GHC to check that every pattern synonym has a type
623
    signature, use the :ghc-flag:`-Wmissing-pat-syn-signatures` option. If this option is
624 625 626
    used in conjunction with :ghc-flag:`-Wmissing-exported-sigs` then only
    exported pattern synonyms must have a type signature. GHC also reports the
    inferred type. This option is off by default.
627

628
.. ghc-flag:: -Wname-shadowing
629

630 631 632 633 634 635 636 637 638 639 640
    .. index::
       single: shadowing, warning

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

    The warning is suppressed for names beginning with an underscore.
641
    For example ::
642 643 644

        f x = do { _ignore <- this; _ignore <- that; return (the other) }

645 646
.. ghc-flag:: -Worphans

647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
    .. index::
       single: orphan instances, warning
       single: orphan rules, warning

    These flags cause 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. See :ref:`orphan-modules` for details.

664
    The flag :ghc-flag:`-Worphans` warns about user-written orphan rules or
665
    instances.
666

667 668
.. ghc-flag:: -Woverlapping-patterns

669 670 671 672 673
    .. index::
       single: overlapping patterns, warning
       single: patterns, overlapping

    By default, the compiler will warn you if a set of patterns are
674
    overlapping, e.g., ::
675 676 677 678 679 680 681 682 683 684

        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.

685 686
.. ghc-flag:: -Wtabs

687 688 689 690 691
    .. index::
       single: tabs, warning

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

692 693
.. ghc-flag:: -Wtype-defaults

694 695 696 697 698 699 700 701 702 703 704 705 706 707
    .. index::
       single: defaulting mechanism, warning

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

708 709
.. ghc-flag:: -Wmonomorphism-restriction

710 711 712 713 714 715 716 717 718 719
    .. index::
       single: monomorphism restriction, warning

    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.

720 721 722 723 724 725
.. ghc-flag:: -Wunsupported-llvm-version

    Warn when using :ghc-flag:`-fllvm` with an unsupported version of LLVM.

.. ghc-flag:: -Wunticked-promoted-constructors

726 727 728 729 730 731
    .. index::
       single: promoted constructor, warning

    Warn if a promoted data constructor is used without a tick preceding
    its name.

732
    For example: ::
733 734 735 736 737 738 739 740 741 742

        data Nat = Succ Nat | Zero

        data Vec n s where
          Nil  :: Vec Zero a
          Cons :: a -> Vec n a -> Vec (Succ n) a

    Will raise two warnings because ``Zero`` and ``Succ`` are not
    written as ``'Zero`` and ``'Succ``.

743 744 745
    This warning is is enabled by default in :ghc-flag:`-Wall` mode.

.. ghc-flag:: -Wunused-binds
746 747 748 749 750 751 752 753

    .. index::
       single: unused binds, warning
       single: binds, unused

    Report any function definitions (and local bindings) which are
    unused. An alias for

754 755 756 757 758
    -  :ghc-flag:`-Wunused-top-binds`
    -  :ghc-flag:`-Wunused-local-binds`
    -  :ghc-flag:`-Wunused-pattern-binds`

.. ghc-flag:: -Wunused-top-binds
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777

    .. index::
       single: unused binds, warning
       single: binds, unused

    Report any function definitions which are unused.

    More precisely, warn if a binding brings into scope a variable that
    is not used, except if the variable's name starts with an
    underscore. The "starts-with-underscore" condition provides a way to
    selectively disable the warning.

    A variable is regarded as "used" if

    -  It is exported, or

    -  It appears in the right hand side of a binding that binds at
       least one used variable that is used

778
    For example: ::
779 780 781 782 783 784 785 786 787

        module A (f) where
        f = let (p,q) = rhs1 in t p  -- No warning: q is unused, but is locally bound
        t = rhs3                     -- No warning: f is used, and hence so is t
        g = h x                      -- Warning: g unused
        h = rhs2                     -- Warning: h is only used in the
                                     -- right-hand side of another unused binding
        _w = True                    -- No warning: _w starts with an underscore

788 789
.. ghc-flag:: -Wunused-local-binds

790 791 792 793
    .. index::
       single: unused binds, warning
       single: binds, unused

794
    Report any local definitions which are unused. For example: ::
795 796 797 798 799

        module A (f) where
        f = let (p,q) = rhs1 in t p  -- Warning: q is unused
        g = h x                      -- No warning: g is unused, but is a top-level binding

800 801
.. ghc-flag:: -Wunused-pattern-binds

802 803 804 805 806
    .. index::
       single: unused binds, warning
       single: binds, unused

    Warn if a pattern binding binds no variables at all, unless it is a
807
    lone, possibly-banged, wild-card pattern. For example: ::
808 809 810 811 812 813 814 815 816 817 818 819

        Just _ = rhs3    -- Warning: unused pattern binding
        (_, _) = rhs4    -- Warning: unused pattern binding
        _  = rhs3        -- No warning: lone wild-card pattern
        !_ = rhs4        -- No warning: banged wild-card pattern; behaves like seq

    The motivation for allowing lone wild-card patterns is they are not
    very different from ``_v = rhs3``, which elicits no warning; and
    they can be useful to add a type constraint, e.g. ``_ = x::Int``. A
    lone banged wild-card pattern is useful as an alternative (to
    ``seq``) way to force evaluation.

820 821
.. ghc-flag:: -Wunused-imports

822 823 824 825 826 827 828 829 830
    .. index::
       single: unused imports, warning
       single: imports, unused

    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.

831 832
.. ghc-flag:: -Wunused-matches

833 834 835 836
    .. index::
       single: unused matches, warning
       single: matches, unused

837 838
    Report all unused variables which arise from term-level pattern matches,
    including patterns consisting of a single variable. For instance
839
    ``f x y = []`` would report ``x`` and ``y`` as unused. The warning
840
    is suppressed if the variable name begins with an underscore, thus: ::
841 842 843

        f _x = True

844 845 846 847 848
    Note that :ghc-flag:`-Wunused-matches` does not warn about variables which
    arise from type-level patterns, as found in type family and data family
    instances. This must be enabled separately through the
    :ghc-flag:`-Wunused-type-patterns` flag.

849 850
.. ghc-flag:: -Wunused-do-bind

851 852 853 854 855 856 857 858 859 860 861
    .. index::
       single: unused do binding, warning
       single: do binding, unused

    Report expressions occurring in ``do`` and ``mdo`` blocks that
    appear to silently throw information away. For instance
    ``do { mapM popInt xs ; return 10 }`` would report the first
    statement in the ``do`` block as suspicious, as it has the type
    ``StackM [Int]`` and not ``StackM ()``, but that ``[Int]`` value is
    not bound to anything. The warning is suppressed by explicitly
    mentioning in the source code that your program is throwing
862
    something away: ::
863 864 865

        do { _ <- mapM popInt xs ; return 10 }

866
    Of course, in this particular situation you can do even better: ::
867 868 869

        do { mapM_ popInt xs ; return 10 }

870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
.. ghc-flag:: -Wunused-type-patterns

    .. index::
       single: unused type patterns, warning
       single: type patterns, unused

    Report all unused type variables which arise from patterns in type family
    and data family instances. For instance: ::

        type instance F x y = []

    would report ``x`` and ``y`` as unused. The warning is suppressed if the
    type variable name begins with an underscore, like so: ::

        type instance F _x _y = []

    Unlike :ghc-flag:`-Wunused-matches`, :ghc-flag:`-Wunused-type-variables` is
    not implied by :ghc-flag:`-Wall`. The rationale for this decision is that
    unlike term-level pattern names, type names are often chosen expressly for
    documentation purposes, so using underscores in type names can make the
    documentation harder to read.

.. ghc-flag:: -Wunused-foralls

    .. index::
       single: unused foralls, warning
       single: foralls, unused

    Report all unused type variables which arise from explicit, user-written
    ``forall`` statements. For instance: ::

        g :: forall a b c. (b -> b)

    would report ``a`` and ``c`` as unused.

905 906
.. ghc-flag:: -Wwrong-do-bind

907 908 909 910 911 912 913 914 915 916 917 918
    .. index::
       single: apparently erroneous do binding, warning
       single: do binding, apparently erroneous

    Report expressions occurring in ``do`` and ``mdo`` blocks that
    appear to lack a binding. For instance
    ``do { return (popInt 10) ; return 10 }`` would report the first
    statement in the ``do`` block as suspicious, as it has the type
    ``StackM (StackM Int)`` (which consists of two nested applications
    of the same monad constructor), but which is not then "unpacked" by
    binding the result. The warning is suppressed by explicitly
    mentioning in the source code that your program is throwing
919
    something away: ::
920 921 922 923

        do { _ <- return (popInt 10) ; return 10 }

    For almost all sensible programs this will indicate a bug, and you
924
    probably intended to write: ::
925 926 927

        do { popInt 10 ; return 10 }

928
.. ghc-flag:: -Winline-rule-shadowing
929 930 931 932 933

    Warn if a rewrite RULE might fail to fire because the function might
    be inlined before the rule has a chance to fire. See
    :ref:`rules-inline`.

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