Skip to content

Commit b5ffafc

Browse files
committed
Treat compgen -V as assigning to an array
compgen -V arr (bash 5.3) stores the completions in the array arr instead of printing them, but getModifiedVariableCommand did not know about it, so `compgen -V files -G '*'` followed by "${files[@]}" produced a spurious SC2154 "files is referenced but not assigned". Add a compgen entry next to mapfile/readarray and a helper shaped like the existing getPrintfVariable/getWaitVariable: parse the flags, take the argument of -V and record it as DataArray. Fixes #3466
1 parent 9af7ee2 commit b5ffafc

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

src/ShellCheck/Analytics.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,6 +2534,8 @@ prop_checkUnassignedReferences50 = verifyNotTree checkUnassignedReferences "echo
25342534
prop_checkUnassignedReferences51 = verifyNotTree checkUnassignedReferences "echo ${foo:+$foo}"
25352535
prop_checkUnassignedReferences52 = verifyNotTree checkUnassignedReferences "wait -p pid; echo $pid"
25362536
prop_checkUnassignedReferences53 = verifyTree checkUnassignedReferences "x=($foo)"
2537+
prop_checkUnassignedReferences54 = verifyNotTree checkUnassignedReferences "compgen -V files -G '*'; echo \"${files[@]}\""
2538+
prop_checkUnassignedReferences55 = verifyNotTree checkUnassignedReferences "compgen -A function -V funcs; echo \"${funcs[@]}\""
25372539

25382540
checkUnassignedReferences = checkUnassignedReferences' False
25392541
checkUnassignedReferences' includeGlobals params t = warnings

src/ShellCheck/AnalyzerLib.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ getModifiedVariableCommand base@(T_SimpleCommand id cmdPrefix (T_NormalWord _ (T
683683

684684
"mapfile" -> maybeToList $ getMapfileArray base rest
685685
"readarray" -> maybeToList $ getMapfileArray base rest
686+
"compgen" -> maybeToList $ getCompgenArray rest
686687

687688
"DEFINE_boolean" -> maybeToList $ getFlagVariable rest
688689
"DEFINE_float" -> maybeToList $ getFlagVariable rest
@@ -742,6 +743,14 @@ getModifiedVariableCommand base@(T_SimpleCommand id cmdPrefix (T_NormalWord _ (T
742743
getPrintfVariable list = getFlagAssignedVariable "v" (SourceFrom list) $ getBsdOpts "v:" list
743744
getWaitVariable list = getFlagAssignedVariable "p" SourceInteger $ return $ getGenericOpts list
744745

746+
-- compgen -V arr stores the completions in the array arr rather than printing them
747+
getCompgenArray list = do
748+
flags <- getGnuOpts "abcdefgjksuvo:A:C:F:G:P:S:V:W:X:" list
749+
(_, (_, value)) <- find ((== "V") . fst) flags
750+
name <- getLiteralString value
751+
guard $ isVariableName name
752+
return (base, value, name, DataArray SourceExternal)
753+
745754
getFlagAssignedVariable str dataSource maybeFlags = do
746755
flags <- maybeFlags
747756
(_, (flag, value)) <- find ((== str) . fst) flags

0 commit comments

Comments
 (0)