Skip to content

Commit 0a1b9df

Browse files
phadej23Skidoo
authored andcommitted
Add checkUnusedFlags
(cherry picked from commit 571a9dc)
1 parent 69e496a commit 0a1b9df

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

Cabal/Distribution/PackageDescription/Check.hs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ import Distribution.Text
5858
import Distribution.Utils.Generic (isAscii)
5959
import Language.Haskell.Extension
6060

61+
import Control.Applicative (Const (..))
6162
import Control.Monad (mapM)
6263
import Data.List (group)
64+
import Data.Monoid (Endo (..))
6365
import qualified System.Directory as System
6466
( doesFileExist, doesDirectoryExist )
6567
import qualified Data.Map as Map
@@ -74,6 +76,7 @@ import System.FilePath
7476
import System.FilePath.Windows as FilePath.Windows
7577
( isValid )
7678

79+
import qualified Data.Set as Set
7780

7881
-- | Results of some kind of failed package check.
7982
--
@@ -147,6 +150,7 @@ checkPackage gpkg mpkg =
147150
++ checkPackageVersions gpkg
148151
++ checkDevelopmentOnlyFlags gpkg
149152
++ checkFlagNames gpkg
153+
++ checkUnusedFlags gpkg
150154
where
151155
pkg = fromMaybe (flattenPackageDescription gpkg) mpkg
152156

@@ -1585,7 +1589,7 @@ checkConditionals pkg =
15851589
COr c1 c2 -> condfv c1 ++ condfv c2
15861590
CAnd c1 c2 -> condfv c1 ++ condfv c2
15871591

1588-
checkFlagNames ::GenericPackageDescription -> [PackageCheck]
1592+
checkFlagNames :: GenericPackageDescription -> [PackageCheck]
15891593
checkFlagNames gpd
15901594
| null invalidFlagNames = []
15911595
| otherwise = [ PackageDistInexcusable
@@ -1606,6 +1610,36 @@ checkFlagNames gpd
16061610
-- mon ascii letter
16071611
invalidFlagName cs = any (not . isAscii) cs
16081612

1613+
checkUnusedFlags :: GenericPackageDescription -> [PackageCheck]
1614+
checkUnusedFlags gpd
1615+
| declared == used = []
1616+
| otherwise = [ PackageDistSuspicious
1617+
$ "Declared and used flag sets differ: "
1618+
++ s declared ++ " /= " ++ s used ++ ". "
1619+
]
1620+
where
1621+
s :: Set.Set FlagName -> String
1622+
s = commaSep . map unFlagName . Set.toList
1623+
1624+
declared :: Set.Set FlagName
1625+
declared = Set.fromList $ map flagName $ genPackageFlags gpd
1626+
1627+
used :: Set.Set FlagName
1628+
used = Set.fromList $ ($[]) $ appEndo $ getConst $
1629+
(traverse . traverseCondTreeV) tellFlag (condLibrary gpd) *>
1630+
(traverse . _2 . traverseCondTreeV) tellFlag (condSubLibraries gpd) *>
1631+
(traverse . _2 . traverseCondTreeV) tellFlag (condForeignLibs gpd) *>
1632+
(traverse . _2 . traverseCondTreeV) tellFlag (condExecutables gpd) *>
1633+
(traverse . _2 . traverseCondTreeV) tellFlag (condTestSuites gpd) *>
1634+
(traverse . _2 . traverseCondTreeV) tellFlag (condBenchmarks gpd)
1635+
1636+
_2 :: Functor f => (a -> f b) -> (c, a) -> f (c, b)
1637+
_2 f (c, a) = (,) c <$> f a
1638+
1639+
tellFlag :: ConfVar -> Const (Endo [FlagName]) ConfVar
1640+
tellFlag (Flag fn) = Const (Endo (fn :))
1641+
tellFlag _ = Const mempty
1642+
16091643
checkDevelopmentOnlyFlagsBuildInfo :: BuildInfo -> [PackageCheck]
16101644
checkDevelopmentOnlyFlagsBuildInfo bi =
16111645
catMaybes [

Cabal/Distribution/Types/CondTree.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ module Distribution.Types.CondTree (
1313
mapTreeConstrs,
1414
mapTreeConds,
1515
mapTreeData,
16+
traverseCondTreeV,
17+
traverseCondBranchV,
1618
extractCondition,
1719
simplifyCondTree,
1820
ignoreConditions,
@@ -102,6 +104,17 @@ mapTreeConds f = mapCondTree id id f
102104
mapTreeData :: (a -> b) -> CondTree v c a -> CondTree v c b
103105
mapTreeData f = mapCondTree f id id
104106

107+
-- | @Traversal (CondTree v c a) (CondTree w c a) v w@
108+
traverseCondTreeV :: Applicative f => (v -> f w) -> CondTree v c a -> f (CondTree w c a)
109+
traverseCondTreeV f (CondNode a c ifs) =
110+
CondNode a c <$> traverse (traverseCondBranchV f) ifs
111+
112+
-- | @Traversal (CondBranch v c a) (CondBranch w c a) v w@
113+
traverseCondBranchV :: Applicative f => (v -> f w) -> CondBranch v c a -> f (CondBranch w c a)
114+
traverseCondBranchV f (CondBranch cnd t me) = CondBranch
115+
<$> traverse f cnd
116+
<*> traverseCondTreeV f t
117+
<*> traverse (traverseCondTreeV f) me
105118

106119
-- | Extract the condition matched by the given predicate from a cond tree.
107120
--

0 commit comments

Comments
 (0)