KEMBAR78
[Fix #13963] Fix wrong autocorrect for `Lint/LiteralAsCondition` when the literal is followed by `return`, `break`, or `next` by Earlopain · Pull Request #13966 · rubocop/rubocop · GitHub
Skip to content

Conversation

@Earlopain
Copy link
Contributor

@Earlopain Earlopain commented Mar 7, 2025

Fix #13963

It still makes sense to register an offense in this case since the code is likely faulty and not what the user intended. There's just not much to reasonably autocorrect to.

For example, OP of the issue noticed that parens where missing.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@Earlopain Earlopain force-pushed the literal-condition-void-expr branch from bab13f9 to 92e6464 Compare March 7, 2025 12:18
@Earlopain Earlopain changed the title Fix wrong autocorrect for Lint/LiteralAsCondition when the literal is followed by return, break, or next [Fix #13963] Fix wrong autocorrect for Lint/LiteralAsCondition when the literal is followed by return, break, or next Mar 7, 2025
RUBY
end

%w[return break next].each do |keyword|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you separate the examples into three different ones instead of using each?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I tweaked examples to remove the block when not necessary for it to be valid syntax for return.


expect_no_corrections
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a spec for something like:

puts "foo" && return if 2 && foo?

Copy link
Contributor Author

@Earlopain Earlopain Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ruby seems to parse this as puts ("foo" && return) if 2 && foo? so I don't think that's necessary.

https://nodepattern.herokuapp.com/?p=(...)&ruby=puts%20%22foo%22%20%26%26%20return%20if%202%20%26%26%20foo%3F

add_offense(node.lhs) do |corrector|
# Don't autocorrect `'foo' && return` because having `return` as
# the leftmost node can lead to a void value expression syntax error.
next if node.rhs.type?(:return, :break, :next)
Copy link
Contributor

@tejasbubane tejasbubane Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we also need to handle other cases like raising exceptions?

puts "foo" && raise("bar") if baz?
puts "a" && fail("b") if c?
puts "a" && exit if c?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ruby doesn't have a problem with these, they are just ordinary method calls. The code itself doesn't make much sense though.

Maybe there are more where this syntax error could happen but the rules must be pretty complex because even the parser gem doesn't implement this as a syntax error.

…n` when the literal is followed by `return`, `break`, or `next`

It still makes sense to register an offense in this case since the code is likely faulty
and not what the user intended. There's just not much to reasonably autocorrect to.

For example, OP of the issue noticed that parens where missing.
@Earlopain Earlopain force-pushed the literal-condition-void-expr branch from 92e6464 to 8b83cec Compare March 7, 2025 12:46
@koic koic merged commit 3317cf1 into rubocop:master Mar 7, 2025
24 checks passed
@koic
Copy link
Member

koic commented Mar 7, 2025

Thanks!

@Earlopain Earlopain deleted the literal-condition-void-expr branch September 3, 2025 12:09
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.

Lint/LiteralAsCondition auto-correct breaks some code even more

3 participants