Skip to content

valid script throws SC2317 #3381

@dotysan

Description

@dotysan

For bugs

  • Rule Id (if any, e.g. SC1000): SC2317, SC2317, SC2317
  • My shellcheck version (shellcheck --version or 'online'): online
  • I tried on shellcheck.net and verified that this is still a problem on the latest commit
  • It's not reproducible on shellcheck.net, but I think that's because it's an OS, configuration or encoding issue

For new checks and feature suggestions

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

#! /usr/bin/env bash

main() {
    say_hello
}

say_hello() {
    echo 'hello'
}

# poor man's __main__
return 2>/dev/null ||:

main "$@"
exit $?

Here's what shellcheck currently says:

Line 12	SC2317: Command appears to be unreachable. Check usage (or ignore if invoked indirectly).
Line 14	SC2317: Command appears to be unreachable. Check usage (or ignore if invoked indirectly).
Line 15	SC2317: Command appears to be unreachable. Check usage (or ignore if invoked indirectly).

Here's what I wanted or expected to see:

This script is made "Pythonic" as it can be run or sourced. When run, it executes main(). When sourced, it loads all functions for later use but does not run main().

I believe the first SC2317 is the bug:

Line 12:
return 2>/dev/null ||:
                     ^-- SC2317 (info): Command appears to be unreachable. Check usage (or ignore if invoked indirectly).

That : (true) is in fact reachable when script is run, but not sourced.

The successive SC2317s are also reachable. But only when not sourced.

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