hlint-3.10: Source code suggestions

Safe Haskell | None
Language | Haskell2010

Language.Haskell.HLint

Contents

- Generate hints
- Idea data type
- Settings
- Hints
- Modules
- Parse flags

Description

This module provides a way to apply HLint hints. If you want to just run `hlint` in-process and collect the results see `hlint`.

If you want to approximate the `hlint` experience with a more structured API try:

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

Synopsis

- hlint :: [String] -> IO [Idea]
- applyHints :: [Classify] -> Hint -> [ModuleEx] -> [Idea]
- data Idea = Idea {
  - ideaModule :: [String]
  - ideaDecl :: [String]
  - ideaSeverity :: Severity
  - ideaHint :: String
  - ideaSpan :: SrcSpan
  - ideaFrom :: String
  - ideaTo :: Maybe String
  - ideaNote :: [Note]
  - ideaRefactoring :: [Refactoring SrcSpan]
  }
- data Severity
  - = Ignore
  - | Suggestion
  - | Warning
  - | Error
- data Note
  - = IncreasesLaziness
  - | DecreasesLaziness
  - | RemovesError String
  - | ValidInstance String String
  - | RequiresExtension String
  - | Note String
- unpackSrcSpan :: SrcSpan -> Maybe (FilePath, (Int, Int), (Int, Int))
- showIdeaANSI :: Idea -> String
- data Classify = Classify {
  - classifySeverity :: Severity
  - classifyHint :: String
  - classifyModule :: String
  - classifyDecl :: String
  }
- getHLintDataDir :: IO FilePath
- autoSettings :: IO (ParseFlags, [Classify], Hint)
- argsSettings :: [String] -> IO (ParseFlags, [Classify], Hint)
- findSettings :: (String -> IO (FilePath, Maybe String)) -> Maybe String -> IO ([FixityInfo], [Classify], Hint)
- readSettingsFile :: Maybe FilePath -> String -> IO (FilePath, Maybe String)
- data Hint
- data ModuleEx
- parseModuleEx :: ParseFlags -> FilePath -> Maybe String -> IO (Either ParseError ModuleEx)
- createModuleEx :: Located (HsModule GhcPs) -> ModuleEx
- createModuleExWithFixities :: [(String, Fixity)] -> Located (HsModule GhcPs) -> ModuleEx
- data ParseError = ParseError {
  - parseErrorLocation :: SrcSpan
  - parseErrorMessage :: String
  - parseErrorContents :: String
  }
- defaultParseFlags :: ParseFlags
- data ParseFlags = ParseFlags {
  - cppFlags :: CppFlags
  - baseLanguage :: Maybe Language
  - enabledExtensions :: [Extension]
  - disabledExtensions :: [Extension]
  - fixities :: [FixityInfo]
  }
- data CppFlags
  - = CppSimple
  - | Cpphs CpphsOptions
- type FixityInfo = (String, Associativity, Int)
- parseFlagsAddFixities :: [FixityInfo] -> ParseFlags -> ParseFlags

# Generate hints

hlint :: [String] -> IO [Idea] Source #

This function takes a list of command line arguments, and returns the given hints. To see a list of arguments type `hlint --help` at the console. This function writes to the stdout/stderr streams, unless `--quiet` is specified.

As an example:

    do hints <- hlint ["src", "--ignore=Use map","--quiet"]
       when (length hints > 3) $ error "Too many hints!"

Warning: The flags provided by HLint are relatively stable, but do not have the same API stability guarantees as the rest of the strongly-typed API. Do not run this function on your server with untrusted input.

applyHints :: [Classify] -> Hint -> [ModuleEx] -> [Idea] Source #

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

Given a set of modules, it may be faster to pass each to `applyHints` in a singleton list. When given multiple modules at once this function attempts to find hints between modules, which is slower and often pointless (by default HLint passes modules singularly, using `--cross` to pass all modules together).

# Idea data type

data Idea Source #

An idea suggest by a `Hint`.

Constructors

Idea |

Fields

- ideaModule :: [String]

  The modules the idea is for, usually a singleton.

- ideaDecl :: [String]

  The declarations the idea is for, usually a singleton, 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.

- ideaRefactoring :: [Refactoring SrcSpan]

  How to perform this idea

#### Instances

Instances details

Show Idea Source # |
Instance details Defined in Idea Methods showsPrec :: Int -> Idea -> ShowS Source # show :: Idea -> String Source # showList :: [Idea] -> ShowS Source #
Eq Idea Source # |
Instance details Defined in Idea Methods (==) :: Idea -> Idea -> Bool Source # (/=) :: Idea -> Idea -> Bool Source #

data Severity Source #

How severe an issue is.

Constructors

Ignore | The issue has been explicitly ignored and will usually be hidden (pass `--show` on the command line to see ignored ideas).
Suggestion | Suggestions are things that some people may consider improvements, but some may not.
Warning | Warnings are suggestions that are nearly always a good idea to apply.
Error | Available as a setting for the user. Only parse errors have this setting by default.

#### Instances

Instances details

ToJSON Severity Source # |
Instance details Defined in Config.Type Methods toJSON :: Severity -> Value Source # toEncoding :: Severity -> Encoding Source # toJSONList :: [Severity] -> Value Source # toEncodingList :: [Severity] -> Encoding Source # omitField :: Severity -> Bool Source #
Data Severity Source # |

Instance details

Defined in Config.Type

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Severity -> c Severity Source #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Severity Source #

toConstr :: Severity -> Constr Source #

dataTypeOf :: Severity -> DataType Source #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Severity) Source #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Severity) Source #

gmapT :: (forall b. Data b => b -> b) -> Severity -> Severity Source #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Severity -> r Source #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Severity -> r Source #

gmapQ :: (forall d. Data d => d -> u) -> Severity -> [u] Source #

gmapQi :: Int -> (forall d. Data d => d -> u) -> Severity -> u Source #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> Severity -> m Severity Source #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Severity -> m Severity Source #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Severity -> m Severity Source #

Bounded Severity Source # |
Instance details Defined in Config.Type Methods minBound :: Severity Source # maxBound :: Severity Source #
Enum Severity Source # |

Instance details

Defined in Config.Type

Methods

succ :: Severity -> Severity Source #

pred :: Severity -> Severity Source #

toEnum :: Int -> Severity Source #

fromEnum :: Severity -> Int Source #

enumFrom :: Severity -> [Severity] Source #

enumFromThen :: Severity -> Severity -> [Severity] Source #

enumFromTo :: Severity -> Severity -> [Severity] Source #

enumFromThenTo :: Severity -> Severity -> Severity -> [Severity] Source #

Generic Severity Source # |

Instance details

Defined in Config.Type

Associated Types

type Rep Severity |

Instance details

Defined in Config.Type

type Rep Severity = D1 ('MetaData "Severity" "Config.Type" "hlint-3.10-G0lkJZHpA3nC7IEUTACl0R" 'False) ((C1 ('MetaCons "Ignore" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Suggestion" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "Warning" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Error" 'PrefixI 'False) (U1 :: Type -> Type)))

Methods

from :: Severity -> Rep Severity x Source #

to :: Rep Severity x -> Severity Source #

Read Severity Source # |
Instance details Defined in Config.Type Methods readsPrec :: Int -> ReadS Severity Source # readList :: ReadS [Severity] Source # readPrec :: ReadPrec Severity Source # readListPrec :: ReadPrec [Severity] Source #
Show Severity Source # |
Instance details Defined in Config.Type Methods showsPrec :: Int -> Severity -> ShowS Source # show :: Severity -> String Source # showList :: [Severity] -> ShowS Source #
Eq Severity Source # |
Instance details Defined in Config.Type Methods (==) :: Severity -> Severity -> Bool Source # (/=) :: Severity -> Severity -> Bool Source #
Ord Severity Source # |
Instance details Defined in Config.Type Methods compare :: Severity -> Severity -> Ordering Source # (<) :: Severity -> Severity -> Bool Source # (<=) :: Severity -> Severity -> Bool Source # (>) :: Severity -> Severity -> Bool Source # (>=) :: Severity -> Severity -> Bool Source # max :: Severity -> Severity -> Severity Source # min :: Severity -> Severity -> Severity Source #
type Rep Severity Source # |

Instance details

Defined in Config.Type

type Rep Severity = D1 ('MetaData "Severity" "Config.Type" "hlint-3.10-G0lkJZHpA3nC7IEUTACl0R" 'False) ((C1 ('MetaCons "Ignore" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Suggestion" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "Warning" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Error" 'PrefixI 'False) (U1 :: Type -> Type)))

data Note Source #

A note describing the impact of the replacement.

Constructors

IncreasesLaziness | The replacement is increases laziness, for example replacing `reverse (reverse x)` with `x` makes the code lazier.
DecreasesLaziness | 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.
RequiresExtension String | The replacement requires this extension to be available.
Note String | An arbitrary note.

#### Instances

Instances details

Show Note Source # |
Instance details Defined in Config.Type Methods showsPrec :: Int -> Note -> ShowS Source # show :: Note -> String Source # showList :: [Note] -> ShowS Source #
Eq Note Source # |
Instance details Defined in Config.Type Methods (==) :: Note -> Note -> Bool Source # (/=) :: Note -> Note -> Bool Source #
Ord Note Source # |
Instance details Defined in Config.Type Methods compare :: Note -> Note -> Ordering Source # (<) :: Note -> Note -> Bool Source # (<=) :: Note -> Note -> Bool Source # (>) :: Note -> Note -> Bool Source # (>=) :: Note -> Note -> Bool Source # max :: Note -> Note -> Note Source # min :: Note -> Note -> Note Source #

unpackSrcSpan :: SrcSpan -> Maybe (FilePath, (Int, Int), (Int, Int)) Source #

Unpack a `SrcSpan` value. Useful to allow using the `Idea` information without adding a dependency on `ghc` or `ghc-lib-parser`. Unpacking gives:

    (filename, (startLine, startCol), (endLine, endCol))

Following the GHC API, end column is the column after the end of the error. Lines and columns are 1-based. Returns `Nothing` if there is no helpful location information.

showIdeaANSI :: Idea -> String Source #

Show an `Idea` with ANSI color codes to give syntax coloring to the Haskell code.

# Settings

data Classify Source #

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

Constructors

Classify |

Fields

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

#### Instances

Instances details

Show Classify Source # |
Instance details Defined in Config.Type Methods showsPrec :: Int -> Classify -> ShowS Source # show :: Classify -> String Source # showList :: [Classify] -> ShowS Source #

getHLintDataDir :: IO FilePath Source #

Get the Cabal configured data directory of HLint.

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

The function produces a tuple containing `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.

argsSettings :: [String] -> IO (ParseFlags, [Classify], Hint) Source #

A version of `autoSettings` which respects some of the arguments supported by HLint. If arguments unrecognised by HLint are used it will result in an error. Arguments which have no representation in the return type are silently ignored.

findSettings :: (String -> IO (FilePath, Maybe String)) -> Maybe String -> IO ([FixityInfo], [Classify], Hint) Source #

Given a function to load a module (typically `readSettingsFile`), and a module to start from (defaults to `hlint.yaml`) 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 module 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.

# Hints

data Hint Source #

Functions to generate hints, combined using the `Monoid` instance.

#### Instances

Instances details

Monoid Hint Source # |
Instance details Defined in Hint.Type Methods mempty :: Hint Source # mappend :: Hint -> Hint -> Hint Source # mconcat :: [Hint] -> Hint Source #
Semigroup Hint Source # |
Instance details Defined in Hint.Type Methods (<>) :: Hint -> Hint -> Hint Source # sconcat :: NonEmpty Hint -> Hint Source # stimes :: Integral b => b -> Hint -> Hint Source #

# Modules

data ModuleEx Source #

Result of `parseModuleEx`, representing a parsed module.

parseModuleEx :: ParseFlags -> FilePath -> Maybe String -> IO (Either ParseError ModuleEx) 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.

Note that certain programs, e.g. `main = do` successfully parse with GHC, but then fail with an error in the renamer. These programs will return a successful parse.

createModuleEx :: Located (HsModule GhcPs) -> ModuleEx Source #

Create a `ModuleEx` from a GHC module. It is assumed the incoming parsed module has not been adjusted to account for operator fixities (it uses the HLint default fixities).

createModuleExWithFixities :: [(String, Fixity)] -> Located (HsModule GhcPs) -> ModuleEx Source #

data ParseError Source #

A parse error.

Constructors

ParseError |

Fields

- parseErrorLocation :: SrcSpan

  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.

# Parse flags

defaultParseFlags :: ParseFlags Source #

Default value for `ParseFlags`.

data ParseFlags Source #

Created with `defaultParseFlags`, used by `parseModuleEx`.

Constructors

ParseFlags |

Fields

- cppFlags :: CppFlags

  How the file is preprocessed (defaults to `NoCpp`).

- baseLanguage :: Maybe Language

  Base language (e.g. Haskell98, Haskell2010), defaults to `Nothing`.

- enabledExtensions :: [Extension]

  List of extensions enabled for parsing, defaults to many non-conflicting extensions.

- disabledExtensions :: [Extension]

  List of extensions disabled for parsing, usually empty.

- fixities :: [FixityInfo]

  List of fixities to be aware of, defaults to those defined in `base`.

data CppFlags Source #

What C pre processor should be used.

Constructors

CppSimple | Lines prefixed with `#` are stripped.
Cpphs CpphsOptions | The `cpphs` library is used.

type FixityInfo = (String, Associativity, Int) Source #

A Fixity definition, comprising the name the fixity applies to, the direction and the precedence. As an example, a source file containing:

    infixr 3 `foo`

would create `("foo", RightAssociative, 3)`.

parseFlagsAddFixities :: [FixityInfo] -> ParseFlags -> ParseFlags Source #

Given some fixities, add them to the existing fixities in `ParseFlags`.