Skip to content

Conversation

Copy link

Copilot AI commented Jan 2, 2026

SC2317 incorrectly flagged code after return statements in non-function contexts as unreachable. When return is invoked outside a function or sourced script, bash emits an error but continues execution.

#!/bin/bash
# Not sourced, not in a function
return || :
echo "This is reachable"  # Was incorrectly flagged as SC2317

Changes

  • CFG.hs: Modified handleReturn to check cfIsFunction context flag

    • In functions: create unreachable node (return terminates function)
    • At script level: treat as regular command that sets exit code (execution continues)
  • Analytics.hs: Added test cases for return at script level and in functions

Behavior

Context Before After
return at script level ❌ Incorrectly unreachable ✅ Reachable
return || : at script level ❌ Incorrectly unreachable ✅ Reachable
return in function ✅ Correctly unreachable ✅ Correctly unreachable
Original prompt

Fix these false postiive SC2317 on return ||: when script is not sourced. koalaman#3381


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI self-assigned this Jan 2, 2026
Copilot AI changed the title [WIP] Fix false positive SC2317 on return ||: Fix SC2317 false positive: return outside functions is not unreachable Jan 2, 2026
Copilot AI requested a review from dotysan January 2, 2026 22:43
@dotysan
Copy link
Owner

dotysan commented Jan 2, 2026

@copilot In addition to the false-positive unreachable on : right of return || when not sourced or in function, does this also address the successive SC2317 on valid code after the return ||: line when not sourced or in function.

Copy link
Author

Copilot AI commented Jan 2, 2026

@copilot In addition to the false-positive unreachable on : right of return || when not sourced or in function, does this also address the successive SC2317 on valid code after the return ||: line whe...

Yes, the fix addresses both issues:

  1. False positive on : (or true) after return || - Fixed ✅
  2. False positive on all successive lines after return ||: - Fixed ✅

The root cause was the same: handleReturn was creating an unreachable end node for all return statements. By checking cfIsFunction and only creating unreachable nodes when inside functions, both the || right-hand side and all subsequent lines are now correctly marked as reachable when return is at script level.

Commit: eb217e8

@dotysan dotysan marked this pull request as ready for review January 2, 2026 23:10
@dotysan dotysan marked this pull request as draft January 2, 2026 23:19
@dotysan
Copy link
Owner

dotysan commented Jan 2, 2026

Move back to draft, because we don't need to merge here. Instead, do it upstream. koalaman#3382

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

Successfully merging this pull request may close these issues.

2 participants