himari 1.1.6.2 → 1.1.6.3
raw patch · 14 files changed
+1118/−93 lines, 14 filesdep +yamlPVP ok
version bump matches the API change (PVP)
Dependencies added: yaml
API changes (from Hackage documentation)
Files
- .hlint.yaml +339/−0
- CHANGELOG.md +21/−0
- himari.cabal +9/−1
- test/HlintBaseSpec.hs +28/−44
- test/HlintExtensionSpec.hs +8/−20
- test/HlintReexportSpec.hs +241/−0
- test/HlintRunner.hs +165/−0
- test/HlintSamples/ReexportExplicitImport.hs +18/−0
- test/HlintSamples/ReexportHimari.hs +168/−0
- test/HlintSamples/ReexportHimariPrelude.hs +36/−0
- test/HlintSamples/ReexportImportForms.hs +37/−0
- test/HlintSamples/ReexportSafePrelude.hs +30/−0
- test/HlintUnliftioSpec.hs +16/−28
- test/Main.hs +2/−0
.hlint.yaml view
@@ -620,15 +620,51 @@ - message: "Partial: throws on 0 or >1 elements. Consider using (^?) or ensure exactly 1 element." name: Control.Lens.Traversal.singular within: []+ - message: "Partial: throws on 0 or >1 elements. Consider using (^?) or ensure exactly 1 element."+ name: Himari.singular+ within: []+ - message: "Partial: throws on 0 or >1 elements. Consider using (^?) or ensure exactly 1 element."+ name: Himari.Prelude.singular+ within: []+ - message: "Partial: throws on 0 or >1 elements. Consider using (^?) or ensure exactly 1 element."+ name: Himari.SafePrelude.singular+ within: [] - message: "Partial: throws on 0 or >1 elements." name: Control.Lens.Traversal.unsafeSingular within: []+ - message: "Partial: throws on 0 or >1 elements."+ name: Himari.unsafeSingular+ within: []+ - message: "Partial: throws on 0 or >1 elements."+ name: Himari.Prelude.unsafeSingular+ within: []+ - message: "Partial: throws on 0 or >1 elements."+ name: Himari.SafePrelude.unsafeSingular+ within: [] - message: "Unsafe: may throw if list length changes." name: Control.Lens.Traversal.unsafePartsOf within: [] - message: "Unsafe: may throw if list length changes."+ name: Himari.unsafePartsOf+ within: []+ - message: "Unsafe: may throw if list length changes."+ name: Himari.Prelude.unsafePartsOf+ within: []+ - message: "Unsafe: may throw if list length changes."+ name: Himari.SafePrelude.unsafePartsOf+ within: []+ - message: "Unsafe: may throw if list length changes." name: Control.Lens.Traversal.iunsafePartsOf within: []+ - message: "Unsafe: may throw if list length changes."+ name: Himari.iunsafePartsOf+ within: []+ - message: "Unsafe: may throw if list length changes."+ name: Himari.Prelude.iunsafePartsOf+ within: []+ - message: "Unsafe: may throw if list length changes."+ name: Himari.SafePrelude.iunsafePartsOf+ within: [] - functions: - message: "Unsound: violates lens laws unless lenses are disjoint. Use alongside instead." name: Control.Lens.Unsound.lensProduct@@ -877,27 +913,90 @@ - message: "Partial: throws on empty list. Use headMay or headDef from Safe instead." name: Data.List.head within: []+ - message: "Partial: throws on empty list. Use headMay or headDef from Safe instead."+ name: Himari.head+ within: []+ - message: "Partial: throws on empty list. Use headMay or headDef from Safe instead."+ name: Himari.Prelude.head+ within: []+ - message: "Partial: throws on empty list. Use headMay or headDef from Safe instead."+ name: Himari.SafePrelude.head+ within: [] - message: "Partial: throws on empty list. Use tailMay, tailDef, or tailSafe from Safe instead." name: Data.List.tail within: []+ - message: "Partial: throws on empty list. Use tailMay, tailDef, or tailSafe from Safe instead."+ name: Himari.tail+ within: []+ - message: "Partial: throws on empty list. Use tailMay, tailDef, or tailSafe from Safe instead."+ name: Himari.Prelude.tail+ within: []+ - message: "Partial: throws on empty list. Use tailMay, tailDef, or tailSafe from Safe instead."+ name: Himari.SafePrelude.tail+ within: [] - message: "Partial: throws on empty list. Use initMay, initDef, or initSafe from Safe instead." name: Data.List.init within: []+ - message: "Partial: throws on empty list. Use initMay, initDef, or initSafe from Safe instead."+ name: Himari.init+ within: []+ - message: "Partial: throws on empty list. Use initMay, initDef, or initSafe from Safe instead."+ name: Himari.Prelude.init+ within: []+ - message: "Partial: throws on empty list. Use initMay, initDef, or initSafe from Safe instead."+ name: Himari.SafePrelude.init+ within: [] - message: "Partial: throws on empty list. Use lastMay or lastDef from Safe instead." name: Data.List.last within: []+ - message: "Partial: throws on empty list. Use lastMay or lastDef from Safe instead."+ name: Himari.last+ within: []+ - message: "Partial: throws on empty list. Use lastMay or lastDef from Safe instead."+ name: Himari.Prelude.last+ within: []+ - message: "Partial: throws on empty list. Use lastMay or lastDef from Safe instead."+ name: Himari.SafePrelude.last+ within: [] - message: "Partial: throws on out-of-bounds. Use atMay or atDef from Safe instead." name: "Data.List.!!" within: [] - message: "Partial: throws on empty list. Use cycleMay or cycleDef from Safe instead." name: Data.List.cycle within: []+ - message: "Partial: throws on empty list. Use cycleMay or cycleDef from Safe instead."+ name: Himari.cycle+ within: []+ - message: "Partial: throws on empty list. Use cycleMay or cycleDef from Safe instead."+ name: Himari.Prelude.cycle+ within: []+ - message: "Partial: throws on empty list. Use cycleMay or cycleDef from Safe instead."+ name: Himari.SafePrelude.cycle+ within: [] - message: "Partial: throws on empty list. Use scanl1May or scanl1Def from Safe instead." name: Data.List.scanl1 within: []+ - message: "Partial: throws on empty list. Use scanl1May or scanl1Def from Safe instead."+ name: Himari.scanl1+ within: []+ - message: "Partial: throws on empty list. Use scanl1May or scanl1Def from Safe instead."+ name: Himari.Prelude.scanl1+ within: []+ - message: "Partial: throws on empty list. Use scanl1May or scanl1Def from Safe instead."+ name: Himari.SafePrelude.scanl1+ within: [] - message: "Partial: throws on empty list. Use scanr1May or scanr1Def from Safe instead." name: Data.List.scanr1 within: []+ - message: "Partial: throws on empty list. Use scanr1May or scanr1Def from Safe instead."+ name: Himari.scanr1+ within: []+ - message: "Partial: throws on empty list. Use scanr1May or scanr1Def from Safe instead."+ name: Himari.Prelude.scanr1+ within: []+ - message: "Partial: throws on empty list. Use scanr1May or scanr1Def from Safe instead."+ name: Himari.SafePrelude.scanr1+ within: [] - message: "Partial: throws on empty list. Use foldl1May from Safe instead." name: Data.List.foldl1 within: []@@ -923,6 +1022,15 @@ - message: "Partial: throws on Nothing. Use fromMaybe or pattern match instead." name: Data.Maybe.fromJust within: []+ - message: "Partial: throws on Nothing. Use fromMaybe or pattern match instead."+ name: Himari.fromJust+ within: []+ - message: "Partial: throws on Nothing. Use fromMaybe or pattern match instead."+ name: Himari.Prelude.fromJust+ within: []+ - message: "Partial: throws on Nothing. Use fromMaybe or pattern match instead."+ name: Himari.SafePrelude.fromJust+ within: [] - functions: - message: "Partial: throws on parse failure. Use readMay or readDef from Safe instead." name: read@@ -931,40 +1039,148 @@ - message: "Partial: throws on empty structure. Use foldl1May from Safe.Foldable instead." name: Data.Foldable.foldl1 within: []+ - message: "Partial: throws on empty structure. Use foldl1May from Safe.Foldable instead."+ name: Himari.foldl1+ within: []+ - message: "Partial: throws on empty structure. Use foldl1May from Safe.Foldable instead."+ name: Himari.Prelude.foldl1+ within: []+ - message: "Partial: throws on empty structure. Use foldl1May from Safe.Foldable instead."+ name: Himari.SafePrelude.foldl1+ within: [] - message: "Partial: throws on empty structure. Use foldr1May from Safe.Foldable instead." name: Data.Foldable.foldr1 within: []+ - message: "Partial: throws on empty structure. Use foldr1May from Safe.Foldable instead."+ name: Himari.foldr1+ within: []+ - message: "Partial: throws on empty structure. Use foldr1May from Safe.Foldable instead."+ name: Himari.Prelude.foldr1+ within: []+ - message: "Partial: throws on empty structure. Use foldr1May from Safe.Foldable instead."+ name: Himari.SafePrelude.foldr1+ within: [] - message: "Partial: throws on empty structure. Use maximumMay or maximumBound from Safe.Foldable instead." name: Data.Foldable.maximum within: []+ - message: "Partial: throws on empty structure. Use maximumMay or maximumBound from Safe.Foldable instead."+ name: Himari.maximum+ within: []+ - message: "Partial: throws on empty structure. Use maximumMay or maximumBound from Safe.Foldable instead."+ name: Himari.Prelude.maximum+ within: []+ - message: "Partial: throws on empty structure. Use maximumMay or maximumBound from Safe.Foldable instead."+ name: Himari.SafePrelude.maximum+ within: [] - message: "Partial: throws on empty structure. Use minimumMay or minimumBound from Safe.Foldable instead." name: Data.Foldable.minimum within: []+ - message: "Partial: throws on empty structure. Use minimumMay or minimumBound from Safe.Foldable instead."+ name: Himari.minimum+ within: []+ - message: "Partial: throws on empty structure. Use minimumMay or minimumBound from Safe.Foldable instead."+ name: Himari.Prelude.minimum+ within: []+ - message: "Partial: throws on empty structure. Use minimumMay or minimumBound from Safe.Foldable instead."+ name: Himari.SafePrelude.minimum+ within: [] - message: "Partial: throws on empty structure. Use maximumByMay or maximumBoundBy from Safe.Foldable instead." name: Data.Foldable.maximumBy within: []+ - message: "Partial: throws on empty structure. Use maximumByMay or maximumBoundBy from Safe.Foldable instead."+ name: Himari.maximumBy+ within: []+ - message: "Partial: throws on empty structure. Use maximumByMay or maximumBoundBy from Safe.Foldable instead."+ name: Himari.Prelude.maximumBy+ within: []+ - message: "Partial: throws on empty structure. Use maximumByMay or maximumBoundBy from Safe.Foldable instead."+ name: Himari.SafePrelude.maximumBy+ within: [] - message: "Partial: throws on empty structure. Use minimumByMay or minimumBoundBy from Safe.Foldable instead." name: Data.Foldable.minimumBy within: []+ - message: "Partial: throws on empty structure. Use minimumByMay or minimumBoundBy from Safe.Foldable instead."+ name: Himari.minimumBy+ within: []+ - message: "Partial: throws on empty structure. Use minimumByMay or minimumBoundBy from Safe.Foldable instead."+ name: Himari.Prelude.minimumBy+ within: []+ - message: "Partial: throws on empty structure. Use minimumByMay or minimumBoundBy from Safe.Foldable instead."+ name: Himari.SafePrelude.minimumBy+ within: [] - functions: - message: "Partial: throws on empty structure. Use bifoldl with a default value instead." name: Data.Bifoldable.bifoldl1 within: []+ - message: "Partial: throws on empty structure. Use bifoldl with a default value instead."+ name: Himari.bifoldl1+ within: []+ - message: "Partial: throws on empty structure. Use bifoldl with a default value instead."+ name: Himari.Prelude.bifoldl1+ within: []+ - message: "Partial: throws on empty structure. Use bifoldl with a default value instead."+ name: Himari.SafePrelude.bifoldl1+ within: [] - message: "Partial: throws on empty structure. Use bifoldr with a default value instead." name: Data.Bifoldable.bifoldr1 within: []+ - message: "Partial: throws on empty structure. Use bifoldr with a default value instead."+ name: Himari.bifoldr1+ within: []+ - message: "Partial: throws on empty structure. Use bifoldr with a default value instead."+ name: Himari.Prelude.bifoldr1+ within: []+ - message: "Partial: throws on empty structure. Use bifoldr with a default value instead."+ name: Himari.SafePrelude.bifoldr1+ within: [] - message: "Partial: throws on empty structure. Use bifoldr with explicit initial value." name: Data.Bifoldable.bimaximum within: [] - message: "Partial: throws on empty structure. Use bifoldr with explicit initial value."+ name: Himari.bimaximum+ within: []+ - message: "Partial: throws on empty structure. Use bifoldr with explicit initial value."+ name: Himari.Prelude.bimaximum+ within: []+ - message: "Partial: throws on empty structure. Use bifoldr with explicit initial value."+ name: Himari.SafePrelude.bimaximum+ within: []+ - message: "Partial: throws on empty structure. Use bifoldr with explicit initial value." name: Data.Bifoldable.biminimum within: [] - message: "Partial: throws on empty structure. Use bifoldr with explicit initial value."+ name: Himari.biminimum+ within: []+ - message: "Partial: throws on empty structure. Use bifoldr with explicit initial value."+ name: Himari.Prelude.biminimum+ within: []+ - message: "Partial: throws on empty structure. Use bifoldr with explicit initial value."+ name: Himari.SafePrelude.biminimum+ within: []+ - message: "Partial: throws on empty structure. Use bifoldr with explicit initial value." name: Data.Bifoldable.bimaximumBy within: [] - message: "Partial: throws on empty structure. Use bifoldr with explicit initial value."+ name: Himari.bimaximumBy+ within: []+ - message: "Partial: throws on empty structure. Use bifoldr with explicit initial value."+ name: Himari.Prelude.bimaximumBy+ within: []+ - message: "Partial: throws on empty structure. Use bifoldr with explicit initial value."+ name: Himari.SafePrelude.bimaximumBy+ within: []+ - message: "Partial: throws on empty structure. Use bifoldr with explicit initial value." name: Data.Bifoldable.biminimumBy within: []+ - message: "Partial: throws on empty structure. Use bifoldr with explicit initial value."+ name: Himari.biminimumBy+ within: []+ - message: "Partial: throws on empty structure. Use bifoldr with explicit initial value."+ name: Himari.Prelude.biminimumBy+ within: []+ - message: "Partial: throws on empty structure. Use bifoldr with explicit initial value."+ name: Himari.SafePrelude.biminimumBy+ within: [] - functions: - message: "Partial: throws on out-of-bounds. Use atMay or atDef from Safe with toList instead." name: "Data.List.NonEmpty.!!"@@ -1153,41 +1369,158 @@ name: System.IO.getChar within: [] - message: Encoding depends on LANG env. Use Text or ByteString.+ name: Himari.getChar+ within: []+ - message: Encoding depends on LANG env. Use Text or ByteString.+ name: Himari.Prelude.getChar+ within: []+ - message: Encoding depends on LANG env. Use Text or ByteString.+ name: Himari.SafePrelude.getChar+ within: []+ - message: Encoding depends on LANG env. Use Text or ByteString. name: System.IO.getLine within: []+ - message: Encoding depends on LANG env. Use Text or ByteString.+ name: Himari.getLine+ within: []+ - message: Encoding depends on LANG env. Use Text or ByteString.+ name: Himari.Prelude.getLine+ within: []+ - message: Encoding depends on LANG env. Use Text or ByteString.+ name: Himari.SafePrelude.getLine+ within: [] - message: "Lazy IO + env-dependent encoding. Use strict Text or ByteString." name: System.IO.getContents within: []+ - message: "Lazy IO + env-dependent encoding. Use strict Text or ByteString."+ name: Himari.getContents+ within: []+ - message: "Lazy IO + env-dependent encoding. Use strict Text or ByteString."+ name: Himari.Prelude.getContents+ within: []+ - message: "Lazy IO + env-dependent encoding. Use strict Text or ByteString."+ name: Himari.SafePrelude.getContents+ within: [] - message: "Lazy IO + env-dependent encoding. Use Data.Text.IO.readFile or Data.ByteString.readFile." name: System.IO.readFile within: []+ - message: "Lazy IO + env-dependent encoding. Use Data.Text.IO.readFile or Data.ByteString.readFile."+ name: Himari.readFile+ within: []+ - message: "Lazy IO + env-dependent encoding. Use Data.Text.IO.readFile or Data.ByteString.readFile."+ name: Himari.Prelude.readFile+ within: []+ - message: "Lazy IO + env-dependent encoding. Use Data.Text.IO.readFile or Data.ByteString.readFile."+ name: Himari.SafePrelude.readFile+ within: [] - message: "Lazy IO + env-dependent encoding." name: System.IO.interact within: []+ - message: "Lazy IO + env-dependent encoding."+ name: Himari.interact+ within: []+ - message: "Lazy IO + env-dependent encoding."+ name: Himari.Prelude.interact+ within: []+ - message: "Lazy IO + env-dependent encoding."+ name: Himari.SafePrelude.interact+ within: [] - message: "Env-dependent + partial (uses read)." name: System.IO.readIO within: [] - message: "Env-dependent + partial (uses read)."+ name: Himari.readIO+ within: []+ - message: "Env-dependent + partial (uses read)."+ name: Himari.Prelude.readIO+ within: []+ - message: "Env-dependent + partial (uses read)."+ name: Himari.SafePrelude.readIO+ within: []+ - message: "Env-dependent + partial (uses read)." name: System.IO.readLn within: []+ - message: "Env-dependent + partial (uses read)."+ name: Himari.readLn+ within: []+ - message: "Env-dependent + partial (uses read)."+ name: Himari.Prelude.readLn+ within: []+ - message: "Env-dependent + partial (uses read)."+ name: Himari.SafePrelude.readLn+ within: [] - message: Encoding depends on LANG env. Use Text or ByteString. name: System.IO.putChar within: [] - message: Encoding depends on LANG env. Use Text or ByteString.+ name: Himari.putChar+ within: []+ - message: Encoding depends on LANG env. Use Text or ByteString.+ name: Himari.Prelude.putChar+ within: []+ - message: Encoding depends on LANG env. Use Text or ByteString.+ name: Himari.SafePrelude.putChar+ within: []+ - message: Encoding depends on LANG env. Use Text or ByteString. name: System.IO.putStr within: [] - message: Encoding depends on LANG env. Use Text or ByteString.+ name: Himari.putStr+ within: []+ - message: Encoding depends on LANG env. Use Text or ByteString.+ name: Himari.Prelude.putStr+ within: []+ - message: Encoding depends on LANG env. Use Text or ByteString.+ name: Himari.SafePrelude.putStr+ within: []+ - message: Encoding depends on LANG env. Use Text or ByteString. name: System.IO.putStrLn within: [] - message: Encoding depends on LANG env. Use Text or ByteString.+ name: Himari.putStrLn+ within: []+ - message: Encoding depends on LANG env. Use Text or ByteString.+ name: Himari.Prelude.putStrLn+ within: []+ - message: Encoding depends on LANG env. Use Text or ByteString.+ name: Himari.SafePrelude.putStrLn+ within: []+ - message: Encoding depends on LANG env. Use Text or ByteString. name: System.IO.print within: []+ - message: Encoding depends on LANG env. Use Text or ByteString.+ name: Himari.print+ within: []+ - message: Encoding depends on LANG env. Use Text or ByteString.+ name: Himari.Prelude.print+ within: []+ - message: Encoding depends on LANG env. Use Text or ByteString.+ name: Himari.SafePrelude.print+ within: [] - message: "Env-dependent encoding. Use Data.Text.IO.writeFile or Data.ByteString.writeFile." name: System.IO.writeFile within: []+ - message: "Env-dependent encoding. Use Data.Text.IO.writeFile or Data.ByteString.writeFile."+ name: Himari.writeFile+ within: []+ - message: "Env-dependent encoding. Use Data.Text.IO.writeFile or Data.ByteString.writeFile."+ name: Himari.Prelude.writeFile+ within: []+ - message: "Env-dependent encoding. Use Data.Text.IO.writeFile or Data.ByteString.writeFile."+ name: Himari.SafePrelude.writeFile+ within: [] - message: "Env-dependent encoding. Use Data.Text.IO.appendFile or Data.ByteString.appendFile." name: System.IO.appendFile within: []+ - message: "Env-dependent encoding. Use Data.Text.IO.appendFile or Data.ByteString.appendFile."+ name: Himari.appendFile+ within: []+ - message: "Env-dependent encoding. Use Data.Text.IO.appendFile or Data.ByteString.appendFile."+ name: Himari.Prelude.appendFile+ within: []+ - message: "Env-dependent encoding. Use Data.Text.IO.appendFile or Data.ByteString.appendFile."+ name: Himari.SafePrelude.appendFile+ within: [] - functions: - message: Use Data.Text.uncons instead name: Data.Text.head@@ -1348,6 +1681,12 @@ within: [] - message: Use typed exceptions instead. Define a proper exception type. name: UnliftIO.Exception.stringException+ within: []+ - message: Use typed exceptions instead. Define a proper exception type.+ name: Himari.stringException+ within: []+ - message: Use typed exceptions instead. Define a proper exception type.+ name: Himari.Prelude.stringException within: [] - functions: - message: Use UnliftIO.Async.async instead for MonadUnliftIO support.
CHANGELOG.md view
@@ -7,6 +7,27 @@ ## [Unreleased] +## [1.1.6.3] - 2026-11-19++### Fixed++- hlint rules written with a module-qualified name,+ such as `Data.List.head` and `System.IO.readFile`,+ did not match symbols used through `import Himari`,+ because hlint does not follow re-exports.+ The functions listed in the following items are now warned when used through `Himari`,+ `Himari.Prelude`, or `Himari.SafePrelude`.+ Projects that use them through these modules will get new warnings+- Warn partial functions of `Data.List`, `Data.Foldable`, and `Data.Bifoldable`+ used through himari, such as `head`, `tail`, `foldl1`, `maximum`, and `bifoldl1`+- Warn `fromJust` of `Data.Maybe` used through himari+- Warn `String` IO functions of `System.IO` used through himari,+ such as `print`, `putStrLn`, `getLine`, and `readFile`+- Warn `singular`, `unsafeSingular`, `unsafePartsOf`, and `iunsafePartsOf`+ of `Control.Lens.Traversal` used through himari+- Warn `stringException` of `UnliftIO.Exception` used through `Himari` or `Himari.Prelude`.+ `Himari.SafePrelude` does not export it+ ## [1.1.6.2] - 2026-11-01 ### Changed
himari.cabal view
@@ -1,6 +1,6 @@ cabal-version: 3.12 name: himari-version: 1.1.6.2+version: 1.1.6.3 synopsis: A standard library for Haskell as an alternative to rio description: A standard library for Haskell inspired by rio.@@ -153,8 +153,15 @@ Himari.Title HlintBaseSpec HlintExtensionSpec+ HlintReexportSpec+ HlintRunner HlintSamples.BasePartial HlintSamples.ExtensionDangerous+ HlintSamples.ReexportExplicitImport+ HlintSamples.ReexportHimari+ HlintSamples.ReexportHimariPrelude+ HlintSamples.ReexportImportForms+ HlintSamples.ReexportSafePrelude HlintSamples.UnliftioPreference HlintUnliftioSpec TitleSpec@@ -168,6 +175,7 @@ QuickCheck >=2.15.0.1, himari, sydtest >=0.18.0.0,+ yaml >=0.11.11.2, build-tool-depends: hlint:hlint >=3.10
test/HlintBaseSpec.hs view
@@ -1,65 +1,49 @@ module HlintBaseSpec (spec) where -import Data.Text qualified as T import Himari+import HlintRunner import Test.Syd spec :: Spec spec = do- describe "hlint base library rules" . beforeAll runHlintOnSample $ do+ describe "hlint base library rules" . withSample "BasePartial.hs" $ do describe "Data.List partial functions" $ do- itWithOuter "Data.List.head should warn (partial function)" $ \output -> do- output `shouldSatisfy` containsWarning "headMay"+ itWithOuter "head through Himari should warn (partial function)" $ \sample ->+ shouldWarnFunction sample "useHead" "head" "headMay" + itWithOuter "Data.List.head should warn (partial function)" $ \sample ->+ shouldWarnFunction sample "useHeadQualified" "L.head" "headMay"+ describe "NonEmpty.head should be safe" $ do- itWithOuter "Data.List.NonEmpty.head should NOT warn (it's safe)" $ \output -> do- output `shouldSatisfy` notContainsWarningAt "useNonEmptyHead"+ itWithOuter "Data.List.NonEmpty.head should NOT warn (it's safe)" $ \sample ->+ sample `shouldNotWarnFunctionIn` "useNonEmptyHead" describe "Debug.Trace functions suggest pTrace*" $ do- itWithOuter "Debug.Trace.trace should suggest pTrace" $ \output -> do- output `shouldSatisfy` containsWarningAt "trace" "pTrace"+ itWithOuter "Debug.Trace.trace should suggest pTrace" $ \sample ->+ shouldSuggestIn sample "useTrace" "Use pTrace" - itWithOuter "Debug.Trace.traceShow should suggest pTraceShow" $ \output -> do- output `shouldSatisfy` containsWarningAt "traceShow" "pTraceShow"+ itWithOuter "Debug.Trace.traceShow should suggest pTraceShow" $ \sample ->+ shouldSuggestIn sample "useTraceShow" "Use pTraceShow" - itWithOuter "Debug.Trace.traceM should suggest pTraceM" $ \output -> do- output `shouldSatisfy` containsWarningAt "traceM" "pTraceM"+ itWithOuter "Debug.Trace.traceM should suggest pTraceM" $ \sample ->+ shouldSuggestIn sample "useTraceM" "Use pTraceM" describe "Numeric integer-showing functions are partial" $ do- itWithOuter "showHex should warn (partial function)" $ \output -> do- output `shouldSatisfy` containsWarningAt "showHex" "non-negative"-- itWithOuter "showInt should warn (partial function)" $ \output -> do- output `shouldSatisfy` containsWarningAt "showInt" "non-negative"-- itWithOuter "showOct should warn (partial function)" $ \output -> do- output `shouldSatisfy` containsWarningAt "showOct" "non-negative"-- itWithOuter "showBin should warn (partial function)" $ \output -> do- output `shouldSatisfy` containsWarningAt "showBin" "non-negative"+ itWithOuter "showHex should warn (partial function)" $ \sample ->+ shouldWarnFunction sample "useShowHex" "showHex" "non-negative" - itWithOuter "showIntAtBase should warn (partial function)" $ \output -> do- output `shouldSatisfy` containsWarningAt "showIntAtBase" "base > 1"+ itWithOuter "showInt should warn (partial function)" $ \sample ->+ shouldWarnFunction sample "useShowInt" "showInt" "non-negative" -runHlintOnSample :: IO Text-runHlintOnSample = do- (exitCode, stdoutOutput, stderrOutput) <-- readProcess $- proc "hlint" ["test/HlintSamples/BasePartial.hs"]- pure $ case exitCode of- ExitSuccess -> convert stdoutOutput- ExitFailure _ -> convert stdoutOutput <> convert stderrOutput+ itWithOuter "showOct should warn (partial function)" $ \sample ->+ shouldWarnFunction sample "useShowOct" "showOct" "non-negative" -containsWarning :: Text -> Text -> Bool-containsWarning expectedMsg output = expectedMsg `T.isInfixOf` output+ itWithOuter "showBin should warn (partial function)" $ \sample ->+ shouldWarnFunction sample "useShowBin" "showBin" "non-negative" -containsWarningAt :: Text -> Text -> Text -> Bool-containsWarningAt funcName expectedMsg output =- let msgAndFunc = funcName `T.isInfixOf` output && expectedMsg `T.isInfixOf` output- in msgAndFunc+ itWithOuter "showIntAtBase should warn (partial function)" $ \sample ->+ shouldWarnFunction sample "useShowIntAtBase" "showIntAtBase" "base > 1" -notContainsWarningAt :: Text -> Text -> Bool-notContainsWarningAt funcName output =- let linesWithFunc =- filter (T.isInfixOf funcName) (T.lines output)- in null linesWithFunc+ describe "Data.Char partial functions" $ do+ itWithOuter "intToDigit should warn (partial function)" $ \sample ->+ shouldWarnFunction sample "useShowIntAtBase" "intToDigit" "intToDigitMay"
test/HlintExtensionSpec.hs view
@@ -1,31 +1,19 @@ module HlintExtensionSpec (spec) where -import Data.Text qualified as T import Himari+import HlintRunner import Test.Syd spec :: Spec spec = do- describe "hlint extension restriction rules" . beforeAll runHlintOnSample $ do+ describe "hlint extension restriction rules" . withSample "ExtensionDangerous.hs" $ do describe "Dangerous extensions" $ do- itWithOuter "AllowAmbiguousTypes should warn" $ \output -> do- output `shouldSatisfy` containsWarning "AllowAmbiguousTypes"+ itWithOuter "AllowAmbiguousTypes should warn" $ \sample ->+ sample `shouldWarnExtension` "AllowAmbiguousTypes" describe "Discouraged extensions" $ do- itWithOuter "ImplicitParams should warn" $ \output -> do- output `shouldSatisfy` containsWarning "ImplicitParams"-- itWithOuter "UndecidableInstances should warn" $ \output -> do- output `shouldSatisfy` containsWarning "UndecidableInstances"--runHlintOnSample :: IO Text-runHlintOnSample = do- (exitCode, stdoutOutput, stderrOutput) <-- readProcess $- proc "hlint" ["test/HlintSamples/ExtensionDangerous.hs"]- pure $ case exitCode of- ExitSuccess -> convert stdoutOutput- ExitFailure _ -> convert stdoutOutput <> convert stderrOutput+ itWithOuter "ImplicitParams should warn" $ \sample ->+ sample `shouldWarnExtension` "ImplicitParams" -containsWarning :: Text -> Text -> Bool-containsWarning expectedMsg output = expectedMsg `T.isInfixOf` output+ itWithOuter "UndecidableInstances should warn" $ \sample ->+ sample `shouldWarnExtension` "UndecidableInstances"
+ test/HlintReexportSpec.hs view
@@ -0,0 +1,241 @@+module HlintReexportSpec (spec) where++import Data.Set qualified as Set+import Data.Text qualified as T+import Himari+import HlintRunner+import Test.Syd++spec :: Spec+spec = do+ describe "hlint rules for symbols re-exported through himari" $ do+ describe "rules in .hlint.yaml" . beforeAll loadRestrictedFunctionNames $ do+ itWithOuter "rules for Himari should be exactly the tested functions" $ \names ->+ rulesThrough "Himari" names `shouldBe` reexportedFunctionNames++ itWithOuter "rules for Himari.Prelude should be exactly the tested functions" $ \names ->+ rulesThrough "Himari.Prelude" names `shouldBe` reexportedFunctionNames++ itWithOuter "rules for Himari.SafePrelude should leave out the functions it lacks" $ \names ->+ rulesThrough "Himari.SafePrelude" names+ `shouldBe` (reexportedFunctionNames `Set.difference` preludeOnlyFunctionNames)++ describe "import Himari" . withSample "ReexportHimari.hs" $ do+ describe "every restricted function re-exported by Himari should warn" $ do+ forM_ reexportedRestrictedFunctions $ \(declName, function, fragment) ->+ itWithOuter (convert function <> " should warn with its own message") $ \sample ->+ shouldWarnFunction sample declName function fragment++ describe "functions restricted by a rule without a module name should warn" $ do+ itWithOuter "throwString should warn" $ \sample ->+ shouldWarnFunction sample "useThrowString" "throwString" "typed exceptions with throwM"++ describe "functions of both Data.List and Data.Foldable should have a single message" $ do+ itWithOuter "every function should be among the tested functions" $ \(_ :: Sample) ->+ fmap snd listAndFoldableFunctions `shouldBe` listAndFoldableFunctionNames++ forM_ listAndFoldableFunctions $ \(declName, function) ->+ itWithOuter (convert function <> " should not have concatenated messages") $ \sample ->+ shouldWarnFunctionOnce sample declName function "Partial:"++ describe "total alternatives should NOT warn" $ do+ itWithOuter "headMay should NOT warn" $ \sample ->+ sample `shouldNotWarnFunctionIn` "useHeadMay"++ itWithOuter "fromMaybe should NOT warn" $ \sample ->+ sample `shouldNotWarnFunctionIn` "useFromMaybe"++ describe "UnliftIO versions exported by Himari should NOT warn" $ do+ forM_ unliftioDeclarations $ \declName ->+ itWithOuter (convert declName <> " should NOT warn") $ \sample ->+ sample `shouldNotWarnFunctionIn` declName++ describe "import Himari.Prelude" . withSample "ReexportHimariPrelude.hs" $ do+ itWithOuter "head should warn" $ \sample ->+ shouldWarnFunction sample "useHead" "head" "headMay"++ itWithOuter "foldl1 should warn with a single message" $ \sample ->+ shouldWarnFunctionOnce sample "useFoldl1" "foldl1" "Partial:"++ itWithOuter "bifoldl1 should warn" $ \sample ->+ shouldWarnFunction sample "useBifoldl1" "bifoldl1" "bifoldl with a default value"++ itWithOuter "fromJust should warn" $ \sample ->+ shouldWarnFunction sample "useFromJust" "fromJust" "fromMaybe"++ itWithOuter "readFile should warn" $ \sample ->+ shouldWarnFunction sample "useReadFile" "readFile" "Lazy IO"++ itWithOuter "singular should warn" $ \sample ->+ shouldWarnFunction sample "useSingular" "singular" "(^?)"++ itWithOuter "stringException should warn" $ \sample ->+ shouldWarnFunction+ sample+ "useStringException"+ "stringException"+ "Define a proper exception type"++ itWithOuter "headMay should NOT warn" $ \sample ->+ sample `shouldNotWarnFunctionIn` "useHeadMay"++ itWithOuter "catch of UnliftIO should NOT warn" $ \sample ->+ sample `shouldNotWarnFunctionIn` "useCatch"++ describe "import Himari.SafePrelude" . withSample "ReexportSafePrelude.hs" $ do+ itWithOuter "head should warn" $ \sample ->+ shouldWarnFunction sample "useHead" "head" "headMay"++ itWithOuter "foldl1 should warn with a single message" $ \sample ->+ shouldWarnFunctionOnce sample "useFoldl1" "foldl1" "Partial:"++ itWithOuter "bifoldl1 should warn" $ \sample ->+ shouldWarnFunction sample "useBifoldl1" "bifoldl1" "bifoldl with a default value"++ itWithOuter "fromJust should warn" $ \sample ->+ shouldWarnFunction sample "useFromJust" "fromJust" "fromMaybe"++ itWithOuter "readFile should warn" $ \sample ->+ shouldWarnFunction sample "useReadFile" "readFile" "Lazy IO"++ itWithOuter "singular should warn" $ \sample ->+ shouldWarnFunction sample "useSingular" "singular" "(^?)"++ itWithOuter "headMay should NOT warn" $ \sample ->+ sample `shouldNotWarnFunctionIn` "useHeadMay"++ describe "import forms of Himari" . withSample "ReexportImportForms.hs" $ do+ itWithOuter "head qualified with the alias of Himari should warn" $ \sample ->+ shouldWarnFunction sample "useQualifiedHead" "H.head" "headMay"++ itWithOuter "readFile qualified with the alias of Himari should warn" $ \sample ->+ shouldWarnFunction sample "useQualifiedReadFile" "H.readFile" "Lazy IO"++ itWithOuter "tail should warn even though other names are hidden" $ \sample ->+ shouldWarnFunction sample "useTail" "tail" "tailMay"++ itWithOuter "head of Data.List.NonEmpty should NOT warn when hidden from Himari" $ \sample ->+ sample `shouldNotWarnFunctionIn` "useNonEmptyHead"++ itWithOuter "head qualified with Data.List.NonEmpty should NOT warn" $ \sample ->+ sample `shouldNotWarnFunctionIn` "useNonEmptyHeadQualified"++ itWithOuter "definition of a user function named last should NOT warn" $ \sample ->+ sample `shouldNotWarnFunctionIn` "last"++ itWithOuter "use of a user function named last should NOT warn" $ \sample ->+ sample `shouldNotWarnFunctionIn` "useOwnLast"++ describe "explicit import list of Himari" . withSample "ReexportExplicitImport.hs" $ do+ itWithOuter "head should warn" $ \sample ->+ shouldWarnFunction sample "useHead" "head" "headMay"++ itWithOuter "readFile should warn" $ \sample ->+ shouldWarnFunction sample "useReadFile" "readFile" "Lazy IO"++ itWithOuter "headMay should NOT warn" $ \sample ->+ sample `shouldNotWarnFunctionIn` "useHeadMay"++-- | Module qualified names of the rules for the functions re-exported through the module.+-- Rules for the modules below it, such as @Himari.Prelude.head@ for @Himari@, are left out.+-- This does not support operators that contain a dot.+rulesThrough :: Text -> [Text] -> Set.Set Text+rulesThrough reexporter ruleNames =+ Set.fromList+ [ function+ | ruleName <- ruleNames+ , Just function <- [T.stripPrefix (reexporter <> ".") ruleName]+ , not ("." `T.isInfixOf` function)+ ]++-- | Names of the functions in 'reexportedRestrictedFunctions'.+reexportedFunctionNames :: Set.Set Text+reexportedFunctionNames =+ Set.fromList [function | (_, function, _) <- reexportedRestrictedFunctions]++-- | Functions re-exported by "Himari.Prelude" but not by "Himari.SafePrelude".+preludeOnlyFunctionNames :: Set.Set Text+preludeOnlyFunctionNames = Set.fromList ["stringException"]++-- | Declarations of the sample paired with the restricted function they use+-- and a fragment of the message of its rule.+-- These are all the functions that have a rule qualified with a module of himari in @.hlint.yaml@,+-- which is verified against @.hlint.yaml@ by the tests.+-- They are re-exported by "Himari" as the very same entity as the original one.+reexportedRestrictedFunctions :: [(Text, Text, Text)]+reexportedRestrictedFunctions =+ [ ("useHead", "head", "headMay")+ , ("useTail", "tail", "tailMay")+ , ("useInit", "init", "initMay")+ , ("useLast", "last", "lastMay")+ , ("useCycle", "cycle", "cycleMay")+ , ("useScanl1", "scanl1", "scanl1May")+ , ("useScanr1", "scanr1", "scanr1May")+ , ("useFoldl1", "foldl1", "foldl1May from Safe.Foldable")+ , ("useFoldr1", "foldr1", "foldr1May from Safe.Foldable")+ , ("useMaximum", "maximum", "maximumMay or maximumBound from Safe.Foldable")+ , ("useMinimum", "minimum", "minimumMay or minimumBound from Safe.Foldable")+ , ("useMaximumBy", "maximumBy", "maximumByMay or maximumBoundBy from Safe.Foldable")+ , ("useMinimumBy", "minimumBy", "minimumByMay or minimumBoundBy from Safe.Foldable")+ , ("useBifoldl1", "bifoldl1", "bifoldl with a default value")+ , ("useBifoldr1", "bifoldr1", "bifoldr with a default value")+ , ("useBimaximum", "bimaximum", "bifoldr with explicit initial value")+ , ("useBiminimum", "biminimum", "bifoldr with explicit initial value")+ , ("useBimaximumBy", "bimaximumBy", "bifoldr with explicit initial value")+ , ("useBiminimumBy", "biminimumBy", "bifoldr with explicit initial value")+ , ("useFromJust", "fromJust", "fromMaybe")+ , ("useGetChar", "getChar", "Encoding depends on LANG")+ , ("useGetLine", "getLine", "Encoding depends on LANG")+ , ("useGetContents", "getContents", "Use strict Text or ByteString")+ , ("useInteract", "interact", "Lazy IO + env-dependent encoding.")+ , ("useReadIO", "readIO", "partial (uses read)")+ , ("useReadLn", "readLn", "partial (uses read)")+ , ("usePutChar", "putChar", "Encoding depends on LANG")+ , ("usePutStr", "putStr", "Encoding depends on LANG")+ , ("usePutStrLn", "putStrLn", "Encoding depends on LANG")+ , ("usePrint", "print", "Encoding depends on LANG")+ , ("useReadFile", "readFile", "Data.Text.IO.readFile")+ , ("useWriteFile", "writeFile", "Data.Text.IO.writeFile")+ , ("useAppendFile", "appendFile", "Data.Text.IO.appendFile")+ , ("useSingular", "singular", "(^?)")+ , ("useUnsafeSingular", "unsafeSingular", "0 or >1 elements")+ , ("useUnsafePartsOf", "unsafePartsOf", "list length changes")+ , ("useIunsafePartsOf", "iunsafePartsOf", "list length changes")+ , ("useStringException", "stringException", "Define a proper exception type")+ ]++-- | Functions that both "Data.List" and "Data.Foldable" export as the very same entity.+-- Both modules have a rule for them, so their messages are prone to be concatenated.+-- The declarations that use them are looked up in 'reexportedRestrictedFunctions'.+listAndFoldableFunctions :: [(Text, Text)]+listAndFoldableFunctions =+ [ (declName, function)+ | (declName, function, _) <- reexportedRestrictedFunctions+ , function `elem` listAndFoldableFunctionNames+ ]++-- | Names of the functions in 'listAndFoldableFunctions'.+listAndFoldableFunctionNames :: [Text]+listAndFoldableFunctionNames =+ [ "foldl1"+ , "foldr1"+ , "maximum"+ , "minimum"+ , "maximumBy"+ , "minimumBy"+ ]++-- | Declarations of the sample that use the UnliftIO version of a name+-- whose non-UnliftIO version has a rule in @.hlint.yaml@.+unliftioDeclarations :: [Text]+unliftioDeclarations =+ [ "useCatch"+ , "useBracket"+ , "useFinally"+ , "useTry"+ , "useWithMVar"+ , "useModifyMVar"+ , "useTimeout"+ , "useMyThreadId"+ , "useDoesFileExist"+ ]
+ test/HlintRunner.hs view
@@ -0,0 +1,165 @@+-- | Helpers to run hlint on a sample file and assert on its structured output.+-- Assertions are keyed on declaration names instead of substrings of the whole output,+-- so a warning for one declaration can never satisfy an expectation for another one.+module HlintRunner+ ( Sample+ , withSample+ , loadRestrictedFunctionNames+ , shouldWarnFunction+ , shouldWarnFunctionOnce+ , shouldNotWarnFunctionIn+ , shouldSuggestIn+ , shouldWarnExtension+ ) where++import Data.Text qualified as T+import Data.Text.IO qualified as T+import Data.Yaml qualified as Yaml+import Himari+import Test.Syd++-- | A single hint reported by @hlint --json@.+data Idea = Idea+ { decl :: [Text]+ -- ^ Names of the declarations the hint belongs to.+ , severity :: Text+ -- ^ Severity of the hint. hlint reports a file it failed to parse as @Error@.+ , hint :: Text+ -- ^ Title of the hint such as @Avoid restricted function@.+ , from :: Text+ -- ^ Source fragment the hint points at.+ , note :: [Text]+ -- ^ Notes of the hint. The message of a restriction rule ends up here.+ , startLine :: Int+ }+ deriving stock (Eq, Generic, Show)++instance FromJSON Idea++-- | A sample file together with the hints hlint reported for it.+data Sample = Sample+ { path :: FilePath+ , source :: Text+ , ideas :: [Idea]+ }+ deriving stock (Eq, Generic, Show)++-- | Run hlint with the project's @.hlint.yaml@ on a sample file.+-- hlint exits with a failure code when it reports hints, so the exit code is ignored.+-- A sample hlint failed to parse is rejected here,+-- because hlint reports nothing but the parse error for it+-- and every negative expectation on it would pass without checking anything.+loadSample :: FilePath -> IO Sample+loadSample path = do+ source <- T.readFile path+ (_, stdoutOutput, stderrOutput) <- readProcess $ proc "hlint" ["--json", path]+ case eitherDecode stdoutOutput of+ Right ideas -> case filter (\idea -> idea.severity == "Error") ideas of+ [] -> pure Sample{path, source, ideas}+ errors ->+ expectationFailure $+ "hlint reported errors for " <> path <> ": " <> show errors+ Left err ->+ expectationFailure $+ "failed to parse hlint output for "+ <> path+ <> ": "+ <> err+ <> "\n"+ <> T.unpack (convert stderrOutput)++-- | Run hlint once on a file of @test/HlintSamples@ and share the result among the tests.+withSample :: FilePath -> TestDefM '[Sample] () () -> Spec+withSample name = beforeAll (loadSample ("test/HlintSamples/" <> name))++-- | A rule of @.hlint.yaml@.+-- Only the function restrictions are read, the other kinds of rules have no @functions@ key.+newtype Rule = Rule+ { functions :: Maybe [FunctionRestriction]+ }+ deriving stock (Eq, Generic, Show)++instance FromJSON Rule++-- | A function restriction of @.hlint.yaml@.+newtype FunctionRestriction = FunctionRestriction+ { name :: Text+ }+ deriving stock (Eq, Generic, Show)++instance FromJSON FunctionRestriction++-- | Names of all the functions restricted by the project's @.hlint.yaml@.+loadRestrictedFunctionNames :: IO [Text]+loadRestrictedFunctionNames =+ Yaml.decodeFileEither ".hlint.yaml" >>= \case+ Right (rules :: [Rule]) ->+ pure [restriction.name | rule <- rules, restriction <- fold rule.functions]+ Left err ->+ expectationFailure $+ "failed to parse .hlint.yaml: " <> Yaml.prettyPrintParseException err++-- | Fail unless the sample defines the declaration.+-- This keeps negative expectations from passing vacuously+-- when a declaration is renamed or removed from the sample.+shouldDefine :: (HasCallStack) => Sample -> Text -> IO ()+shouldDefine sample declName =+ unless (any (T.isPrefixOf (declName <> " ::")) (T.lines sample.source)) . expectationFailure $+ sample.path <> " does not define " <> T.unpack declName++restrictedFunctionHint :: Text+restrictedFunctionHint = "Avoid restricted function"++-- | Restricted function warnings reported inside the declaration.+restrictedFunctionsIn :: Text -> Sample -> [Idea]+restrictedFunctionsIn declName sample =+ filter (\idea -> idea.hint == restrictedFunctionHint && declName `elem` idea.decl) sample.ideas++-- | Expect exactly one restricted function warning for the function inside the declaration.+expectSingleWarning :: (HasCallStack) => Sample -> Text -> Text -> IO Idea+expectSingleWarning sample declName function = do+ sample `shouldDefine` declName+ case filter (\idea -> idea.from == function) (restrictedFunctionsIn declName sample) of+ [idea] -> pure idea+ found ->+ expectationFailure $+ "expected exactly one restricted function warning for "+ <> T.unpack function+ <> " in "+ <> T.unpack declName+ <> ", but got "+ <> show found++-- | Expect the function used inside the declaration to be warned+-- with a message that contains the fragment.+shouldWarnFunction :: (HasCallStack) => Sample -> Text -> Text -> Text -> IO ()+shouldWarnFunction sample declName function fragment = do+ idea <- expectSingleWarning sample declName function+ idea.note `shouldSatisfy` any (T.isInfixOf fragment)++-- | Expect the function used inside the declaration to be warned with a single message.+-- hlint concatenates the messages of all matching rules without any separator,+-- so the message of a function matched by several rules contains the marker more than once.+shouldWarnFunctionOnce :: (HasCallStack) => Sample -> Text -> Text -> Text -> IO ()+shouldWarnFunctionOnce sample declName function marker = do+ idea <- expectSingleWarning sample declName function+ fmap (T.count marker) idea.note `shouldBe` [1]++-- | Expect no restricted function warning inside the declaration.+shouldNotWarnFunctionIn :: (HasCallStack) => Sample -> Text -> IO ()+shouldNotWarnFunctionIn sample declName = do+ sample `shouldDefine` declName+ restrictedFunctionsIn declName sample `shouldBe` []++-- | Expect a hint with the given title inside the declaration.+shouldSuggestIn :: (HasCallStack) => Sample -> Text -> Text -> IO ()+shouldSuggestIn sample declName hintTitle = do+ sample `shouldDefine` declName+ filter (\idea -> declName `elem` idea.decl) sample.ideas+ `shouldSatisfy` any (\idea -> idea.hint == hintTitle)++-- | Expect the language extension to be reported as restricted.+shouldWarnExtension :: (HasCallStack) => Sample -> Text -> IO ()+shouldWarnExtension sample extension =+ filter (\idea -> idea.hint == "Avoid restricted extensions") sample.ideas+ `shouldSatisfy` any (\idea -> ("LANGUAGE " <> extension <> " ") `T.isInfixOf` idea.from)
+ test/HlintSamples/ReexportExplicitImport.hs view
@@ -0,0 +1,18 @@+{-# OPTIONS_GHC -Wno-x-partial #-}++-- Sample file to test that hlint rules match symbols imported from Himari+-- with an explicit import list.+module HlintSamples.ReexportExplicitImport where++import Himari (IO, Int, Maybe, String, head, headMay, readFile)++-- These should trigger warnings+useHead :: [Int] -> Int+useHead = head++useReadFile :: IO String+useReadFile = readFile "foo.txt"++-- This should NOT trigger a warning+useHeadMay :: [Int] -> Maybe Int+useHeadMay = headMay
+ test/HlintSamples/ReexportHimari.hs view
@@ -0,0 +1,168 @@+{-# OPTIONS_GHC -Wno-x-partial #-}++-- Sample file to test that hlint rules match symbols re-exported through Himari.+-- Everything is used through a plain `import Himari`, which is the primary way to use himari.+module HlintSamples.ReexportHimari where++import Himari++-- Data.List partial functions (should warn)+useHead :: [Int] -> Int+useHead = head++useTail :: [Int] -> [Int]+useTail = tail++useInit :: [Int] -> [Int]+useInit = init++useLast :: [Int] -> Int+useLast = last++useCycle :: [Int] -> [Int]+useCycle = cycle++useScanl1 :: [Int] -> [Int]+useScanl1 = scanl1 (+)++useScanr1 :: [Int] -> [Int]+useScanr1 = scanr1 (+)++-- Data.Foldable partial functions (should warn)+-- Data.List exports the very same entities, so the rules of both modules apply to them.+useFoldl1 :: [Int] -> Int+useFoldl1 = foldl1 (+)++useFoldr1 :: [Int] -> Int+useFoldr1 = foldr1 (+)++useMaximum :: [Int] -> Int+useMaximum = maximum++useMinimum :: [Int] -> Int+useMinimum = minimum++useMaximumBy :: [Int] -> Int+useMaximumBy = maximumBy (comparing negate)++useMinimumBy :: [Int] -> Int+useMinimumBy = minimumBy (comparing negate)++-- Data.Bifoldable partial functions (should warn)+useBifoldl1 :: (Int, Int) -> Int+useBifoldl1 = bifoldl1 (+)++useBifoldr1 :: (Int, Int) -> Int+useBifoldr1 = bifoldr1 (+)++useBimaximum :: (Int, Int) -> Int+useBimaximum = bimaximum++useBiminimum :: (Int, Int) -> Int+useBiminimum = biminimum++useBimaximumBy :: (Int, Int) -> Int+useBimaximumBy = bimaximumBy (comparing negate)++useBiminimumBy :: (Int, Int) -> Int+useBiminimumBy = biminimumBy (comparing negate)++-- Data.Maybe partial functions (should warn)+useFromJust :: Maybe Int -> Int+useFromJust = fromJust++-- System.IO String IO functions (should warn)+useGetChar :: IO Char+useGetChar = getChar++useGetLine :: IO String+useGetLine = getLine++useGetContents :: IO String+useGetContents = getContents++useInteract :: IO ()+useInteract = interact id++useReadIO :: IO Int+useReadIO = readIO "1"++useReadLn :: IO Int+useReadLn = readLn++usePutChar :: IO ()+usePutChar = putChar 'a'++usePutStr :: IO ()+usePutStr = putStr "a"++usePutStrLn :: IO ()+usePutStrLn = putStrLn "a"++usePrint :: IO ()+usePrint = print (1 :: Int)++useReadFile :: IO String+useReadFile = readFile "foo.txt"++useWriteFile :: IO ()+useWriteFile = writeFile "foo.txt" "a"++useAppendFile :: IO ()+useAppendFile = appendFile "foo.txt" "a"++-- Control.Lens.Traversal partial or unsafe functions (should warn)+useSingular :: [Int] -> Int+useSingular xs = xs ^. singular traverse++useUnsafeSingular :: [Int] -> Int+useUnsafeSingular xs = xs ^. unsafeSingular traverse++useUnsafePartsOf :: [Int] -> [Int]+useUnsafePartsOf xs = xs ^. unsafePartsOf traverse++useIunsafePartsOf :: [Int] -> [Int]+useIunsafePartsOf xs = xs ^. iunsafePartsOf traversed++-- UnliftIO.Exception string exceptions (should warn)+useThrowString :: IO ()+useThrowString = throwString "a"++useStringException :: StringException+useStringException = stringException "a"++-- Total alternatives (should NOT warn)+useHeadMay :: [Int] -> Maybe Int+useHeadMay = headMay++useFromMaybe :: Maybe Int -> Int+useFromMaybe = fromMaybe 0++-- Himari exports the UnliftIO versions of these names, which are the recommended ones.+-- The rules for the base versions of the same names must not apply to them (should NOT warn).+useCatch :: IO ()+useCatch = catch (pure ()) (\(_ :: IOException) -> pure ())++useBracket :: IO ()+useBracket = bracket (pure ()) (const (pure ())) (const (pure ()))++useFinally :: IO ()+useFinally = finally (pure ()) (pure ())++useTry :: IO (Either IOException ())+useTry = try (pure ())++useWithMVar :: MVar () -> IO ()+useWithMVar mv = withMVar mv pure++useModifyMVar :: MVar () -> IO ()+useModifyMVar mv = modifyMVar_ mv pure++useTimeout :: IO (Maybe ())+useTimeout = timeout 1000 (pure ())++useMyThreadId :: IO ThreadId+useMyThreadId = myThreadId++useDoesFileExist :: IO Bool+useDoesFileExist = doesFileExist "foo.txt"
+ test/HlintSamples/ReexportHimariPrelude.hs view
@@ -0,0 +1,36 @@+{-# OPTIONS_GHC -Wno-x-partial #-}++-- Sample file to test that hlint rules match symbols re-exported through Himari.Prelude.+-- Importing Himari.Prelude directly is needed inside himari itself to avoid import cycles.+module HlintSamples.ReexportHimariPrelude where++import Himari.Prelude++-- These should trigger warnings+useHead :: [Int] -> Int+useHead = head++useFoldl1 :: [Int] -> Int+useFoldl1 = foldl1 (+)++useBifoldl1 :: (Int, Int) -> Int+useBifoldl1 = bifoldl1 (+)++useFromJust :: Maybe Int -> Int+useFromJust = fromJust++useReadFile :: IO String+useReadFile = readFile "foo.txt"++useSingular :: [Int] -> Int+useSingular xs = xs ^. singular traverse++useStringException :: StringException+useStringException = stringException "a"++-- These should NOT trigger warnings+useHeadMay :: [Int] -> Maybe Int+useHeadMay = headMay++useCatch :: IO ()+useCatch = catch (pure ()) (\(_ :: IOException) -> pure ())
+ test/HlintSamples/ReexportImportForms.hs view
@@ -0,0 +1,37 @@+{-# OPTIONS_GHC -Wno-x-partial #-}++-- Sample file to test how hlint rules for re-exported symbols interact with import forms.+-- `head` and `last` are hidden from Himari,+-- so those unqualified names refer to something else here.+module HlintSamples.ReexportImportForms where++import Data.List.NonEmpty (head)+import Data.List.NonEmpty qualified as NE+import Himari hiding (head, last)+import Himari qualified as H++-- Qualified use through Himari (should warn)+useQualifiedHead :: [Int] -> Int+useQualifiedHead = H.head++useQualifiedReadFile :: IO String+useQualifiedReadFile = H.readFile "foo.txt"++-- Hiding some names does not hide the others (should warn)+useTail :: [Int] -> [Int]+useTail = tail++-- `head` is the total one of Data.List.NonEmpty+-- because the one of Himari is hidden (should NOT warn)+useNonEmptyHead :: NonEmpty Int -> Int+useNonEmptyHead = head++useNonEmptyHeadQualified :: NonEmpty Int -> Int+useNonEmptyHeadQualified = NE.head++-- A total function defined by the user under the name hidden from Himari (should NOT warn)+last :: [Int] -> Maybe Int+last = lastMay++useOwnLast :: [Int] -> Maybe Int+useOwnLast = last
+ test/HlintSamples/ReexportSafePrelude.hs view
@@ -0,0 +1,30 @@+{-# OPTIONS_GHC -Wno-x-partial #-}++-- Sample file to test that hlint rules match symbols re-exported through Himari.SafePrelude.+-- Himari.SafePrelude is the subset of himari that can be imported under Safe Haskell.+module HlintSamples.ReexportSafePrelude where++import Himari.SafePrelude++-- These should trigger warnings+useHead :: [Int] -> Int+useHead = head++useFoldl1 :: [Int] -> Int+useFoldl1 = foldl1 (+)++useBifoldl1 :: (Int, Int) -> Int+useBifoldl1 = bifoldl1 (+)++useFromJust :: Maybe Int -> Int+useFromJust = fromJust++useReadFile :: IO String+useReadFile = readFile "foo.txt"++useSingular :: [Int] -> Int+useSingular xs = xs ^. singular traverse++-- These should NOT trigger warnings+useHeadMay :: [Int] -> Maybe Int+useHeadMay = headMay
test/HlintUnliftioSpec.hs view
@@ -1,45 +1,33 @@ module HlintUnliftioSpec (spec) where -import Data.Text qualified as T import Himari+import HlintRunner import Test.Syd spec :: Spec spec = do- describe "hlint unliftio preference rules" . beforeAll runHlintOnSample $ do+ describe "hlint unliftio preference rules" . withSample "UnliftioPreference.hs" $ do describe "Control" $ do describe "Exception" $ do- itWithOuter "catch should warn to use UnliftIO.Exception" $ \output -> do- output `shouldSatisfy` containsWarning "UnliftIO.Exception.catch"+ itWithOuter "catch should warn to use UnliftIO.Exception" $ \sample ->+ shouldWarnFunction sample "badCatch" "Exception.catch" "UnliftIO.Exception.catch" - itWithOuter "bracket should warn to use UnliftIO.Exception" $ \output -> do- output `shouldSatisfy` containsWarning "UnliftIO.Exception.bracket"+ itWithOuter "bracket should warn to use UnliftIO.Exception" $ \sample ->+ shouldWarnFunction sample "badBracket" "Exception.bracket" "UnliftIO.Exception.bracket" - itWithOuter "finally should warn to use UnliftIO.Exception" $ \output -> do- output `shouldSatisfy` containsWarning "UnliftIO.Exception.finally"+ itWithOuter "finally should warn to use UnliftIO.Exception" $ \sample ->+ shouldWarnFunction sample "badFinally" "Exception.finally" "UnliftIO.Exception.finally" - itWithOuter "try should warn to use UnliftIO.Exception" $ \output -> do- output `shouldSatisfy` containsWarning "UnliftIO.Exception.try"+ itWithOuter "try should warn to use UnliftIO.Exception" $ \sample ->+ shouldWarnFunction sample "badTry" "Exception.try" "UnliftIO.Exception.try" describe "Concurrent.MVar" $ do- itWithOuter "withMVar should warn to use UnliftIO.MVar" $ \output -> do- output `shouldSatisfy` containsWarning "UnliftIO.MVar.withMVar"+ itWithOuter "withMVar should warn to use UnliftIO.MVar" $ \sample ->+ shouldWarnFunction sample "badWithMVar" "MVar.withMVar" "UnliftIO.MVar.withMVar" - itWithOuter "modifyMVar_ should warn to use UnliftIO.MVar" $ \output -> do- output `shouldSatisfy` containsWarning "UnliftIO.MVar.modifyMVar_"+ itWithOuter "modifyMVar_ should warn to use UnliftIO.MVar" $ \sample ->+ shouldWarnFunction sample "badModifyMVar" "MVar.modifyMVar_" "UnliftIO.MVar.modifyMVar_" describe "System.Timeout" $ do- itWithOuter "timeout should warn to use UnliftIO.Timeout" $ \output -> do- output `shouldSatisfy` containsWarning "UnliftIO.Timeout.timeout"--runHlintOnSample :: IO Text-runHlintOnSample = do- (exitCode, stdoutOutput, stderrOutput) <-- readProcess $- proc "hlint" ["test/HlintSamples/UnliftioPreference.hs"]- pure $ case exitCode of- ExitSuccess -> convert stdoutOutput- ExitFailure _ -> convert stdoutOutput <> convert stderrOutput--containsWarning :: Text -> Text -> Bool-containsWarning expectedMsg output = expectedMsg `T.isInfixOf` output+ itWithOuter "timeout should warn to use UnliftIO.Timeout" $ \sample ->+ shouldWarnFunction sample "badTimeout" "Timeout.timeout" "UnliftIO.Timeout.timeout"
test/Main.hs view
@@ -6,6 +6,7 @@ import Himari.Prelude.FunctorSpec qualified as FunctorSpec import HlintBaseSpec qualified import HlintExtensionSpec qualified+import HlintReexportSpec qualified import HlintUnliftioSpec qualified import Test.Syd import TitleSpec qualified@@ -19,3 +20,4 @@ describe "HlintUnliftio" HlintUnliftioSpec.spec describe "HlintBase" HlintBaseSpec.spec describe "HlintExtension" HlintExtensionSpec.spec+ describe "HlintReexport" HlintReexportSpec.spec