Skip to content

SC2034 - trigger for unused variables set with variable expansion or only checked for existence (fixes #3607) - #3615

Open
slycordinator wants to merge 2 commits into
koalaman:masterfrom
slycordinator:fix_unset_vars
Open

slycordinator wants to merge 2 commits into
koalaman:masterfrom
slycordinator:fix_unset_vars

Conversation

@slycordinator

@slycordinator slycordinator commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

fixes #3607

  • Change checkUnusedAssignments to be able to handle instances where a variable is set using ${var:-0}, ${var-0}, ${var:=0}, and/or ${var=0}, and ones where a variable is merely checked for having been set using [[ -z $var ]], [ -z "$var" ], test -z "$var" plus same with -n, and treat the variable as unused until found further referenced elsewhere
  • To do this, added isSelfDefault and isSetUnsetCheck functions. isSelfDefault returns true if the string is an assignment of one of the first set above and isSetUnsetCheck returns true if it finds a variable being checked for existence like the second set.
  • added test cases

1) Change checkUnusedAssignments to be able to handle instances where a variable is set using ${var:-0}, ${var-0}, ${var:=0}, and/or ${var=0} and not treat the variable as used in those cases, unless further used elsewhere

1a) To do this, added isSelfDefault function, which returns true if the string is an assignment of one of the above.

2) added test cases

This partially fixes issue 3607
@slycordinator
slycordinator marked this pull request as draft November 10, 2026 11:07
@slycordinator

Copy link
Copy Markdown
Contributor Author

Changed to a draft as I believe I've found a way to fix the other part of the referenced issue, but don't have a way to test it from home.

* in checkUnusedAssignments, add ability to exclude variables that are merely checked for whether they are set or unset as being used
like [[ -z ${var} ]], [[ -n ${var} ]] and the same using [ ] and test

* to do this, added  isSetUnsetCheck helper function that returns True if the variable in question is being checked for having been set/unset inside [[ ]] or [ ] and if is checked by a call to test

* added tests
@slycordinator
slycordinator marked this pull request as ready for review November 11, 2026 05:01
@slycordinator slycordinator changed the title Update Analytics.hs (Partially addresses #3607) SC2034 - trigger for unused variables set with variable expansion or only checked for existence (fixes #3607) Nov 11, 2026

This branch has not been deployed

No deployments
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.

SC2034 not triggered for case where a variable is only 'accessed' to check that it is empty

1 participant