Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SC2218 when overriding existing function #1595

Open
2 tasks done
Toreno96 opened this issue May 31, 2019 · 1 comment
Open
2 tasks done

SC2218 when overriding existing function #1595

Toreno96 opened this issue May 31, 2019 · 1 comment

Comments

@Toreno96
Copy link

For bugs

  • Rule Id (if any, e.g. SC1000): SC2218
  • My shellcheck version (shellcheck --version or "online"): 0.6.0
  • The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2086)
  • I tried on shellcheck.net and verified that this is still a problem on the latest commit

Here's a snippet or screenshot that shows the problem:

File b.sh:

#!/bin/bash

foo() {
    echo 'foo'
}
foo

foo() {
    echo 'overridden'
}
foo

Here's what shellcheck currently says:

$ shellcheck b.sh

In b.sh line 6:
foo
^-^ SC2218: This function is only defined later. Move the definition up.

For more information:
  https://www.shellcheck.net/wiki/SC2218 -- This function is only defined lat...

Here's what I wanted or expected to see:

No error, because it is not true, that "this function is only defined later", b.sh works as expected:

$ ./b.sh
foo
overridden

Optionally, there could be a warning that I'm overriding existing function.

@wileyhy
Copy link

wileyhy commented Jun 3, 2023

Encountering this SC issue while trying to isolate a double free in bash.

The way Shellcheck's error is worded isn't factually accurate. SC says, "only defined later," which in this case isn't true. In my opinion, it's something concerning this idea of, "can I trust ShellCheck to give me accurate information?" Perhaps it would be more accurate for ShellCheck to say something like, "This function is defined later. Please consider moving the definition up."

In ./test-script.sh_2 line 28:
test; echo "$?"
^--^ SC2218 (error): This function is only defined later. Move the definition up.

[liveuser@localhost-live]$ vim ./test-script.sh_2

...
13 test(){
14 shopt -s expand_aliases;
15 echo "$ec", shopt: "$?"
16 alias M_='m=("${l[@]}")';
17 echo "$ec", alias: "$?"
18 wait -f;
19 echo "$ec", wait: "$?"
20 type -a M_;
21 echo "$ec", type: "$?"
22 M_;
23 echo "$ec", alias:M_: "$?"
24 };
25 echo "$ec", definition of function:test "$?"
26 type -a test;
27 echo "$ec", type "$?"
28 test;
29 echo "$ec", function:test "$?"
30 M_
31 echo "$ec", alias:M_ "$?"
32 set -x;
33 test(){
34 shopt -s expand_aliases;
35 echo "$ec", shopt: "$?";
36 alias M_='m=("${l[@]}")';
37 echo "$ec", alias: "$?";
38 wait -f;
39 echo "$ec", wait: "$?";
40 type -a M_;
41 echo "$ec", type: "$?";
42 M_;
43 echo "$ec", alias:M_: "$?";
44 };
45 echo "$ec", duplicate re-definition of function:test "$?"
...

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants