-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
return_in_void_context.rb
59 lines (52 loc) · 1.28 KB
/
return_in_void_context.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# This cop checks for the use of a return with a value in a context
# where the value will be ignored. (initialize and setter methods)
#
# @example
#
# # bad
# def initialize
# foo
# return :qux if bar?
# baz
# end
#
# def foo=(bar)
# return 42
# end
#
# @example
#
# # good
# def initialize
# foo
# return if bar?
# baz
# end
#
# def foo=(bar)
# return
# end
class ReturnInVoidContext < Base
MSG = 'Do not return a value in `%<method>s`.'
def on_return(return_node)
return unless return_node.descendants.any?
context_node = non_void_context(return_node)
return unless context_node&.def_type?
return unless context_node&.void_context?
add_offense(
return_node.loc.keyword,
message: format(message, method: context_node.method_name)
)
end
private
def non_void_context(return_node)
return_node.each_ancestor(:block, :def, :defs).first
end
end
end
end
end