on master help sign in
Add back redundancy check

Also make the error message for redundant patterns a bit nicer

commit626b319244841b7699270e543b15177469213967 authorEvan Czaplicki <evancz@users.noreply.github.com> dateThu 24 Nov 2016, 01:33 (10 years ago) parentc5b225d
2 files changed+34−17
src/Nitpick/PatternMatches.hs+29−13 src/Reporting/Error/Pattern.hs+5−4
src/Nitpick/PatternMatches.hs+29−13
@@ -32,6 +32,9 @@ import qualified Reporting.Result as Result
3232
3333
3434
35+-- NITPICK PATTERN MATCHES
36+
37+
3538type Result warning a =
3639 Result.Result () warning Error.Error a
3740
@@ -49,10 +52,6 @@ patternMatches interfaces (Module.Module name _ info) =
4952 <* checkExpression arityDict (Module.program info)
5053
5154
52−
53−-- CHECK EXPRESSIONS
54−
55−
5655checkExpression :: ArityDict -> C.Expr -> Result wrn ()
5756checkExpression arityDict (A.A region expression) =
5857 let
@@ -140,15 +139,32 @@ checkExpression arityDict (A.A region expression) =
140139
141140checkPatterns :: ArityDict -> Region.Region -> Error.Origin -> [Pattern.Canonical] -> Result wrn ()
142141checkPatterns arityDict region origin patterns =
143− let
144− matrix =
145− map (\p -> [fromCanonicalPattern p]) patterns
146− in
147− if isUseful arityDict matrix [Anything] then
148− Result.throw region (Error.Incomplete origin (error "TODO"))
142+ do matrix <- checkRedundant arityDict region [] patterns
143+ if isUseful arityDict matrix [Anything]
144+ then Result.throw region (Error.Incomplete origin (error "TODO"))
145+ else Result.ok ()
146+
147+
148+checkRedundant
149+ :: ArityDict
150+ -> Region.Region
151+ -> [[Pattern]]
152+ -> [Pattern.Canonical]
153+ -> Result wrn [[Pattern]]
154+checkRedundant arityDict region checked unchecked =
155+ case unchecked of
156+ [] ->
157+ Result.ok checked
149158
150− else
151− return ()
159+ nextPattern@(A.A patRegion _) : rest ->
160+ let
161+ next =
162+ [fromCanonicalPattern nextPattern]
163+ in
164+ if isUseful arityDict checked next then
165+ checkRedundant arityDict region (next : checked) rest
166+ else
167+ Result.throw region (Error.Redundant patRegion (length checked + 1))
152168
153169
154170
@@ -163,7 +179,7 @@ checkPatterns arityDict region origin patterns =
163179
164180
165181
166−-- REDUNDANT PATTERNS
182+-- DETECT USEFUL PATTERNS
167183
168184
169185isUseful :: ArityDict -> [[Pattern]] -> [Pattern] -> Bool
src/Reporting/Error/Pattern.hs+5−4
@@ -6,13 +6,14 @@ import Text.PrettyPrint.ANSI.Leijen (text)
66import qualified Nitpick.Pattern as Pattern
77import qualified Reporting.Error.Helpers as Help
88import qualified Reporting.Render.Type as RenderType
9+import qualified Reporting.Region as Region
910import qualified Reporting.Report as Report
1011
1112
1213
1314data Error
1415 = Incomplete Origin [Pattern.Pattern]
15− | Redundant
16+ | Redundant Region.Region Int
1617
1718
1819data Origin
@@ -49,11 +50,11 @@ toReport _localizer err =
4950 "This `case` does not have branches for all possibilities."
5051 (text (unhandledError unhandled (missingBranchesMessage (length unhandled))))
5152
52− Redundant ->
53+ Redundant region index ->
5354 Report.report
5455 "REDUNDANT PATTERN"
55− Nothing
56− "The following pattern is redundant. Remove it."
56+ (Just region)
57+ ("The " ++ Help.ordinalize index ++ " pattern is redundant. Remove it.")
5758 (text "Any value with this shape will be handled by a previous pattern.")
5859
5960