Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix UnusedLetBind: account for inherit #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/Nix/Linter/Checks.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Nix.Expr.Types
import Nix.Expr.Types.Annotated

import Nix.Linter.Tools
import Nix.Linter.Tools.FreeVars
import Nix.Linter.Types
import Nix.Linter.Utils (choose, sorted, (<$$>), (<&>))

Expand All @@ -35,11 +36,16 @@ checkUnusedLetBinding :: CheckBase
checkUnusedLetBinding warn e = [ (warn UnusedLetBind)
& setLoc loc
& note' varName name
| NLet_ _ binds usedIn <- [unFix e]
, (bind, others) <- choose binds
, NamedVar (StaticKey name :| []) _ loc <- [bind]
, all (noRef name) (values others)
, name `noRef` usedIn
| NLet_ _ binds _ <- [unFix e]
, free <- [freeVarsIgnoreTopBinds' e]
, bind <- binds
, (name, loc) <- case bind of
NamedVar (StaticKey name :| []) _ loc -> [(name, loc)]
Inherit _ keys loc -> do
StaticKey name <- keys
[(name, loc)]
_ -> []
, not $ Set.member name free
]

checkUnusedArg :: CheckBase
Expand Down
41 changes: 40 additions & 1 deletion src/Nix/Linter/Tools/FreeVars.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
module Nix.Linter.Tools.FreeVars (freeVars, freeVars') where
module Nix.Linter.Tools.FreeVars (freeVars, freeVars', freeVarsIgnoreTopBinds
, freeVarsIgnoreTopBinds') where
import Data.Set (Set)

import Nix.Expr.Types
import Nix.Expr.Types.Annotated
import Nix.TH (freeVars)

import qualified Data.Set as Set
import Data.Maybe (mapMaybe)
import Data.Fix (unFix, Fix (..))

freeVars' :: NExprLoc -> Set VarName
freeVars' = freeVars . stripAnnotation

freeVarsIgnoreTopBinds' :: NExprLoc -> Set VarName
freeVarsIgnoreTopBinds' = freeVarsIgnoreTopBinds . stripAnnotation

-- gets the free variables of the expression, assuming the top level bindings
-- were not bound. Note that this function is *not* recursive, since we want to
-- count bindings deeper in the tree
freeVarsIgnoreTopBinds :: NExpr -> Set VarName
freeVarsIgnoreTopBinds e =
case unFix e of
(NSet NRecursive bindings) -> bindFreeVars bindings
(NAbs (Param _) expr) -> freeVars expr
(NAbs (ParamSet set _ _) expr) ->
freeVars expr <> (Set.unions $ freeVars <$> mapMaybe snd set)
(NLet bindings expr) -> freeVars expr <> bindFreeVars bindings
noTopBinds -> freeVars $ Fix noTopBinds
where
bindFreeVars :: Foldable t => t (Binding NExpr) -> Set VarName
bindFreeVars = foldMap bind1Free
where
bind1Free :: Binding NExpr -> Set VarName
bind1Free (Inherit Nothing keys _) = Set.fromList $ mapMaybe staticKey keys
bind1Free (Inherit (Just scope) _ _) = freeVars scope
bind1Free (NamedVar path expr _) = pathFree path <> freeVars expr

staticKey :: NKeyName r -> Maybe VarName
staticKey (StaticKey varname) = pure varname
staticKey (DynamicKey _ ) = mempty

pathFree :: NAttrPath NExpr -> Set VarName
pathFree = foldMap mapFreeVars

mapFreeVars :: Foldable t => t NExpr -> Set VarName
mapFreeVars = foldMap freeVars