Skip to content

Commit 2bc6310

Browse files
Hlint hints. (haskell#1227)
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 7d3cf87 commit 2bc6310

File tree

25 files changed

+57
-55
lines changed

25 files changed

+57
-55
lines changed

GenChangelogs.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ main = do
2424

2525
prs <- github' $ pullRequestsForR "haskell" "haskell-language-server" stateClosed FetchAll
2626
let prsAfterLastTag = either (error . show)
27-
(foldMap (\pr -> if inRange pr then [pr] else []))
27+
(foldMap (\pr -> [pr | inRange pr]))
2828
prs
2929
inRange pr
3030
| Just mergedDate <- simplePullRequestMergedAt pr = mergedDate > lastDate

ghcide/.hlint.yaml

+16-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
- ignore: {name: "Redundant do"}
1212
- ignore: {name: "Avoid lambda"}
1313
- ignore: {name: "Use newtype instead of data"}
14-
- ignore: {name: "Use fromMaybe"}
1514
- ignore: {name: "Use unless"}
1615
- ignore: {name: "Move brackets to avoid $"}
1716
- ignore: {name: "Eta reduce"}
@@ -25,6 +24,21 @@
2524
- ignore: {name: "Use uncurry"}
2625
- ignore: {name: "Avoid lambda using `infix`"}
2726

27+
# Gives at least one suggestion we don't like.
28+
- ignore: {name: "Use <=<"}
29+
- ignore: {name: "Use zipFrom"}
30+
- ignore: {name: "Use zipWithFrom"}
31+
32+
# We are using the "redundant" return/pure to assign a name. We do not want to
33+
# delete it. In particular, this is not an improvement:
34+
# Found:
35+
# do options <- somethingComplicated
36+
# pure options
37+
# Perhaps:
38+
# do somethingComplicated
39+
- ignore: {name: "Redundant return"}
40+
- ignore: {name: "Redundant pure"}
41+
2842
# Off by default hints we like
2943
- warn: {name: Use module export list}
3044

@@ -107,7 +121,7 @@
107121
# Things that are unsafe in Haskell base library
108122
- {name: unsafeInterleaveIO, within: [Development.IDE.LSP.LanguageServer]}
109123
- {name: unsafeDupablePerformIO, within: []}
110-
- {name: unsafeCoerce, within: []}
124+
- {name: unsafeCoerce, within: [Ide.Plugin.Eval.Code]}
111125
# Things that are a bit dangerous in the GHC API
112126
- {name: nameModule, within: []}
113127

ghcide/session-loader/Development/IDE/Session.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ getInitialGhcLibDirDefault = do
123123
hPutStrLn stderr $ "Couldn't load cradle for libdir: " ++ show (err,dir,hieYaml,cradle)
124124
pure Nothing
125125
CradleNone -> do
126-
hPutStrLn stderr $ "Couldn't load cradle (CradleNone)"
126+
hPutStrLn stderr "Couldn't load cradle (CradleNone)"
127127
pure Nothing
128128

129129
-- | Sets `unsafeGlobalDynFlags` on using the hie-bios cradle and returns the GHC libdir

ghcide/src/Development/IDE/GHC/ExactPrint.hs

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
{-# LANGUAGE RankNTypes #-}
66
{-# LANGUAGE TypeFamilies #-}
77

8-
{- HLINT ignore "Use zipFrom" -}
9-
108
module Development.IDE.GHC.ExactPrint
119
( Graft(..),
1210
graft,

ghcide/src/Development/IDE/Spans/AtPoint.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ documentHighlight hf rf pos = pure highlights
158158
ns = concat $ pointCommand hf pos (rights . M.keys . nodeIdentifiers . nodeInfo)
159159
highlights = do
160160
n <- ns
161-
ref <- maybe [] id (M.lookup (Right n) rf)
161+
ref <- fromMaybe [] (M.lookup (Right n) rf)
162162
pure $ makeHighlight ref
163163
makeHighlight (sp,dets) =
164164
DocumentHighlight (realSrcSpanToRange sp) (Just $ highlightType $ identInfo dets)
@@ -266,12 +266,12 @@ typeLocationsAtPoint hiedb lookupModule _ideOptions pos (HAR _ ast _ _ hieKind)
266266
HQualTy a b -> getTypes [a,b]
267267
HCastTy a -> getTypes [a]
268268
_ -> []
269-
in fmap nubOrd $ concatMapM (fmap (maybe [] id) . nameToLocation hiedb lookupModule) (getTypes ts)
269+
in fmap nubOrd $ concatMapM (fmap (fromMaybe []) . nameToLocation hiedb lookupModule) (getTypes ts)
270270
HieFresh ->
271271
let ts = concat $ pointCommand ast pos getts
272272
getts x = nodeType ni ++ (mapMaybe identType $ M.elems $ nodeIdentifiers ni)
273273
where ni = nodeInfo x
274-
in fmap nubOrd $ concatMapM (fmap (maybe [] id) . nameToLocation hiedb lookupModule) (getTypes ts)
274+
in fmap nubOrd $ concatMapM (fmap (fromMaybe []) . nameToLocation hiedb lookupModule) (getTypes ts)
275275

276276
namesInType :: Type -> [Name]
277277
namesInType (TyVarTy n) = [Var.varName n]

hls-plugin-api/src/Ide/PluginUtils.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ clientSupportsDocumentChanges caps =
135135
WorkspaceEditClientCapabilities mDc _ _ <- _workspaceEdit wCaps
136136
mDc
137137
in
138-
fromMaybe False supports
138+
Just True == supports
139139

140140
-- ---------------------------------------------------------------------
141141

@@ -214,7 +214,7 @@ allLspCmdIds' pid mp = mkPlugin (allLspCmdIds pid) (Just . pluginCommands)
214214

215215

216216
allLspCmdIds :: T.Text -> [(PluginId, [PluginCommand ideState])] -> [T.Text]
217-
allLspCmdIds pid commands = concat $ map go commands
217+
allLspCmdIds pid commands = concatMap go commands
218218
where
219219
go (plid, cmds) = map (mkLspCmdId pid plid . commandId) cmds
220220

hls-plugin-api/src/Ide/Types.hs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{-# LANGUAGE GADTs #-}
22
{-# LANGUAGE OverloadedStrings #-}
33
{-# LANGUAGE ScopedTypeVariables #-}
4-
{-# LANGUAGE TupleSections #-}
54
{-# LANGUAGE FlexibleContexts #-}
65
{-# LANGUAGE PolyKinds #-}
76
{-# LANGUAGE ViewPatterns #-}

install/src/Cabal.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ getProjectFile ver = do
9696
else "cabal.project"
9797

9898
checkCabal_ :: [String] -> Action ()
99-
checkCabal_ args = checkCabal args >> return ()
99+
checkCabal_ args = void $ checkCabal args
100100

101101
-- | check `cabal` has the required version
102102
checkCabal :: [String] -> Action String

install/src/Env.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ findInstalledGhcs = do
5454
-- sort by version to make it coherent with getHlsVersions
5555
$ sortBy (comparing fst)
5656
-- nub by version. knownGhcs takes precedence.
57-
$ nubBy ((==) `on` fst)
57+
$ nubOrdBy (compare `on` fst)
5858
-- filter out stack provided GHCs (assuming that stack programs path is the default one in linux)
5959
$ filter (not . isInfixOf ".stack" . snd) (knownGhcs ++ availableGhcs)
6060

install/src/Print.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ printInStars = liftIO . putStrLn . embedInStars
2525

2626
-- | Trim whitespace of both ends of a string
2727
trim :: String -> String
28-
trim = dropWhileEnd isSpace . dropWhile isSpace
28+
trim = trimEnd . trimStart
2929

3030
-- | Trim the whitespace of the stdout of a command
3131
trimmedStdout :: Stdout String -> String

plugins/default/src/Ide/Plugin/Fourmolu.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ provider ideState typ contents fp fo = withIndefiniteProgress title Cancellable
9393

9494
convertDynFlags :: DynFlags -> IO [DynOption]
9595
convertDynFlags df =
96-
let pp = if null p then [] else ["-pgmF=" <> p]
96+
let pp = ["-pgmF=" <> p | not (null p)]
9797
p = D.sPgm_F $ D.settings df
9898
pm = map (("-fplugin=" <>) . moduleNameString) $ D.pluginModNames df
9999
ex = map showExtension $ S.toList $ D.extensionFlags df

plugins/default/src/Ide/Plugin/Ormolu.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
4444
let
4545
pp =
4646
let p = D.sPgm_F $ D.settings df
47-
in if null p then [] else ["-pgmF=" <> p]
47+
in ["-pgmF=" <> p | not (null p)]
4848
pm = map (("-fplugin=" <>) . moduleNameString) $ D.pluginModNames df
4949
ex = map showExtension $ S.toList $ D.extensionFlags df
5050
in

plugins/default/src/Ide/Plugin/Pragmas.hs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
{-# LANGUAGE DeriveAnyClass #-}
2-
{-# LANGUAGE DeriveGeneric #-}
31
{-# LANGUAGE DuplicateRecordFields #-}
42
{-# LANGUAGE OverloadedStrings #-}
53
{-# LANGUAGE ViewPatterns #-}
@@ -12,6 +10,7 @@ module Ide.Plugin.Pragmas
1210

1311
import Control.Lens hiding (List)
1412
import qualified Data.HashMap.Strict as H
13+
import Data.Maybe (catMaybes)
1514
import qualified Data.Text as T
1615
import Development.IDE as D
1716
import Ide.Types
@@ -78,7 +77,7 @@ codeActionProvider state _plId (CodeActionParams _ _ docId _ (J.CodeActionContex
7877
disabled
7978
| Just dynFlags <- mDynflags
8079
-- GHC does not export 'OnOff', so we have to view it as string
81-
= [ e | Just e <- T.stripPrefix "Off " . T.pack . prettyPrint <$> extensions dynFlags]
80+
= catMaybes $ T.stripPrefix "Off " . T.pack . prettyPrint <$> extensions dynFlags
8281
| otherwise
8382
-- When the module failed to parse, we don't have access to its
8483
-- dynFlags. In that case, simply don't disable any pragmas.

plugins/hls-eval-plugin/src/Ide/Plugin/Eval/CodeLens.hs

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
{-# LANGUAGE TypeApplications #-}
22
{-# LANGUAGE ViewPatterns #-}
33
{-# LANGUAGE LambdaCase #-}
4-
{-# LANGUAGE DeriveAnyClass #-}
5-
{-# LANGUAGE DeriveGeneric #-}
64
{-# LANGUAGE DuplicateRecordFields #-}
75
{-# LANGUAGE ExtendedDefaultRules #-}
86
{-# LANGUAGE FlexibleContexts #-}

plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Util.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Ide.Plugin.Eval.Util (
1515
logWith,
1616
) where
1717

18+
import Control.Monad.Extra (maybeM)
1819
import Control.Monad.IO.Class (MonadIO (liftIO))
1920
import Control.Monad.Trans.Class (lift)
2021
import Control.Monad.Trans.Except (
@@ -84,7 +85,7 @@ handleMaybe :: Monad m => e -> Maybe b -> ExceptT e m b
8485
handleMaybe msg = maybe (throwE msg) return
8586

8687
handleMaybeM :: Monad m => e -> m (Maybe b) -> ExceptT e m b
87-
handleMaybeM msg act = maybe (throwE msg) return =<< lift act
88+
handleMaybeM msg act = maybeM (throwE msg) return $ lift act
8889

8990
response :: Functor f => ExceptT String f c -> f (Either ResponseError c)
9091
response =

plugins/hls-hlint-plugin/src/Ide/Plugin/Hlint.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ codeActionProvider ideState plId (CodeActionParams _ _ docId _ context) = Right
278278
in LSP.CodeAction "Apply all hints" (Just LSP.CodeActionQuickFix) Nothing Nothing Nothing Nothing (Just cmd)
279279

280280
applyOneActions :: [LSP.CodeAction]
281-
applyOneActions = catMaybes $ map mkHlintAction (filter validCommand diags)
281+
applyOneActions = mapMaybe mkHlintAction (filter validCommand diags)
282282

283283
-- |Some hints do not have an associated refactoring
284284
validCommand (LSP.Diagnostic _ _ (Just (InR code)) (Just "hlint") _ _ _) =

plugins/hls-retrie-plugin/src/Ide/Plugin/Retrie.hs

+10-12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Control.Concurrent.Extra (readVar)
2020
import Control.Exception.Safe (Exception (..), SomeException,
2121
catch, throwIO, try)
2222
import Control.Monad (forM, unless)
23+
import Control.Monad.Extra (maybeM)
2324
import Control.Monad.IO.Class (MonadIO (liftIO))
2425
import Control.Monad.Trans.Class (MonadTrans (lift))
2526
import Control.Monad.Trans.Except (ExceptT (..), runExceptT,
@@ -146,14 +147,14 @@ extractImports ModSummary{ms_mod} topLevelBinds (Unfold thing)
146147
, names <- listify p fun_matches
147148
=
148149
[ AddImport {..}
149-
| name <- names,
150-
Just ideclNameString <-
151-
[moduleNameString . GHC.moduleName <$> nameModule_maybe name],
152-
let ideclSource = False,
150+
| let ideclSource = False,
151+
name <- names,
153152
let r = nameRdrName name,
154153
let ideclQualifiedBool = isQual r,
155154
let ideclAsString = moduleNameString . fst <$> isQual_maybe r,
156-
let ideclThing = Just (IEVar $ occNameString $ rdrNameOcc r)
155+
let ideclThing = Just (IEVar $ occNameString $ rdrNameOcc r),
156+
Just ideclNameString <-
157+
[moduleNameString . GHC.moduleName <$> nameModule_maybe name]
157158
]
158159
where
159160
p name = nameModule_maybe name /= Just ms_mod
@@ -178,8 +179,8 @@ provider state plId (CodeActionParams _ _ (TextDocumentIdentifier uri) range ca)
178179
++ [ r
179180
| TyClGroup {group_tyclds} <- hs_tyclds,
180181
L l g <- group_tyclds,
181-
r <- suggestTypeRewrites uri ms_mod g,
182-
pos `isInsideSrcSpan` l
182+
pos `isInsideSrcSpan` l,
183+
r <- suggestTypeRewrites uri ms_mod g
183184

184185
]
185186

@@ -235,7 +236,6 @@ suggestBindRewrites originatingFile pos ms_mod FunBind {fun_id = L l' rdrName}
235236
description = "Fold " <> pprNameText <> describeRestriction restrictToOriginatingFile
236237
in (description, CodeActionRefactorExtract, RunRetrieParams {..})
237238
in [unfoldRewrite False, unfoldRewrite True, foldRewrite False, foldRewrite True]
238-
where
239239
suggestBindRewrites _ _ _ _ = []
240240

241241
describeRestriction :: IsString p => Bool -> p
@@ -409,9 +409,7 @@ callRetrie state session rewrites origin restrictToOriginatingFile = do
409409
-- TODO add the imports to the resulting edits
410410
(_user, ast, change@(Change _replacements _imports)) <-
411411
lift $ runRetrie fixityEnv retrie cpp
412-
case ast of
413-
_ ->
414-
return $ asTextEdits change
412+
return $ asTextEdits change
415413

416414
let (errors :: [CallRetrieError], replacements) = partitionEithers results
417415
editParams :: WorkspaceEdit
@@ -485,7 +483,7 @@ handleMaybe :: Monad m => e -> Maybe b -> ExceptT e m b
485483
handleMaybe msg = maybe (throwE msg) return
486484

487485
handleMaybeM :: Monad m => e -> m (Maybe b) -> ExceptT e m b
488-
handleMaybeM msg act = maybe (throwE msg) return =<< lift act
486+
handleMaybeM msg act = maybeM (throwE msg) return $ lift act
489487

490488
response :: Monad m => ExceptT String m a -> m (Either ResponseError a)
491489
response =

plugins/hls-splice-plugin/hls-splice-plugin.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ library
1818
build-depends: aeson
1919
, base >=4.12 && <5
2020
, containers
21+
, extra
2122
, foldl
2223
, lsp
2324
, hls-plugin-api

plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs

+5-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import Control.Arrow
2525
import qualified Control.Foldl as L
2626
import Control.Lens (ix, view, (%~), (<&>), (^.))
2727
import Control.Monad
28+
import Control.Monad.Extra (eitherM)
2829
import qualified Control.Monad.Fail as Fail
2930
import Control.Monad.Trans.Class
3031
import Control.Monad.Trans.Except
@@ -324,8 +325,8 @@ manualCalcEdit clientCapabilities reportEditor ran ps hscEnv typechkd srcSpan _e
324325
graftDeclsWithM (RealSrcSpan srcSpan) $ \case
325326
(L _spn (SpliceD _ (SpliceDecl _ (L _ spl) _))) -> do
326327
eExpr <-
327-
either (fail . show) pure
328-
=<< lift
328+
eitherM (fail . show) pure
329+
$ lift
329330
( lift $
330331
gtry @_ @SomeException $
331332
(fst <$> rnTopSpliceDecls spl)
@@ -337,8 +338,8 @@ manualCalcEdit clientCapabilities reportEditor ran ps hscEnv typechkd srcSpan _e
337338
graftWithM (RealSrcSpan srcSpan) $ \case
338339
(L _spn (matchSplice astP -> Just spl)) -> do
339340
eExpr <-
340-
either (fail . show) pure
341-
=<< lift
341+
eitherM (fail . show) pure
342+
$ lift
342343
( lift $
343344
gtry @_ @SomeException $
344345
(fst <$> expandSplice astP spl)

src/Ide/Main.hs

-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
{-# LANGUAGE OverloadedStrings #-}
66
{-# LANGUAGE RecordWildCards #-}
77
{-# LANGUAGE ScopedTypeVariables #-}
8-
{-# LANGUAGE TupleSections #-}
98
{-# LANGUAGE TypeFamilies #-}
10-
{-# LANGUAGE ViewPatterns #-}
11-
{-# LANGUAGE NamedFieldPuns #-}
129

1310
module Ide.Main(defaultMain, runLspMode) where
1411

test/functional/Progress.hs

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
{-# LANGUAGE OverloadedStrings #-}
44
{-# LANGUAGE DuplicateRecordFields #-}
55
{-# LANGUAGE DataKinds #-}
6-
{-# LANGUAGE TypeOperators #-}
76
{-# LANGUAGE GADTs #-}
87

98
module Progress (tests) where
@@ -106,9 +105,9 @@ expectProgressReports xs = expectProgressReports' [] xs
106105
EndM msg -> do
107106
liftIO $ token msg `expectElem` tokens
108107
expectProgressReports' (delete (token msg) tokens) expectedTitles
109-
title msg = msg ^. L.value ^. L.title
108+
title msg = msg ^. L.value . L.title
110109
token msg = msg ^. L.token
111-
create = CreateM . view L.params <$> (message SWindowWorkDoneProgressCreate)
110+
create = CreateM . view L.params <$> message SWindowWorkDoneProgressCreate
112111
begin = BeginM <$> satisfyMaybe (\case
113112
FromServerMess SProgress (NotificationMessage _ _ (ProgressParams t (Begin x))) -> Just (ProgressParams t x)
114113
_ -> Nothing)

test/functional/Reference.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ tests = testGroup "references" [
2525
, mkRange 4 14 4 17
2626
, mkRange 4 0 4 3
2727
, mkRange 2 6 2 9
28-
] `isInfixOf` (coerce refs) @? "Contains references"
28+
] `isInfixOf` coerce refs @? "Contains references"
2929
-- TODO: Respect withDeclaration parameter
3030
-- ignoreTestBecause "Broken" $ testCase "works without definitions" $ runSession hlsCommand fullCaps "test/testdata" $ do
3131
-- doc <- openDoc "References.hs" "haskell"

test/functional/Tactic.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ mkTest name fp line col ts =
150150
for_ ts $ \(f, tc, var) -> do
151151
let title = tacticTitle tc var
152152
liftIO $
153-
f (elem title titles)
153+
f (title `elem` titles)
154154
@? ("Expected a code action with title " <> T.unpack title)
155155

156156

test/utils/Test/Hls/Util.hs

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ logFilePath = "hls-" ++ show ghcVersion ++ ".log"
173173
-- on PATH. Cabal seems to respond to @build-tool-depends@ specifically while
174174
-- stack just puts all project executables on PATH.
175175
hlsCommand :: String
176+
{-# NOINLINE hlsCommand #-}
176177
hlsCommand = unsafePerformIO $ do
177178
testExe <- fromMaybe "haskell-language-server" <$> lookupEnv "HLS_TEST_EXE"
178179
pure $ testExe ++ " --lsp -d -j2 -l test-logs/" ++ logFilePath

0 commit comments

Comments
 (0)