Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FUNCTION_SETS_GLOBAL_VARS does not understand context #2

Open
cebtenzzre opened this issue Feb 1, 2023 · 2 comments
Open

FUNCTION_SETS_GLOBAL_VARS does not understand context #2

cebtenzzre opened this issue Feb 1, 2023 · 2 comments

Comments

@cebtenzzre
Copy link

The global vars check creates a lot of false positives because of how simple it is. It seems to ignore almost all context. Consider this example:

(
  a='a'
)

bar() (
  b='b'
)

c='c' true

local arr=(
  d='d'
)

e='e'; export e

python - <<'EOF'
f='f'
print(f)
EOF

All of those variable-like patterns should be fine ('d' and 'f' are not even variables), but zsh-sweep complains about all of them. 'f' is the most problematic, because it's not always practical to add a shell-style comment inside of a heredoc in order to suppress it. Another problem is that zsh-sweep complains about implicitly global variables being set outside of functions, even in --script mode, where there is no practical difference between local and global AFAIK.

Notice: running check: CHECK_FUNCTION_SETS_GLOBAL_VARS_PRE…
Notice: running check: CHECK_FUNCTION_SETS_GLOBAL_VARS_POST…
[ZSweep][zsfilt:184]: Error: Error near line #2 ↔  a='a':
[ZSweep][zsfilt:184]: Error: Error near line #6 ↔  b='b':
[ZSweep][zsfilt:184]: Error: Error near line #9 ↔ c='c' true:
[ZSweep][zsfilt:184]: Error: Error near line #12 ↔  d='d':
[ZSweep][zsfilt:184]: Error: Error near line #15 ↔ e='e'; export e:
[ZSweep][zsfilt:184]: Error: Error near line #18 ↔ f='f':
[ZSweep][zscan:125]: Warning: A global variable has been created without declaration, by assignment. Add a line like local VAR or typeset -g VAR so that the variable is defined before use.

Less concerning are the false negatives like this, which zsh-sweep does not see as variables:

{ a='a' }

foo() { b='b' }
@psprint
Copy link
Owner

psprint commented Feb 2, 2023

(
  a='a'
)

I think that a should be reported here. Was your thought that var created in subshell isn't a global? I think that it's good to expect a typeset in (…), for readability.

@cebtenzzre
Copy link
Author

I'm reading the error code as "a global was implicitly created inside of a function", and I see no function in that example. I suppose if you go by the description it might be referring to any undeclared variable, which seems like a purely stylistic note and not a diagnostic, since typeset can be used before or after a value is assigned to a variable. In that case, the error code should probably be changed to make its meaning clearer.

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

No branches or pull requests

2 participants