/
empty_in_pattern.rb
62 lines (57 loc) · 1.24 KB
/
empty_in_pattern.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
60
61
62
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# This cop checks for the presence of `in` pattern branches without a body.
#
# @example
#
# # bad
# case condition
# in [a]
# do_something
# in [a, b]
# end
#
# # good
# case condition
# in [a]
# do_something
# in [a, b]
# nil
# end
#
# @example AllowComments: true (default)
#
# # good
# case condition
# in [a]
# do_something
# in [a, b]
# # noop
# end
#
# @example AllowComments: false
#
# # bad
# case condition
# in [a]
# do_something
# in [a, b]
# # noop
# end
#
class EmptyInPattern < Base
extend TargetRubyVersion
MSG = 'Avoid `in` branches without a body.'
minimum_target_ruby_version 2.7
def on_case_match(node)
node.in_pattern_branches.each do |branch|
next if branch.body || cop_config['AllowComments'] && comment_lines?(node)
add_offense(branch)
end
end
end
end
end
end