koalaman / shellcheck

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

SC2031 is reported even if the subshell is in a different code path #3068

Open ale5000-git opened 1 month ago

ale5000-git commented 1 month ago

For bugs

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

#!/usr/bin/env sh
# shellcheck enable=all

my_func()
{
  if test "${1-}" = '-' && test "${#}" -eq 1; then
    (
      # shellcheck disable=SC2030 # Intended: Modification of LC_ALL is local (to subshell)
      LC_ALL='C'
      export LC_ALL

      some_code
    )
  else
    _old_lcall="${LC_ALL-}"
    LC_ALL='C'
    export LC_ALL

    some_other_code

    if test -n "${_old_lcall?}"; then LC_ALL="${_old_lcall:?}"; else unset LC_ALL; fi
  fi
}

my_func '-'

Here's what shellcheck currently says:

[Line 15:](javascript:setPosition(15, 17))
    _old_lcall="${LC_ALL-}"
                ^-- [SC2031](https://www.shellcheck.net/wiki/SC2031) (info): LC_ALL was modified in a subshell. That change might be lost.

Here's what I wanted or expected to see:

Nothing. Since the subshell is in a different code path it cannot affect the code at line 15 so it shouldn't report it.