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

[SC2317] False positive with nested functions #3057

Open
2 tasks done
GrabbenD opened this issue Sep 15, 2024 · 0 comments
Open
2 tasks done

[SC2317] False positive with nested functions #3057

GrabbenD opened this issue Sep 15, 2024 · 0 comments

Comments

@GrabbenD
Copy link

GrabbenD commented Sep 15, 2024

For bugs


Non-problematic code example

#!/usr/bin/env bash

PARENT () {
  echo 'ok'

  NESTED () {
    echo 'ok'
  }
}

PARENT # SC2317 isn't triggered despite NESTED never being invoked (unless exit is specified on next line)

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

  1. When PARENT function is exported:
PARENT () {
  echo 'ok'

  NESTED () {
    echo 'SC2317 ERROR triggered only for this line'
  }
}

func=$(declare -f PARENT)
$func  # PARENT echo works
NESTED # NESTED echo works
  1. When NESTED function definition is printed:
PARENT () {
  echo 'ok'

  NESTED () {
    echo 'SC2317 ERROR triggered only for this line'
  }
}

func=$(PARENT && declare -f NESTED) 
echo "$func" # Contents of NESTED are printed
  1. When exported PARENT function is called in a new shell
PARENT () {
  echo 'ok'

  NESTED () {
    echo 'SC2317 ERROR triggered only for this line'
  }
}

cmd="$(declare -f PARENT); PARENT; NESTED"
bash -c "${cmd}" # Both echo works
#runuser --login "${USER}" --shell "/bin/bash" -- -c "${cmd}" # Another example
  1. When PARENT function is called indirectly through a function argument (this pattern is commonly used for callback functions):
PARENT () {
  echo 'ok'

  NESTED () {
    echo 'SC2317 ERROR triggered only for this line'
  }
}

HANDLER () {
  $1     # PARENT echo works
  NESTED # NESTED echo works
}

HANDLER PARENT

Here's what shellcheck currently says:

www.shellcheck.net

SC2329 (info): This function is never invoked. Check usage (or ignored if invoked indirectly).

VSCode

Command appears to be unreachable. Check usage (or ignore if invoked indirectly). shellcheck(SC2329)


Here's what I wanted or expected to see:

Indirectly invoked NESTED function(s) shouldn't trigger SC2329 if the PARENT function isn't detected as unused

Summary

Looks like this false-positive is triggered for NESTED function(s) only when the PARENT function is called indirectly

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

1 participant