Use a pattern match instead of null and head in checkCommand

This commit is contained in:
Joseph C. Sible 2023-12-29 14:18:42 -05:00
parent dab77b2c8d
commit 3bd7df955b
1 changed files with 10 additions and 10 deletions

View File

@ -20,6 +20,7 @@
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiWayIf #-} {-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE PatternGuards #-}
-- This module contains checks that examine specific commands by name. -- This module contains checks that examine specific commands by name.
module ShellCheck.Checks.Commands (checker, optionalChecks, ShellCheck.Checks.Commands.runTests) where module ShellCheck.Checks.Commands (checker, optionalChecks, ShellCheck.Checks.Commands.runTests) where
@ -181,14 +182,13 @@ checkCommand :: M.Map CommandName (Token -> Analysis) -> Token -> Analysis
checkCommand map t@(T_SimpleCommand id cmdPrefix (cmd:rest)) = sequence_ $ do checkCommand map t@(T_SimpleCommand id cmdPrefix (cmd:rest)) = sequence_ $ do
name <- getLiteralString cmd name <- getLiteralString cmd
return $ return $
if '/' `elem` name if | '/' `elem` name ->
then
M.findWithDefault nullCheck (Basename $ basename name) map t M.findWithDefault nullCheck (Basename $ basename name) map t
else if name == "builtin" && not (null rest) then | name == "builtin", (h:_) <- rest ->
let t' = T_SimpleCommand id cmdPrefix rest let t' = T_SimpleCommand id cmdPrefix rest
selectedBuiltin = onlyLiteralString $ head rest selectedBuiltin = onlyLiteralString h
in M.findWithDefault nullCheck (Exactly selectedBuiltin) map t' in M.findWithDefault nullCheck (Exactly selectedBuiltin) map t'
else do | otherwise -> do
M.findWithDefault nullCheck (Exactly name) map t M.findWithDefault nullCheck (Exactly name) map t
M.findWithDefault nullCheck (Basename name) map t M.findWithDefault nullCheck (Basename name) map t