Treat compgen -V as assigning to an array - #3518
Open
Eljees wants to merge 1 commit into
Open
Conversation
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 koalaman#3466
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3466.
Symptom
compgen -V arr(bash 5.3) stores the completions in the arrayarrinstead of printing them, but ShellCheck does not know that:The neighbouring forms are already handled —
mapfile -t files,readarray -t filesandprintf -v outare all silent — so this is a gap in the table rather than a missing mechanism.Change
getModifiedVariableCommandgains acompgenentry next tomapfile/readarray, plus a helper shaped like the existinggetPrintfVariable/getWaitVariablepair: parse the flags, take the argument of-V, and record it asDataArray.The flag spec lists compgen's own options, so
-Vis found wherever it appears in the argument list, and acompgencall without-Vrecords nothing.Tests
The second covers
-Varriving after another option. Both fail onmasterwith the tests alone —*** Failed! Falsified (after 1 test)for exactly these two and nothing else — and pass with the change; the fullcabal testsuite is green on GHC 9.8.4.End-to-end, per
CLAUDE.md:compgen -V files -G '*'+"${files[@]}"compgen -A function -V funcs+"${funcs[@]}"compgen -G '*'(no-V) +"${files[@]}"compgen -V files -G '*'+"${other[@]}"other, and SC2034 forfilesmapfile -t files(control)The fourth row is the one I would check first as a reviewer:
filesnow reports as assigned but unused rather than being silently ignored, which shows the variable is genuinely modelled as an assignment rather than the warning being suppressed.AI usage
I used Claude to help locate
getModifiedVariableCommandand draft the helper and the tests, following this repository's.claude/CLAUDE.md. I reviewed every line, reproduced the warning first and confirmed thatmapfile/readarray/printf -valready behave correctly so the fix would follow an existing shape, ran the new tests against unpatchedmasterto confirm they fail, and ran the full test suite and the end-to-end table above myself.