rubocop / rubocop

A Ruby static code analyzer and formatter, based on the community Ruby style guide.
https://docs.rubocop.org
MIT License
12.6k stars 3.05k forks source link

False negative for `Lint/Void` with ternary expression #13104

Open Earlopain opened 1 month ago

Earlopain commented 1 month ago

Expected behavior

Offense for the following:

foo? ? bar : 1
foo? ? 1 : bar

Actual behavior

No offense. There is already handling for 1 if foo? so this seems like a logical improvement.

RuboCop version

$ [bundle exec] rubocop -V
1.65.1 (using Prism 0.30.0, rubocop-ast 1.31.3, running on ruby 3.3.2) [aarch64-linux-android]
vlad-pisanov commented 1 month ago

That's an interesting thought. Expanding on it, any if/case statement in a void context that returns a literal (in any branch!) should potentially be flagged by this cop.

if cond?
  42 # literal returned into void context ⚠️ 
end
if cond?
  42 # literal returned into void context ⚠️ 
else
  foo
end
case val
when 'foo' then 42 # literal returned into void context ⚠️ 
end
Earlopain commented 1 month ago

When you write it like that, it looks very similar to #12393