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

Used but not assigned not warning when in same line #1083

Open
1 of 2 tasks
rugk opened this issue Dec 31, 2017 · 4 comments
Open
1 of 2 tasks

Used but not assigned not warning when in same line #1083

rugk opened this issue Dec 31, 2017 · 4 comments

Comments

@rugk
Copy link

rugk commented Dec 31, 2017

For bugs

  • Rule Id (if any, e.g. SC1000): SC2034
  • My shellcheck version (shellcheck --version or "online"): online
  • I tried on shellcheck.net and verified that this is still a problem on the latest commit
  • It's not reproducible on shellcheck.net, but I think that's because it's an OS, configuration or encoding issue

Here's a snippet or screenshot that shows the problem:

#!/bin/sh
hu="x$hu"
number=$(( number+1 ))

Here's what shellcheck currently says:

nothing

The problem is, it does consider that the right part of the = is evaluated first and thus the variable is first used and later assigned to itself. Especially with arithmetic operations, this is highly problematic.

Here's what I wanted or expected to see:

Warning: "Variable hu referenced before assigned."
Warning: "Variable number referenced before assigned."

@nihilus
Copy link

nihilus commented Dec 31, 2017

How is that problematic in arithmetic operations? It will implicity get the value 0.

@rugk
Copy link
Author

rugk commented Dec 31, 2017

If you think of multiplication (var*5) it will get worse (syntax errored in my small test) out dividing through zero it so…

@koalaman
Copy link
Owner

koalaman commented Jan 7, 2018

You're right, of course. This is something I really want to fix in the general case, but it requires some significant infrastructure improvements that I haven't gotten around to (specifically, building a useful control flow graph of the script).

@NateEag
Copy link

NateEag commented Aug 27, 2020

I spent a few minutes yesterday scratching my head until I realized I had made this error.

It surprised me that Shellcheck didn't catch it for me, as it does amazingly well at detecting my stupid mistakes overall.

@koalaman , when you say you want to fix this "in the general case", do you mean to imply there's a simpler, dumber fix for this that you don't want to use, because analogous problems will exist elsewhere?

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

4 participants