Skip to content

Commit

Permalink
Implement isCondition in terms of foldr
Browse files Browse the repository at this point in the history
  • Loading branch information
josephcsible committed Dec 31, 2023
1 parent 71889c1 commit 7b05899
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/ShellCheck/Analytics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,11 @@ dist a b
hasFloatingPoint params = shellType params == Ksh

-- Checks whether the current parent path is part of a condition
isCondition [] = False
isCondition [_] = False
isCondition (child:parent:rest) =
case child of
T_BatsTest {} -> True -- count anything in a @test as conditional
_ -> getId child `elem` map getId (getConditionChildren parent) || isCondition (parent:rest)
isCondition (x NE.:| xs) = foldr go (const False) xs x
where
go _ _ T_BatsTest{} = True -- count anything in a @test as conditional
go parent go_rest child =
getId child `elem` map getId (getConditionChildren parent) || go_rest parent
getConditionChildren t =
case t of
T_AndIf _ left right -> [left]
Expand Down Expand Up @@ -886,7 +884,7 @@ checkShorthandIf params x@(T_OrIf _ (T_AndIf id _ _) (T_Pipeline _ _ t))
name <- getCommandBasename t
return $ name `elem` ["echo", "exit", "return", "printf"])
isOk _ = False
inCondition = isCondition $ NE.toList $ getPath (parentMap params) x
inCondition = isCondition $ getPath (parentMap params) x
checkShorthandIf _ _ = return ()


Expand Down Expand Up @@ -3185,7 +3183,7 @@ checkUncheckedCdPushdPopd params root =
| name `elem` ["cd", "pushd", "popd"]
&& not (isSafeDir t)
&& not (name `elem` ["pushd", "popd"] && ("n" `elem` map snd (getAllFlags t)))
&& not (isCondition $ NE.toList $ getPath (parentMap params) t) =
&& not (isCondition $ getPath (parentMap params) t) =
warnWithFix (getId t) 2164
("Use '" ++ name ++ " ... || exit' or '" ++ name ++ " ... || return' in case " ++ name ++ " fails.")
(fixWith [replaceEnd (getId t) params 0 " || exit"])
Expand Down Expand Up @@ -4002,7 +4000,7 @@ checkUselessBang params t = when (hasSetE params) $ mapM_ check (getNonReturning
where
check t =
case t of
T_Banged id cmd | not $ isCondition (NE.toList $ getPath (parentMap params) t) ->
T_Banged id cmd | not $ isCondition (getPath (parentMap params) t) ->
addComment $ makeCommentWithFix InfoC id 2251
"This ! is not on a condition and skips errexit. Use `&& exit 1` instead, or make sure $? is checked."
(fixWith [replaceStart id params 1 "", replaceEnd (getId cmd) params 0 " && exit 1"])
Expand Down

0 comments on commit 7b05899

Please sign in to comment.