detect unused functions
For new checks and feature suggestions
- [ x] https://www.shellcheck.net/ (i.e. the latest commit) currently gives no useful warnings about this
- [ x] I searched through https://github.com/koalaman/shellcheck/issues and didn't find anything related
Unused variables are detected by shellcheck but not functions. bash allows "exporting" a function. Using export -f creates an environment variable with the function body.
Example:
#!/bin/bash -f function FUNCTION_UNUSED() { VAR_UNUSED=TRUE } #eof
Here's what I got: Line 3: VAR_UNUSED=TRUE ^-- SC2034: VAR_UNUSED appears unused. Verify use (or export if used externally).
Here's what I wanted or expected to see: Line 2: function FUNCTION_UNUSED() ^-- SC####: FUNCTION_UNUSED () appears unused. Verify use (or export if used externally). Line 3: VAR_UNUSED=TRUE ^-- SC2034: VAR_UNUSED appears unused. Verify use (or export if used externally).
yeah i was about to suggest this also, good thing i searched to see if someone else suggested it
i tried to work around this in a custom check
using ctags from universal-ctags, but it is limited:
# show all function names:
ctags --sort=no --language-force=sh -x "file.sh"
# iterate over files, search for function names:
grep -q "$name[^(]\|${name}$" "$CODEBASE"
But think about this (contructed function names) or
even function build during execution time using eval:
#!/bin/sh
FILE="$1"
function_image_gif()
{
true
}
MIME="$( file --mime-type --brief "$FILE" )"
MIMEPRE="${MIME%/*}" # e.g. image
MIMESUB="${MIME#*/}" # e.g. gif
MYFUNC="function_${MIMEPRE}_${MIMESUB}"
command -v "$MYFUNC" >/dev/null && $MYFUNC