[feedback requested] Detect void value expressions #12671
Draft
+535
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a WIP 🚧 hoping to get some feedback before I commit more effort. I know it doesn't have documentation, changelog, etc. I hope this is fine for the moment 🙏
Some interesting questions here would be:
Detail
Recently I came across a new (to me) error in Ruby code similar to the following:
This code would fail with the following syntax error:
So that's ok, I can understand it. Also it is an interesting one: despite being a syntax error, it's not flagged by Rubocop or ruby-parse. I'm assuming the interpreter catches it after building the AST, as opposed to during the parsing process itself? Whichever way it is, the tools I use (including Rubocop) don't flag it so I don't get to know it's happening until I run the code.
I suspect this is well known scenario and there's a good reason why Rubocop is not catching it. Is this correct?
Just in case, it piqued my curiosity and I had a go at it :-) This PR is a first version of what a cop for this might look like, if there's desire for one. I'm sure I'm missing something. For one, I know that
next
andbreak
can also cause a void value expression, so that'd be something to add. And of course documentation.I have also noticed that
case
statements appear immune to it, so I'm not raising offences on them. However I wonder if that's something that should be an option to the rule.Additionally, I have noticed that different MRI and JRuby treat this differently, which makes sense. There are edge cases (noted in the spec) where JRuby is better at catching these cases. Also there are questions of what to do in cases that look wrong, but the interpreters I have tried don't complain (see also marked in the spec).
Checklist
Not fully checked as this is a WIP asking for feedback:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.