class RuboCop::Cop::Lint::ReturnInVoidContext
end
return
def foo=(bar)
end
baz
return if bar?
foo
def initialize
# good
@example
end
return 42
def foo=(bar)
end
baz
return :qux if bar?
foo
def initialize
# bad
@example
where the value will be ignored. (initialize and setter methods)
This cop checks for the use of a return with a value in a context
def method_name(context_node)
def method_name(context_node) context_node.children.first end
def non_void_context(return_node)
def non_void_context(return_node) return_node.each_ancestor(:block, :def, :defs).first end
def on_return(return_node)
def on_return(return_node) return unless return_node.descendants.any? context_node = non_void_context(return_node) return unless context_node&.def_type? method_name = method_name(context_node) return unless method_name && void_context_method?(method_name) add_offense(return_node.loc.keyword, message: format(message, method: method_name)) end
def setter_method?(method_name)
def setter_method?(method_name) method_name.to_s.end_with?('=') && !AST::Node::COMPARISON_OPERATORS.include?(method_name) end
def void_context_method?(method_name)
def void_context_method?(method_name) method_name == :initialize || setter_method?(method_name) end