Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better "dead code" error location #7729

Open
jez opened this issue Feb 28, 2024 · 0 comments
Open

Better "dead code" error location #7729

jez opened this issue Feb 28, 2024 · 0 comments
Labels
enhancement New feature or surprising current feature IDE Relating to Sorbet's LSP server or VS Code extension

Comments

@jez
Copy link
Collaborator

jez commented Feb 28, 2024

Input

→ View on sorbet.run

# typed: true
class A; end
class B1
  if !(self < A)
    raise
  end

  variable = T.unsafe("hello")

  unless variable.nil?
    puts("this is also dead")
  end
end
class B2
  if !(self < A)
    raise
  end

  unless T.unsafe("hello").nil?
    puts("this is also dead")
  end
end

Observed output

editor.rb:8: This code is unreachable https://srb.help/7006
     8 |  variable = T.unsafe("hello")
     9 |
    10 |  unless variable.nil?
    editor.rb:4: This condition was always truthy (T::Boolean)
     4 |  if !(self < A)
             ^^^^^^^^^^^
  Got T::Boolean originating from:
    editor.rb:4:
     4 |  if !(self < A)
             ^^^^^^^^^^^

editor.rb:19: This code is unreachable https://srb.help/7006
    19 |  unless T.unsafe("hello").nil?
                 ^^^^^^^^^^^^^^^^^^^^^^
    editor.rb:15: This condition was always truthy (T::Boolean)
    15 |  if !(self < A)
             ^^^^^^^^^^^
  Got T::Boolean originating from:
    editor.rb:15:
    15 |  if !(self < A)
             ^^^^^^^^^^^
Errors: 2

Expected behavior

In #4483, we made some changes to both tweak the error location for "This code is unreachable" errors so that it would span all the dead code in a dead block from the first dead instruction to the end of the block. That doesn't quite go far enough: it would be nice to have the error expand all the way to any downstream blocks.

This particular error code powers Sorbet's support for the LSP "Unnecessary" diagnostic tag, which instructs LSP clients to dim or gray out the unnecessary code if they wish. Since the error diagnostic stops short of highlighting all the dead code, the IDE won't highlight all the dead code.


@jez jez added enhancement New feature or surprising current feature IDE Relating to Sorbet's LSP server or VS Code extension labels Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or surprising current feature IDE Relating to Sorbet's LSP server or VS Code extension
Projects
None yet
Development

No branches or pull requests

1 participant