hlint-1.9.20: Source code suggestions

Safe HaskellNone




WARNING: This module represents the evolving second version of the HLint API. It will be renamed to drop the "3" in the next major version.

This module provides a way to apply HLint hints. As an example of approximating the hlint experience:

(flags, classify, hint) <- autoSettings
Right m <- parseModuleEx flags "MyFile.hs" Nothing
print $ applyHints classify hint [m]



applyHints :: [Classify] -> Hint -> [(Module SrcSpanInfo, [Comment])] -> [Idea] Source

Given a way of classifying results, and a Hint, apply to a set of modules generating a list of Ideas. The Idea values will be ordered within a file.

Idea data type

data Idea Source

An idea suggest by a Hint.




ideaModule :: String

The module the idea applies to, may be "" if the module cannot be determined or is a result of cross-module hints.

ideaDecl :: String

The declaration the idea applies to, typically the function name, but may be a type name.

ideaSeverity :: Severity

The severity of the idea, e.g. Warning.

ideaHint :: String

The name of the hint that generated the idea, e.g. "Use reverse".

ideaSpan :: SrcSpan

The source code the idea relates to.

ideaFrom :: String

The contents of the source code the idea relates to.

ideaTo :: Maybe String

The suggested replacement, or Nothing for no replacement (e.g. on parse errors).

ideaNote :: [Note]

Notes about the effect of applying the replacement.


data Severity Source

How severe an issue is.



The issue has been explicitly ignored and will usually be hidden (pass --show on the command line to see ignored ideas).


Warnings are things that some people may consider improvements, but some may not.


Errors are suggestions that are nearly always a good idea to apply.

data Note Source

A note describing the impact of the replacement.



The replacement is increases laziness, for example replacing reverse (reverse x) with x makes the code lazier.


The replacement is decreases laziness, for example replacing (fst x, snd x) with x makes the code stricter.

RemovesError String

The replacement removes errors, for example replacing foldr1 (+) with sum removes an error on [], and might contain the text "on []".

ValidInstance String String

The replacement assumes standard type class lemmas, a hint with the note ValidInstance "Eq" "x" might only be valid if the x variable has a reflexive Eq instance.

Note String

An arbitrary note.



data Classify Source

How to classify an Idea. If any matching field is "" then it matches everything.




classifySeverity :: Severity

Severity to set the Idea to.

classifyHint :: String

Match on Idea field ideaHint.

classifyModule :: String

Match on Idea field ideaModule.

classifyDecl :: String

Match on Idea field ideaDecl.


getHLintDataDir :: IO FilePath Source

Get the Cabal configured data directory of HLint.

autoSettings :: IO (ParseFlags, [Classify], Hint) Source

The function produces a tuple containg ParseFlags (for parseModuleEx), and Classify and Hint for applyHints. It approximates the normal HLint configuration steps, roughly:

  1. Use findSettings with readSettingsFile to find and load the HLint settings files.
  2. Use parseFlagsAddFixities and resolveHints to transform the outputs of findSettings.

If you want to do anything custom (e.g. using a different data directory, storing intermediate outputs, loading hints from a database) you are expected to copy and paste this function, then change it to your needs.

findSettings :: (String -> IO (FilePath, Maybe String)) -> Maybe String -> IO ([Fixity], [Classify], [Either HintBuiltin HintRule]) Source

Given a function to load a module (typically readSettingsFile), and a module to start from (defaults to HLint.HLint) find the information from all settings files.

readSettingsFile :: Maybe FilePath -> String -> IO (FilePath, Maybe String) Source

Given a directory (or Nothing to imply getHLintDataDir), and a mdoule name (e.g. HLint.Default), find the settings file associated with it, returning the name of the file, and (optionally) the contents.

This function looks for all settings files starting with HLint. in the directory argument, and all other files relative to the current directory.


data HintRule Source

A LHS ==> RHS style hint rule.




hintRuleSeverity :: Severity

Default severity for the hint.

hintRuleName :: String

Name for the hint.

hintRuleScope :: Scope

Module scope in which the hint operates.

hintRuleLHS :: Exp SrcSpanInfo


hintRuleRHS :: Exp SrcSpanInfo


hintRuleSide :: Maybe (Exp SrcSpanInfo)

Side condition, typically specified with where _ = ....

hintRuleNotes :: [Note]

Notes about application of the hint.


data Hint Source

Functions to generate hints, combined using the Monoid instance.




hintModules :: [(Scope, Module SrcSpanInfo)] -> [Idea]

Given a list of modules (and their scope information) generate some Ideas.

hintModule :: Scope -> Module SrcSpanInfo -> [Idea]

Given a single module and its scope information generate some Ideas.

hintDecl :: Scope -> Module SrcSpanInfo -> Decl SrcSpanInfo -> [Idea]

Given a declaration (with a module and scope) generate some Ideas. This function will be partially applied with one module/scope, then used on multiple Decl values.

hintComment :: Comment -> [Idea]

Given a comment generate some Ideas.


resolveHints :: [Either HintBuiltin HintRule] -> Hint Source

Transform a list of HintRule into a Hint.


data Scope Source

Data type representing the modules in scope within a module. Created with scopeCreate and queried with scopeMatch and scopeMove. Note that the mempty Scope is not equivalent to scopeCreate on an empty module, due to the implicit import of Prelude.


scopeCreate :: Module SrcSpanInfo -> Scope Source

Create a Scope value from a module, based on the modules imports.

scopeMatch :: (Scope, QName SrcSpanInfo) -> (Scope, QName SrcSpanInfo) -> Bool Source

Given a two names in scopes, could they possibly refer to the same thing. This property is reflexive.

scopeMove :: (Scope, QName SrcSpanInfo) -> Scope -> QName SrcSpanInfo Source

Given a name in a scope, and a new scope, create a name for the new scope that will refer to the same thing. If the resulting name is ambiguous, it picks a plausible candidate.


parseModuleEx :: ParseFlags -> FilePath -> Maybe String -> IO (Either ParseError (Module SrcSpanInfo, [Comment])) Source

Parse a Haskell module. Applies the C pre processor, and uses best-guess fixity resolution if there are ambiguities. The filename - is treated as stdin. Requires some flags (often defaultParseFlags), the filename, and optionally the contents of that file.

data ParseError Source

A parse error from parseModuleEx.




parseErrorLocation :: SrcLoc

Location of the error.

parseErrorMessage :: String

Message about the cause of the error.

parseErrorContents :: String

Snippet of several lines (typically 5) including a > character pointing at the faulty line.

data ParseFlags Source

Created with defaultParseFlags, used by parseModuleEx.




encoding :: TextEncoding

How the file is read in (defaults to utf8).

cppFlags :: CppFlags

How the file is preprocessed (defaults to NoCpp).

hseFlags :: ParseMode

How the file is parsed (defaults to all fixities in the base package and most non-conflicting extensions).

data CppFlags Source

What C pre processor should be used.



No pre processing is done.


Lines prefixed with # are stripped.

Cpphs CpphsOptions

The cpphs library is used.