Skip to content

Trivial exhaustiveness checking for view patterns and guards

As discussed in #15753 (closed), ee should have better exhaustiveness checking for view patterns and guards. For example, I would expect the following program not to generate any warnings:

{-# OPTIONS_GHC -Wincomplete-patterns -fforce-recomp #-}
{-# LANGUAGE ViewPatterns #-}

module Lib where

safeLast :: [a] -> Maybe a
safeLast xs
  | []    <- reverse xs = Nothing
  | (x:_) <- reverse xs = Just x

safeLast2 :: [a] -> Maybe a
safeLast2 (reverse -> [])    = Nothing
safeLast2 (reverse -> (x:_)) = Just x

safeLast3 :: [a] -> Maybe a
safeLast3 xs
  | []    <- reverse xs = Nothing
safeLast3 xs'
  | (x:_) <- reverse xs' = Just x
Edited by Sebastian Graf
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information