Skip to content

Treat compgen -V as assigning to an array - #3518

Open
Eljees wants to merge 1 commit into
koalaman:masterfrom
Eljees:fix/3466-compgen-v-array
Open

Treat compgen -V as assigning to an array#3518
Eljees wants to merge 1 commit into
koalaman:masterfrom
Eljees:fix/3466-compgen-v-array

Conversation

@Eljees

@Eljees Eljees commented Aug 9, 2026

Copy link
Copy Markdown

Fixes #3466.

Symptom

compgen -V arr (bash 5.3) stores the completions in the array arr instead of printing them, but ShellCheck does not know that:

#!/bin/bash
compgen -V files -G '*'
printf "%s\n" "${files[@]}"
SC2154 (warning): files is referenced but not assigned.

The neighbouring forms are already handled — mapfile -t files, readarray -t files and printf -v out are all silent — so this is a gap in the table rather than a missing mechanism.

Change

getModifiedVariableCommand gains a compgen entry next to mapfile/readarray, plus a helper shaped like the existing getPrintfVariable/getWaitVariable pair: parse the flags, take the argument of -V, and record it as DataArray.

The flag spec lists compgen's own options, so -V is found wherever it appears in the argument list, and a compgen call without -V records nothing.

Tests

prop_checkUnassignedReferences54 = verifyNotTree checkUnassignedReferences "compgen -V files -G '*'; echo \"${files[@]}\""
prop_checkUnassignedReferences55 = verifyNotTree checkUnassignedReferences "compgen -A function -V funcs; echo \"${funcs[@]}\""

The second covers -V arriving after another option. Both fail on master with the tests alone — *** Failed! Falsified (after 1 test) for exactly these two and nothing else — and pass with the change; the full cabal test suite is green on GHC 9.8.4.

End-to-end, per CLAUDE.md:

script result
compgen -V files -G '*' + "${files[@]}" silent
compgen -A function -V funcs + "${funcs[@]}" silent
compgen -G '*' (no -V) + "${files[@]}" still SC2154
compgen -V files -G '*' + "${other[@]}" SC2154 for other, and SC2034 for files
mapfile -t files (control) silent, unchanged

The fourth row is the one I would check first as a reviewer: files now 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 getModifiedVariableCommand and draft the helper and the tests, following this repository's .claude/CLAUDE.md. I reviewed every line, reproduced the warning first and confirmed that mapfile/readarray/printf -v already behave correctly so the fix would follow an existing shape, ran the new tests against unpatched master to confirm they fail, and ran the full test suite and the end-to-end table above myself.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

compgen -V array not considered assigned

1 participant