I am seeing more and more code like this:
def initialize
foo
return :qux if bar?
baz
end
I don't think it's very useful to return from an initializer, or at least I have never had a need to do so before. Even if there are cases where that would make sense, it is not possible to use a return value from an initializer.
I suggest a cop that marks returns from initializer methods as offenses or, alternatively (if someone can provide a strong rationale for still doing that) marks code that tries to return an explicit value from an initilaizer.
RuboCop registers an offense.
RuboCop doesn't register an offense.
Run bundle exec rubocop on:
def initialize
foo
return :qux if bar?
baz
end
Include the output of rubocop -V. Here's an example:
$ rubocop -V
0.47.1 (using Parser 2.3.3.1, running on ruby 2.3.3 x86_64-darwin15)
This reminded me that the return value from setter methods is ignored. We could make a cop for that too.
pry> class A
pry* def foo=(bar)
pry* return 42
pry* end
pry* end
pry> a = A.new
pry> a.foo = :some_value
:some_value
ConfigurableEnforcedStyle uses a return in the detected_style= setter method.
Will fix my PR later to modify this file.
Most helpful comment
This reminded me that the return value from setter methods is ignored. We could make a cop for that too.