Skip to content

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

@ale5000-git

Description

@ale5000-git

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions