Skip to content

Commit 3cb264c

Browse files
authored
Merge pull request #184 from NixOS/hlint
Enforce hlint
2 parents 35b0186 + 54b4fbf commit 3cb264c

File tree

12 files changed

+90
-90
lines changed

12 files changed

+90
-90
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ jobs:
2020
- name: reuse lint
2121
run: nix-build -A packages.reuse && result/bin/reuse lint
2222

23+
- name: hlint
24+
run: nix-build -A checks.hlint
25+
2326
- name: build nixfmt
2427
run: nix-build
2528
if: success() || failure()

default.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ build
5555
haskellPackages.haskell-language-server
5656
shellcheck
5757
npins
58+
hlint
5859
];
5960
};
6061

6162
checks = {
62-
hlint = pkgs.build.haskell.hlint ./.;
63+
hlint = pkgs.build.haskell.hlint src;
6364
stylish-haskell = pkgs.build.haskell.stylish-haskell ./.;
6465
};
6566
}

main/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ checkTarget format Target{tDoRead, tPath} = do
9494
| otherwise -> Left $ tPath ++ ": not formatted"
9595

9696
stdioTarget :: Target
97-
stdioTarget = Target TextIO.getContents "<stdin>" (const $ TextIO.putStr)
97+
stdioTarget = Target TextIO.getContents "<stdin>" (const TextIO.putStr)
9898

9999
fileTarget :: FilePath -> Target
100100
fileTarget path = Target (readFileUtf8 path) path atomicWriteFile

main/System/IO/Utf8.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ withUtf8StdHandles :: IO a -> IO a
6161
withUtf8StdHandles action =
6262
withConfiguredHandle stdin $
6363
withConfiguredHandle stdout $
64-
withConfiguredHandle stderr $
65-
action
64+
withConfiguredHandle stderr action
6665
where
6766
withConfiguredHandle :: IO.Handle -> IO a -> IO a
6867
withConfiguredHandle h = bracket (hSetBestUtf8Enc h) ($ h) . const

src/Nixfmt.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,16 @@ formatVerify :: Width -> FilePath -> Text -> Either String Text
3939
formatVerify width path unformatted = do
4040
unformattedParsed@(Whole unformattedParsed' _) <- parse unformatted
4141
let formattedOnce = layout width unformattedParsed
42-
formattedOnceParsed <- flip first (parse formattedOnce) $
43-
(\x -> pleaseReport "Fails to parse after formatting.\n" <> x <> "\n\nAfter Formatting:\n" <> unpack formattedOnce)
42+
formattedOnceParsed <- first (\x -> pleaseReport "Fails to parse after formatting.\n" <> x <> "\n\nAfter Formatting:\n" <> unpack formattedOnce) (parse formattedOnce)
4443
let formattedTwice = layout width formattedOnceParsed
4544
if formattedOnceParsed /= unformattedParsed
4645
then Left $
4746
let
4847
minimized = minimize unformattedParsed' (\e -> parse (layout width e) == Right (Whole e []))
4948
in
5049
pleaseReport "Parses differently after formatting."
51-
<> "\n\nBefore formatting:\n" <> (show minimized)
52-
<> "\n\nAfter formatting:\n" <> (show $ fromRight (error "TODO") $ parse (layout width minimized))
50+
<> "\n\nBefore formatting:\n" <> show minimized
51+
<> "\n\nAfter formatting:\n" <> show (fromRight (error "TODO") $ parse (layout width minimized))
5352
else if formattedOnce /= formattedTwice
5453
then Left $
5554
let
@@ -67,6 +66,6 @@ formatVerify width path unformatted = do
6766

6867
minimize :: Expression -> (Expression -> Bool) -> Expression
6968
minimize expr test =
70-
case concatMap (\e -> case test e of { False -> [minimize e test]; True -> [] }) $ walkSubprograms expr of
69+
case concatMap (\e -> ([minimize e test | not (test e)])) $ walkSubprograms expr of
7170
result:_ -> result
7271
[] -> expr

src/Nixfmt/Lexer.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE BlockArguments, FlexibleContexts, LambdaCase, OverloadedStrings, TupleSections #-}
1+
{-# LANGUAGE BlockArguments, FlexibleContexts, LambdaCase, OverloadedStrings #-}
22

33
module Nixfmt.Lexer (lexeme, pushTrivia, takeTrivia, whole) where
44

@@ -47,7 +47,7 @@ blockComment = try $ preLexeme $ do
4747
let pos' = unPos pos - 1
4848
_ <- chunk "/*"
4949
-- Try to parse /** before /*, but don't parse /**/ (i.e. the empty comment)
50-
isDoc <- try (const True <$> char '*' <* notFollowedBy (char '/')) <|> pure False
50+
isDoc <- try ((True <$ char '*') <* notFollowedBy (char '/')) <|> pure False
5151

5252
chars <- manyTill anySingle $ chunk "*/"
5353
return $ PTBlockComment isDoc $ dropWhile Text.null $ fixIndent pos' $ removeStars pos' $ splitLines $ pack chars
@@ -83,7 +83,7 @@ blockComment = try $ preLexeme $ do
8383
stripIndentation n t = fromMaybe (stripStart t) $ stripPrefix (Text.replicate n " ") t
8484

8585
commonIndentationLength :: Int -> [Text] -> Int
86-
commonIndentationLength def = foldr min def . map (Text.length . Text.takeWhile (==' '))
86+
commonIndentationLength = foldr (min . Text.length . Text.takeWhile (==' '))
8787

8888
-- This should be called with zero or one elements, as per `span isTrailing`
8989
convertTrailing :: [ParseTrivium] -> Maybe TrailingComment
@@ -119,7 +119,7 @@ convertTrivia pts nextCol =
119119
-- This happens especially often after `{` or `[` tokens, where the comment of the first item
120120
-- starts on the same line ase the opening token.
121121
([PTLineComment _ pos], (PTNewlines 1):(PTLineComment _ pos'):_) | pos == pos' -> (Nothing, convertLeading pts)
122-
([PTLineComment _ pos], [(PTNewlines 1)]) | pos == nextCol -> (Nothing, convertLeading pts)
122+
([PTLineComment _ pos], [PTNewlines 1]) | pos == nextCol -> (Nothing, convertLeading pts)
123123
_ -> (convertTrailing trailing, convertLeading leading)
124124

125125
trivia :: Parser [ParseTrivium]

src/Nixfmt/Parser.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ parens = Parenthesized <$>
227227

228228
simpleSelector :: Parser StringPart -> Parser SimpleSelector
229229
simpleSelector parseInterpolation =
230-
((IDSelector <$> identifier) <|>
230+
(IDSelector <$> identifier) <|>
231231
(InterpolSelector <$> lexeme parseInterpolation) <|>
232-
(StringSelector <$> lexeme simpleString))
232+
(StringSelector <$> lexeme simpleString)
233233

234234
selector :: Maybe (Parser Leaf) -> Parser Selector
235235
selector parseDot = Selector <$>
@@ -246,7 +246,7 @@ selectorPath' = many $ try $ selector $ Just $ symbol TDot
246246
-- Everything but selection
247247
simpleTerm :: Parser Term
248248
simpleTerm =
249-
(SimpleString <$> (lexeme $ simpleString <|> uri))
249+
(SimpleString <$> lexeme (simpleString <|> uri))
250250
<|> (IndentedString <$> lexeme indentedString)
251251
<|> (Path <$> path)
252252
<|> (Token <$> (envPath <|> float <|> integer <|> identifier))

src/Nixfmt/Parser/Float.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import "scientific" Data.Scientific (toRealFloat, scientific)
2020
data SP = SP !Integer {-# UNPACK #-} !Int
2121
floatParse :: (MonadParsec e s m, Token s ~ Char, RealFloat a) => m a
2222
floatParse = do
23-
notFollowedBy $ (char '0') >> digitChar
24-
notFollowedBy $ (char' 'e')
23+
notFollowedBy $ char '0' >> digitChar
24+
notFollowedBy (char' 'e')
2525
c' <- (decimal <?> "decimal") <|> return 0
2626
toRealFloat
2727
<$> (( do

src/Nixfmt/Predoc.hs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE FlexibleInstances, OverloadedStrings, LambdaCase #-}
1+
{-# LANGUAGE FlexibleInstances, OverloadedStrings #-}
22

33
module Nixfmt.Predoc
44
( text
@@ -151,7 +151,7 @@ trailing t = [Text 0 0 Trailing t]
151151
-- Must not contain non-hard whitespace (e.g. line, softline' etc.) at the start of the end.
152152
-- Use group' for that instead if you are sure of what you are doing.
153153
group :: HasCallStack => Pretty a => a -> Doc
154-
group x = pure . (Group RegularG) $
154+
group x = pure . Group RegularG $
155155
if p /= [] && (isSoftSpacing (head p) || isSoftSpacing (last p)) then
156156
error $ "group should not start or end with whitespace, use `group'` if you are sure; " <> show p
157157
else
@@ -166,7 +166,7 @@ group x = pure . (Group RegularG) $
166166
--
167167
-- Also allows to create priority groups (see Node Group documentation)
168168
group' :: Pretty a => GroupAnn -> a -> Doc
169-
group' ann = pure . (Group ann) . pretty
169+
group' ann = pure . Group ann . pretty
170170

171171
-- | @nest doc@ declarse @doc@ to have a higher nesting depth
172172
-- than before. Not all nestings actually result in indentation changes,
@@ -276,10 +276,10 @@ spanEnd p = fmap reverse . span p . reverse
276276
unexpandSpacing' :: Maybe Int -> Doc -> Maybe Doc
277277
unexpandSpacing' (Just n) _ | n < 0 = Nothing
278278
unexpandSpacing' _ [] = Just []
279-
unexpandSpacing' n (txt@(Text _ _ _ t):xs) = (txt :) <$> unexpandSpacing' (n <&> (subtract $ textWidth t)) xs
280-
unexpandSpacing' n (Spacing Hardspace:xs) = (Spacing Hardspace :) <$> unexpandSpacing' (n <&> (subtract 1)) xs
281-
unexpandSpacing' n (Spacing Space:xs) = (Spacing Hardspace :) <$> unexpandSpacing' (n <&> (subtract 1)) xs
282-
unexpandSpacing' n (Spacing Softspace:xs) = (Spacing Hardspace :) <$> unexpandSpacing' (n <&> (subtract 1)) xs
279+
unexpandSpacing' n (txt@(Text _ _ _ t):xs) = (txt :) <$> unexpandSpacing' (n <&> subtract (textWidth t)) xs
280+
unexpandSpacing' n (Spacing Hardspace:xs) = (Spacing Hardspace :) <$> unexpandSpacing' (n <&> subtract 1) xs
281+
unexpandSpacing' n (Spacing Space:xs) = (Spacing Hardspace :) <$> unexpandSpacing' (n <&> subtract 1) xs
282+
unexpandSpacing' n (Spacing Softspace:xs) = (Spacing Hardspace :) <$> unexpandSpacing' (n <&> subtract 1) xs
283283
unexpandSpacing' n (Spacing Break:xs) = unexpandSpacing' n xs
284284
unexpandSpacing' n (Spacing Softbreak:xs) = unexpandSpacing' n xs
285285
unexpandSpacing' _ (Spacing _:_) = Nothing
@@ -316,7 +316,7 @@ fixup (a@(Spacing _) : Group ann xs : ys) =
316316
-- For the leading side, also move out comments out of groups, they are kinda the same thing
317317
-- (We could move out trailing comments too but it would make no difference)
318318
(pre, rest) = span (\x -> isHardSpacing x || isComment x) $ fixup xs
319-
(post, body) = (second $ simplifyGroup ann) $ spanEnd isHardSpacing rest
319+
(post, body) = second (simplifyGroup ann) $ spanEnd isHardSpacing rest
320320
in if null body then
321321
-- Dissolve empty group
322322
fixup $ (a : pre) ++ post ++ ys
@@ -326,7 +326,7 @@ fixup (a@(Spacing _) : Group ann xs : ys) =
326326
fixup (Group ann xs : ys) =
327327
let
328328
(pre, rest) = span (\x -> isHardSpacing x || isComment x) $ fixup xs
329-
(post, body) = (second $ simplifyGroup ann) $ spanEnd isHardSpacing rest
329+
(post, body) = second (simplifyGroup ann) $ spanEnd isHardSpacing rest
330330
in if null body then
331331
fixup $ pre ++ post ++ ys
332332
else
@@ -380,7 +380,7 @@ priorityGroups = explode . mergeSegments . segments
380380
| prio = [([], x, [])]
381381
| otherwise = []
382382
explode ((prio, x):xs)
383-
| prio = ([], x, concatMap (snd) xs) : (map (\(a, b, c) -> (x<>a, b, c)) $ explode xs)
383+
| prio = ([], x, concatMap snd xs) : map (\(a, b, c) -> (x<>a, b, c)) (explode xs)
384384
| otherwise = map (\(a, b, c) -> (x<>a, b, c)) (explode xs)
385385

386386
-- | To support i18n, this function needs to be patched.
@@ -434,7 +434,7 @@ firstLineFits targetWidth maxWidth docs = go maxWidth docs
434434
where go c _ | c < 0 = False
435435
go c [] = maxWidth - c <= targetWidth
436436
go c (Text _ _ RegularT t : xs) = go (c - textWidth t) xs
437-
go c (Text _ _ _ _ : xs) = go c xs
437+
go c (Text {} : xs) = go c xs
438438
-- This case is impossible in the input thanks to fixup, but may happen
439439
-- due to our recursion on groups below
440440
go c (Spacing a : Spacing b : xs) = go c $ Spacing (mergeSpacings a b) : xs
@@ -534,7 +534,7 @@ layoutGreedy tw doc = Text.concat $ evalState (go [Group RegularG doc] []) (0, s
534534
-- [ # comment
535535
-- 1
536536
-- ]
537-
Text _ _ TrailingComment t | cc == 2 && (fst $ nextIndent xs) > lineNL -> putText' [" ", t]
537+
Text _ _ TrailingComment t | cc == 2 && fst (nextIndent xs) > lineNL -> putText' [" ", t]
538538
where lineNL = snd $ NonEmpty.head indents
539539
Text nl off _ t -> putText nl off t
540540

@@ -572,7 +572,7 @@ layoutGreedy tw doc = Text.concat $ evalState (go [Group RegularG doc] []) (0, s
572572
-- Ignore transparent groups as their priority children have already been handled up in the parent (and failed)
573573
<|> (if ann /= Transparent then
574574
-- Each priority group will be handled individually, and the priority groups are tried in reverse order
575-
asum $ map (flip goPriorityGroup xs) $ reverse $ priorityGroups ys
575+
asum $ map (`goPriorityGroup` xs) $ reverse $ priorityGroups ys
576576
else
577577
empty
578578
)
@@ -593,7 +593,7 @@ layoutGreedy tw doc = Text.concat $ evalState (go [Group RegularG doc] []) (0, s
593593
-- Try to render post onto one line
594594
postRendered <- goGroup post rest
595595
-- If none of these failed, put together and return
596-
return $ (preRendered ++ prioRendered ++ postRendered)
596+
return (preRendered ++ prioRendered ++ postRendered)
597597

598598
-- Try to fit the group onto a single line, while accounting for the fact that the first
599599
-- bits of rest must fit as well (until the first possibility for a line break within rest).
@@ -610,7 +610,7 @@ layoutGreedy tw doc = Text.concat $ evalState (go [Group RegularG doc] []) (0, s
610610
-- therefore drop any leading whitespace within the group to avoid duplicate newlines
611611
grp' = case head grp of
612612
Spacing _ -> tail grp
613-
Group ann ((Spacing _) : inner) -> (Group ann inner) : tail grp
613+
Group ann ((Spacing _) : inner) -> Group ann inner : tail grp
614614
_ -> grp
615615
(nl, off) = nextIndent grp'
616616

0 commit comments

Comments
 (0)