module Language.Haskell.Inspector.Smell (
hasRedundantBooleanComparison,
hasRedundantIf,
hasRedundantGuards,
hasRedundantLambda,
hasRedundantParameter) where
import Language.Haskell.Explorer
import Language.Haskell.Syntax
import Language.Haskell.Inspector
hasRedundantBooleanComparison :: Inspection
hasRedundantBooleanComparison = hasExpression f
where f (E (HsInfixApp x (HsQVarOp (UnQual (HsSymbol c))) y)) = any isBooleanLiteral [x, y] && isComp c
f _ = False
isComp c = c == "==" || c == "/="
hasRedundantIf :: Inspection
hasRedundantIf = hasExpression f
where f (E (HsIf _ x y)) = all isBooleanLiteral [x, y]
f _ = False
hasRedundantGuards :: Inspection
hasRedundantGuards = hasRhs f
where f (HsGuardedRhss [
HsGuardedRhs _ _ x,
HsGuardedRhs _ (HsVar (UnQual (HsIdent "otherwise"))) y]) = all isBooleanLiteral [x, y]
f _ = False
hasRedundantLambda :: Inspection
hasRedundantLambda = hasExpression f
where f (E (HsLambda _ [HsPVar (HsIdent x)] (HsApp _ (HsVar (UnQual (HsIdent y)))))) = x == y
f _ = False
hasRedundantParameter :: Inspection
hasRedundantParameter binding = any f . declsOf binding
where f (HsFunBind [
HsMatch _ _ params (HsUnGuardedRhs (HsApp _ (HsVar (UnQual arg)))) _ ]) | (HsPVar param) <- last params = param == arg
f _ = False
--private
isBooleanLiteral (HsCon (UnQual (HsIdent "True"))) = True
isBooleanLiteral (HsCon (UnQual (HsIdent "False"))) = True
isBooleanLiteral _ = False