[SC2120] Wrongly triggerd when argument explicitly expanded with a default value
For bugs
- Rule Id (if any, e.g. SC1000): SC2120, SC2119
- My shellcheck version (
shellcheck --versionor 'online'): online - [x] I tried on shellcheck.net and verified that this is still a problem on the latest commit
For new checks and feature suggestions
- [x] shellcheck.net (i.e. the latest commit) currently gives no useful warnings about this
Here's a snippet or screenshot that shows the problem:
#!/bin/bash
fn() {
local -i a=${1:-42}
printf '%d\n' "$a"
}
fn
Here's what shellcheck currently says:
Line 3 SC2120: fn references arguments, but none are ever passed.
Line 7 SC2119: Use fn "$@" if function's $1 should mean script's $1.
Here's what I wanted or expected to see:
These warnings shall not be triggered when an argument is explicitly expanded with a default value like ${1:-default_value}
Had the same issue. I tried implementing this myself and hunted it down to https://github.com/koalaman/shellcheck/blob/ba86c6363c30a5dbefd0b8b9a7c5f4ab0478dc91/src/ShellCheck/Analytics.hs#L2826
and that isPositionalReference only looks if the variable is referenced https://github.com/koalaman/shellcheck/blob/ba86c6363c30a5dbefd0b8b9a7c5f4ab0478dc91/src/ShellCheck/Analytics.hs#L2837
but it does not check if there is a default value. Since I don't know any Haskell or this project in particular I am not sure how to extract that information from there, so that all my attempts failed. Would be nice if someone would pick this up :)