summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoseph C. Sible <josephcsible@users.noreply.github.com>2023-12-31 15:55:06 -0500
committerJoseph C. Sible <josephcsible@users.noreply.github.com>2023-12-31 15:55:06 -0500
commita786f996a176555ca5c09866dcb785cf7fd9323d (patch)
tree6b152ba767acb5bf40cff911d4cf629bb04f46b1
parent6e5b5401c6e593c82ce11ab15b532fe3a9c07d3b (diff)
Replace !!! with pattern-matching where it's easy
-rw-r--r--src/ShellCheck/Analytics.hs14
-rw-r--r--src/ShellCheck/Checks/Commands.hs3
-rw-r--r--src/ShellCheck/Checks/ShellSupport.hs5
3 files changed, 8 insertions, 14 deletions
diff --git a/src/ShellCheck/Analytics.hs b/src/ShellCheck/Analytics.hs
index 9926462..2fb3185 100644
--- a/src/ShellCheck/Analytics.hs
+++ b/src/ShellCheck/Analytics.hs
@@ -468,9 +468,8 @@ checkAssignAteCommand _ (T_SimpleCommand id [T_Assignment _ _ _ _ assignmentTerm
where
isCommonCommand (Just s) = s `elem` commonCommands
isCommonCommand _ = False
- firstWordIsArg list = fromMaybe False $ do
- head <- list !!! 0
- return $ isGlob head || isUnquotedFlag head
+ firstWordIsArg (head:_) = isGlob head || isUnquotedFlag head
+ firstWordIsArg [] = False
checkAssignAteCommand _ _ = return ()
@@ -491,9 +490,7 @@ prop_checkWrongArit2 = verify checkWrongArithmeticAssignment "n=2; i=n*2"
checkWrongArithmeticAssignment params (T_SimpleCommand id [T_Assignment _ _ _ _ val] []) =
sequence_ $ do
str <- getNormalString val
- match <- matchRegex regex str
- var <- match !!! 0
- op <- match !!! 1
+ var:op:_ <- matchRegex regex str
Map.lookup var references
return . warn (getId val) 2100 $
"Use $((..)) for arithmetics, e.g. i=$((i " ++ op ++ " 2))"
@@ -1460,9 +1457,8 @@ prop_checkForDecimals2 = verify checkForDecimals "foo[1.2]=bar"
prop_checkForDecimals3 = verifyNot checkForDecimals "declare -A foo; foo[1.2]=bar"
checkForDecimals params t@(TA_Expansion id _) = sequence_ $ do
guard $ not (hasFloatingPoint params)
- str <- getLiteralString t
- first <- str !!! 0
- guard $ isDigit first && '.' `elem` str
+ first:rest <- getLiteralString t
+ guard $ isDigit first && '.' `elem` rest
return $ err id 2079 "(( )) doesn't support decimals. Use bc or awk."
checkForDecimals _ _ = return ()
diff --git a/src/ShellCheck/Checks/Commands.hs b/src/ShellCheck/Checks/Commands.hs
index c4ffd87..97c9088 100644
--- a/src/ShellCheck/Checks/Commands.hs
+++ b/src/ShellCheck/Checks/Commands.hs
@@ -1237,8 +1237,7 @@ checkSudoArgs = CommandCheck (Basename "sudo") f
where
f t = sequence_ $ do
opts <- parseOpts $ arguments t
- let nonFlags = [x | ("",(x, _)) <- opts]
- commandArg <- nonFlags !!! 0
+ (_,(commandArg, _)) <- find (null . fst) opts
command <- getLiteralString commandArg
guard $ command `elem` builtins
return $ warn (getId t) 2232 $ "Can't use sudo with builtins like " ++ command ++ ". Did you want sudo sh -c .. instead?"
diff --git a/src/ShellCheck/Checks/ShellSupport.hs b/src/ShellCheck/Checks/ShellSupport.hs
index d070497..cab0546 100644
--- a/src/ShellCheck/Checks/ShellSupport.hs
+++ b/src/ShellCheck/Checks/ShellSupport.hs
@@ -79,9 +79,8 @@ prop_checkForDecimals3 = verifyNot checkForDecimals "declare -A foo; foo[1.2]=ba
checkForDecimals = ForShell [Sh, Dash, BusyboxSh, Bash] f
where
f t@(TA_Expansion id _) = sequence_ $ do
- str <- getLiteralString t
- first <- str !!! 0
- guard $ isDigit first && '.' `elem` str
+ first:rest <- getLiteralString t
+ guard $ isDigit first && '.' `elem` rest
return $ err id 2079 "(( )) doesn't support decimals. Use bc or awk."
f _ = return ()