1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Warning where
5import Data.Aeson ((.=))
6import qualified Data.Aeson as Json
7import qualified Text.PrettyPrint as P
8import Text.PrettyPrint ((<+>))
10import qualified AST.Module.Name as ModuleName
11import qualified AST.Type as Type
12import qualified Nitpick.Pattern as Pattern
13import qualified Reporting.Annotation as A
14import qualified Reporting.PrettyPrint as P
15import qualified Reporting.Report as Report
18-- ALL POSSIBLE WARNINGS
21 = UnusedImport ModuleName.Raw
22 | MissingTypeAnnotation String Type.Canonical
23 | InexhaustivePatternMatch [Pattern.Pattern]
24 | RedundantPatternMatch
29toString :: P.Dealiaser -> String -> String -> A.Located Warning -> String
30toString dealiaser location source (A.A region warning) =
31 Report.toString location region (toReport dealiaser warning) source
34print :: P.Dealiaser -> String -> String -> A.Located Warning -> IO ()
35print dealiaser location source (A.A region warning) =
36 Report.printWarning location region (toReport dealiaser warning) source
39toReport :: P.Dealiaser -> Warning -> Report.Report
40toReport dealiaser warning =
42 UnusedImport moduleName ->
45 ("Module `" ++ ModuleName.toString moduleName ++ "` is unused.")
48 MissingTypeAnnotation name inferredType ->
50 "missing type annotation"
51 ("Top-level value `" ++ name ++ "` does not have a type annotation.")
52 ( "The type annotation you want looks something like this:\n\n"
53 ++ P.render (P.nest 4 typeDoc)
58 (P.text name <+> P.colon)
60 (P.pretty dealiaser False inferredType)
62 InexhaustivePatternMatch unhandled ->
65 "The following case expression does not handle all possible inputs."
66 ( "The following patterns are not being handled:\n"
67 ++ viewPatternList unhandled
69 ++ "If we get values like this, we have no choice but to crash! Normally this means\n"
70 ++ "you just added a new tag to a union type, but sometimes it can be because there\n"
71 ++ "is something very odd about the data you are modeling. In those rare cases, you\n"
72 ++ "probably want to either:\n"
74 ++ " 1. Rethink the model. Maybe some cases are \"impossible\" but it is still\n"
75 ++ " possible to construct such cases. Is there a way to model the values more\n"
76 ++ " precisely such that \"impossible\" values truly are impossible?\n"
77 ++ " 2. End the pattern match with a wildcard match that leads to an expression\n"
78 ++ " like: Debug.crash \"If you are reading this, I have made a mistake!\"\n"
79 ++ " Generally speaking, you do not want it to have to be this way."
82 RedundantPatternMatch ->
85 "The following pattern is redundant."
86 "Any value with this shape will be handled by a previous pattern."
91toJson :: P.Dealiaser -> FilePath -> A.Located Warning -> Json.Value
92toJson dealiaser filePath (A.A region warning) =
94 (maybeRegion, additionalFields) =
95 Report.toJson [] (toReport dealiaser warning)
99 , "region" .= maybe region id maybeRegion
100 , "type" .= ("warning" :: String)
107viewPatternList :: [Pattern.Pattern] -> String
108viewPatternList unhandledPatterns =
110 (showPatterns, rest) =
111 splitAt 4 unhandledPatterns
113 concatMap ((++) "\n ") $
114 map Pattern.toString showPatterns
115 ++ if null rest then [] else ["..."]
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list