koalaman / shellcheck

ShellCheck, a static analysis tool for shell scripts
https://www.shellcheck.net
GNU General Public License v3.0
36.3k stars 1.77k forks source link

SC2119 for functions in sourced files with optional arguments #1679

Open anlutro opened 5 years ago

anlutro commented 5 years ago

Given a function defined in a sourced file that has optional arguments, shellcheck treats it as if it's a mistake to not pass any arguments.

Minimal reproducible setup:

$ cat lib.sh 
#!/bin/sh
f() { echo "f $*"; }

$ cat test.sh 
#!/bin/sh
. lib.sh
f

$ shellcheck -x ./test.sh 
In ./test.sh line 3:
f
^-- SC2119: Use f "$@" if function's $1 should mean script's $1.

If the function is defined in the same file, you correctly get a warning that the function is never called with arguments, but here this is not the case.

I ran into this when using db_go which is defined in /usr/share/debconf/confmodule on Debian/Ubuntu systems.

$ shellcheck --version
ShellCheck - shell script analysis tool
version: 0.7.0
license: GNU General Public License, version 3
website: https://www.shellcheck.net
jmccanta commented 4 years ago

I just ran into the same thing. db_go too.

leoheck commented 2 years ago

I am seeing the same issue.

juliyvchirkov commented 2 years ago

Same here, shellcheck 0.8.0

woodward2 commented 5 months ago

Same issue sourcing my function library.

andrew $ shellcheck --version
ShellCheck - shell script analysis tool
version: 0.7.0
license: GNU General Public License, version 3
website: https://www.shellcheck.net
brother commented 5 months ago

0.7.0 is getting a tad old at this moment (2020). Consider updating.

That said, this problem is till present in the latest HEAD.