Skip to content

SC2319: rule triggered even though using variable assignment #3486

Description

@gblues

The resolution text says to assign it to a variable, but I still get the warning when doing so via a guarded statement.

I assume it is expecting variable assignment to look something like:

[[ -e "./lockfile" ]]
retval=$?

However, our scripts run with -e set, so the guarded form is needed to avoid script termination.

For bugs with existing features

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

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

#!/usr/bin/env bash

set -eu -o pipefail

test_function() {
  local retval=0

  [[ -e "./lockfile" ]] || retval=$?

  return "$retval"
}

main() {
  test_function || echo "lockfile does not exist"
}

main "$@"

Here's what shellcheck currently says:

In test.sh line 8:
  [[ -e "./lockfile" ]] || retval=$?
                                  ^-- SC2319 (warning): This $? refers to a condition, not a command. Assign to a variable to avoid it being overwritten.

For more information:
  https://www.shellcheck.net/wiki/SC2319 -- This $? refers to a condition, no...

Here's what I wanted or expected to see:

No warning

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