koalaman/shellcheck

[BUG] SC2218 false negative for every code above last `"$(f)"`

Opened this issue · 0 comments

bug

SC2218 false negative for every code above last "$(f)"

For bugs with existing features

  • Rule Id (if any, e.g. SC1000): SC2218
  • My shellcheck version (shellcheck --version or "online"):
ShellCheck - shell script analysis tool
version: 0.11.0
license: GNU General Public License, version 3
website: https://www.shellcheck.net

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

#!/bin/bash
set -euo pipefail

function pre_log() {
  echo "pre_log"
}

function pre_do_something() {
  echo pre_do_something
}

function post_log() {
  echo "post_log"
}

function post_do_something() {
  echo post_do_something
}

function main() {
  echo "[log] $(pre_log)"
  pre_do_something

  echo "[log] $(post_log)"
  post_do_something
}

main "$@"

Here's what shellcheck currently says:

$ shellcheck /tmp/reproduce.sh && echo OK

In /tmp/reproduce.sh line 21:
  echo "[log] $(pre_log)"
                ^-----^ SC2218 (error): This function is only defined later. Move the definition up.


In /tmp/reproduce.sh line 22:
  pre_do_something
  ^--------------^ SC2218 (error): 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...

Within simplify reproduce

  • "[log] $(post_log)" is the last "$(f)"
  • every code above last "$(f)", are reported SC2218
    • echo "[log] $(pre_log)"
    • pre_do_something
  • but these SC2218 are false negative
  • every code not above last "$(f)", are OK
    • echo "[log] $(post_log)"
    • post_do_something

Here's what I wanted or expected to see:

  • every code above last "$(f)", are NOT reported SC2218
    • echo "[log] $(pre_log)"
    • pre_do_something