Skip to content

Commit 9af7ee2

Browse files
authored
Merge pull request #3445 from e-kwsm/noglob
feat(SC2035): do not issue SC2035 when script has `set -f` or `set -o noglob`
2 parents 7d37c4f + d65122a commit 9af7ee2

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

src/ShellCheck/Analytics.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2627,8 +2627,10 @@ prop_checkGlobsAsOptions3 = verifyNot checkGlobsAsOptions "rm -- *.txt"
26272627
prop_checkGlobsAsOptions4 = verifyNot checkGlobsAsOptions "*.txt"
26282628
prop_checkGlobsAsOptions5 = verifyNot checkGlobsAsOptions "echo 'Files:' *.txt"
26292629
prop_checkGlobsAsOptions6 = verifyNot checkGlobsAsOptions "printf '%s\\n' *"
2630-
checkGlobsAsOptions _ cmd@(T_SimpleCommand _ _ args) =
2631-
unless ((fromMaybe "" $ getCommandBasename cmd) `elem` ["echo", "printf"]) $
2630+
prop_checkGlobsAsOptions7 = verifyNot checkGlobsAsOptions "set -f; ls *"
2631+
prop_checkGlobsAsOptions8 = verifyNot checkGlobsAsOptions "set -o noglob\nrm *"
2632+
checkGlobsAsOptions params cmd@(T_SimpleCommand _ _ args) =
2633+
unless (((fromMaybe "" $ getCommandBasename cmd) `elem` ["echo", "printf"]) || hasNoglob params) $
26322634
mapM_ check $ takeWhile (not . isEndOfArgs) (drop 1 args)
26332635
where
26342636
check v@(T_NormalWord _ (T_Glob id s:_)) | s == "*" || s == "?" =

src/ShellCheck/AnalyzerLib.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ data Parameters = Parameters {
8787
hasInheritErrexit :: Bool,
8888
-- Whether this script has 'set -e' anywhere.
8989
hasSetE :: Bool,
90+
-- Whether this script has 'set -f' or 'set -o noglob' anywhere.
91+
hasNoglob :: Bool,
9092
-- Whether this script has 'set -o pipefail' anywhere.
9193
hasPipefail :: Bool,
9294
-- Whether this script has 'shopt -s execfail' anywhere.
@@ -207,6 +209,7 @@ makeParameters spec = params
207209
rootNode = root,
208210
shellType = fromMaybe (determineShell (asFallbackShell spec) root) $ asShellType spec,
209211
hasSetE = containsSetE root,
212+
hasNoglob = containsNoglob root,
210213
hasLastpipe =
211214
case shellType params of
212215
Bash -> isOptionSet "lastpipe" root
@@ -262,6 +265,17 @@ containsSetE root = isNothing $ doAnalysis (guard . not . isSetE) root
262265
_ -> False
263266
re = mkRegex "[[:space:]]-[^-]*e"
264267

268+
containsNoglob root = isNothing $ doAnalysis (guard . not . isNoglob) root
269+
where
270+
isNoglob t =
271+
case t of
272+
T_Script _ (T_Literal _ str) _ -> str `matches` re
273+
T_SimpleCommand {} ->
274+
t `isUnqualifiedCommand` "set" &&
275+
("noglob" `elem` oversimplify t ||
276+
"f" `elem` map snd (getAllFlags t))
277+
_ -> False
278+
re = mkRegex "[[:space:]]-[^-]*f"
265279

266280
containsSetOption opt root = isNothing $ doAnalysis (guard . not . isPipefail) root
267281
where

0 commit comments

Comments
 (0)