shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

[SC2120] Wrongly triggerd when argument explicitly expanded with a default value

Open leagris opened this issue 3 years ago • 1 comments

For bugs

  • Rule Id (if any, e.g. SC1000): SC2120, SC2119
  • My shellcheck version (shellcheck --version or '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}

leagris avatar Jun 06 '22 11:06 leagris

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 :)

miallo avatar Jan 10 '24 14:01 miallo