From 90f03c87647c72b82713cc3d6983025ed6eacc85 Mon Sep 17 00:00:00 2001 From: koalaman Date: Mon, 24 Feb 2014 18:21:54 -0800 Subject: [PATCH] Created SC2034 (markdown) --- SC2034.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 SC2034.md diff --git a/SC2034.md b/SC2034.md new file mode 100644 index 0000000..862354b --- /dev/null +++ b/SC2034.md @@ -0,0 +1,40 @@ +# foo appears unused. Verify it or export it. + +### Problematic code: + + foo=42 + echo "$FOO" + +### Correct code: + + foo=42 + echo "$foo" + +### Rationale: + +Variables not used for anything are often associated with bugs, so ShellCheck warns about them. + +### Contraindications + +ShellCheck may not always realize that the variable is in use (especially with indirection), and may not realize you don't care (with throwaway variables or unimplemented features). + +For throwaway variables, consider using `_` as a dummy: + + read _ last _ zip _ _ <<< "$str" + echo "$last, $zip" + +or use a directive to disable the warning: + + # shellcheck disable=SC2034 + read first last email zip lat lng <<< "$str" + echo "$last, $zip" + +For indirection, there's not much you can do without rewriting to use arrays or similar: + + bar=42 # will always appear unused + foo=bar + echo "${!foo}" + +This is expected behavior, and not a bug. There is no good way to statically analyze indirection in shell scripts, just like static C analyzers have a hard time preventing segfaults. + +As always, you can make an alias for `shellcheck -e SC2034` if you're frequently annoyed by this error. \ No newline at end of file