rubocop 0.20.1 → 0.21.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rubocop might be problematic. Click here for more details.

Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +46 -0
  3. data/README.md +16 -4
  4. data/config/default.yml +37 -10
  5. data/config/enabled.yml +25 -7
  6. data/lib/rubocop.rb +15 -19
  7. data/lib/rubocop/cli.rb +2 -2
  8. data/lib/rubocop/config.rb +40 -3
  9. data/lib/rubocop/config_loader.rb +6 -37
  10. data/lib/rubocop/config_store.rb +0 -1
  11. data/lib/rubocop/cop/commissioner.rb +12 -9
  12. data/lib/rubocop/cop/cop.rb +17 -5
  13. data/lib/rubocop/cop/force.rb +41 -0
  14. data/lib/rubocop/cop/ignored_node.rb +10 -10
  15. data/lib/rubocop/cop/lint/ambiguous_operator.rb +1 -1
  16. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
  17. data/lib/rubocop/cop/lint/block_alignment.rb +2 -2
  18. data/lib/rubocop/cop/lint/condition_position.rb +2 -0
  19. data/lib/rubocop/cop/lint/debugger.rb +17 -3
  20. data/lib/rubocop/cop/lint/end_alignment.rb +3 -11
  21. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
  22. data/lib/rubocop/cop/lint/rescue_exception.rb +11 -0
  23. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +4 -6
  24. data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +39 -0
  25. data/lib/rubocop/cop/lint/unused_block_argument.rb +81 -0
  26. data/lib/rubocop/cop/lint/unused_method_argument.rb +52 -0
  27. data/lib/rubocop/cop/lint/useless_assignment.rb +6 -8
  28. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +6 -0
  29. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  30. data/lib/rubocop/cop/lint/void.rb +1 -1
  31. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +2 -2
  32. data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +7 -3
  33. data/lib/rubocop/cop/mixin/check_assignment.rb +11 -0
  34. data/lib/rubocop/cop/mixin/check_methods.rb +12 -0
  35. data/lib/rubocop/cop/mixin/percent_literal.rb +26 -0
  36. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +2 -2
  37. data/lib/rubocop/cop/mixin/string_help.rb +0 -4
  38. data/lib/rubocop/cop/rails/delegate.rb +109 -0
  39. data/lib/rubocop/cop/style/align_hash.rb +3 -3
  40. data/lib/rubocop/cop/style/class_and_module_children.rb +2 -2
  41. data/lib/rubocop/cop/style/colon_method_call.rb +2 -2
  42. data/lib/rubocop/cop/style/dot_position.rb +1 -1
  43. data/lib/rubocop/cop/style/encoding.rb +44 -16
  44. data/lib/rubocop/cop/style/indentation_width.rb +29 -19
  45. data/lib/rubocop/cop/style/lambda_call.rb +1 -1
  46. data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
  47. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +3 -21
  48. data/lib/rubocop/cop/style/predicate_name.rb +1 -1
  49. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  50. data/lib/rubocop/cop/style/redundant_begin.rb +18 -2
  51. data/lib/rubocop/cop/style/redundant_self.rb +2 -2
  52. data/lib/rubocop/cop/style/regexp_literal.rb +29 -37
  53. data/lib/rubocop/cop/style/space_after_method_name.rb +1 -1
  54. data/lib/rubocop/cop/style/string_literals.rb +1 -1
  55. data/lib/rubocop/cop/style/trailing_blank_lines.rb +37 -23
  56. data/lib/rubocop/cop/style/trailing_comma.rb +2 -3
  57. data/lib/rubocop/cop/style/trivial_accessors.rb +40 -4
  58. data/lib/rubocop/cop/style/unneeded_capital_w.rb +29 -0
  59. data/lib/rubocop/cop/team.rb +10 -2
  60. data/lib/rubocop/cop/util.rb +2 -2
  61. data/lib/rubocop/cop/{variable_inspector.rb → variable_force.rb} +45 -37
  62. data/lib/rubocop/cop/{variable_inspector → variable_force}/assignment.rb +1 -1
  63. data/lib/rubocop/cop/{variable_inspector → variable_force}/locatable.rb +1 -1
  64. data/lib/rubocop/cop/{variable_inspector → variable_force}/reference.rb +13 -1
  65. data/lib/rubocop/cop/{variable_inspector → variable_force}/scope.rb +9 -1
  66. data/lib/rubocop/cop/{variable_inspector → variable_force}/variable.rb +14 -4
  67. data/lib/rubocop/cop/{variable_inspector → variable_force}/variable_table.rb +1 -1
  68. data/lib/rubocop/file_inspector.rb +3 -1
  69. data/lib/rubocop/formatter/base_formatter.rb +1 -1
  70. data/lib/rubocop/formatter/disabled_config_formatter.rb +2 -2
  71. data/lib/rubocop/formatter/disabled_lines_formatter.rb +2 -2
  72. data/lib/rubocop/formatter/offense_count_formatter.rb +11 -10
  73. data/lib/rubocop/formatter/progress_formatter.rb +2 -2
  74. data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
  75. data/lib/rubocop/options.rb +74 -58
  76. data/lib/rubocop/path_util.rb +17 -2
  77. data/lib/rubocop/rake_task.rb +23 -5
  78. data/lib/rubocop/version.rb +1 -1
  79. data/relnotes/v0.21.0.md +45 -0
  80. data/rubocop-todo.yml +6 -6
  81. data/rubocop.gemspec +1 -1
  82. data/spec/rubocop/cli_spec.rb +170 -59
  83. data/spec/rubocop/config_spec.rb +48 -3
  84. data/spec/rubocop/config_store_spec.rb +3 -3
  85. data/spec/rubocop/cop/commissioner_spec.rb +9 -7
  86. data/spec/rubocop/cop/cop_spec.rb +0 -2
  87. data/spec/rubocop/cop/force_spec.rb +29 -0
  88. data/spec/rubocop/cop/lint/ambiguous_operator_spec.rb +2 -2
  89. data/spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb +1 -1
  90. data/spec/rubocop/cop/lint/block_alignment_spec.rb +24 -24
  91. data/spec/rubocop/cop/lint/condition_position_spec.rb +7 -5
  92. data/spec/rubocop/cop/lint/debugger_spec.rb +26 -9
  93. data/spec/rubocop/cop/lint/end_alignment_spec.rb +6 -3
  94. data/spec/rubocop/cop/lint/invalid_character_literal_spec.rb +7 -6
  95. data/spec/rubocop/cop/lint/rescue_exception_spec.rb +36 -0
  96. data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +4 -4
  97. data/spec/rubocop/cop/lint/underscore_prefixed_variable_name_spec.rb +179 -0
  98. data/spec/rubocop/cop/lint/unused_block_argument_spec.rb +147 -0
  99. data/spec/rubocop/cop/lint/unused_method_argument_spec.rb +140 -0
  100. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +50 -48
  101. data/spec/rubocop/cop/lint/useless_else_without_rescue_spec.rb +1 -1
  102. data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +2 -0
  103. data/spec/rubocop/cop/rails/delegate_spec.rb +152 -0
  104. data/spec/rubocop/cop/style/encoding_spec.rb +131 -36
  105. data/spec/rubocop/cop/style/indentation_width_spec.rb +79 -0
  106. data/spec/rubocop/cop/style/redundant_begin_spec.rb +32 -0
  107. data/spec/rubocop/cop/style/regexp_literal_spec.rb +83 -13
  108. data/spec/rubocop/cop/style/string_literals_spec.rb +9 -3
  109. data/spec/rubocop/cop/style/trailing_blank_lines_spec.rb +65 -25
  110. data/spec/rubocop/cop/style/trivial_accessors_spec.rb +76 -0
  111. data/spec/rubocop/cop/style/unneeded_capital_w_spec.rb +85 -0
  112. data/spec/rubocop/cop/team_spec.rb +43 -0
  113. data/spec/rubocop/cop/{variable_inspector → variable_force}/assignment_spec.rb +3 -3
  114. data/spec/rubocop/cop/{variable_inspector → variable_force}/locatable_spec.rb +3 -3
  115. data/spec/rubocop/cop/{variable_inspector → variable_force}/scope_spec.rb +30 -2
  116. data/spec/rubocop/cop/{variable_inspector → variable_force}/variable_spec.rb +12 -27
  117. data/spec/rubocop/cop/{variable_inspector → variable_force}/variable_table_spec.rb +1 -1
  118. data/spec/rubocop/cop/{variable_inspector_spec.rb → variable_force_spec.rb} +4 -8
  119. data/spec/rubocop/formatter/base_formatter_spec.rb +2 -2
  120. data/spec/rubocop/formatter/offense_count_formatter_spec.rb +1 -1
  121. data/spec/rubocop/formatter/progress_formatter_spec.rb +1 -1
  122. data/spec/rubocop/options_spec.rb +2 -2
  123. data/spec/rubocop/path_util_spec.rb +47 -14
  124. data/spec/spec_helper.rb +9 -3
  125. data/spec/support/file_helper.rb +2 -0
  126. metadata +43 -26
  127. data/lib/rubocop/cop/style/final_newline.rb +0 -29
  128. data/spec/rubocop/cop/style/final_newline_spec.rb +0 -30
@@ -15,8 +15,9 @@ describe Rubocop::Cop::Lint::EndAlignment, :config do
15
15
  inspect_source(cop, ["#{prefix}#{alignment_base} #{arg}",
16
16
  end_kw])
17
17
  expect(cop.offenses.size).to eq(1)
18
- expect(cop.messages.first)
19
- .to match(/end at 2, \d+ is not aligned with #{alignment_base} at 1,/)
18
+ expect(cop.messages.first).to match(
19
+ /`end` at 2, \d+ is not aligned with `#{alignment_base}` at 1,/
20
+ )
20
21
  expect(cop.highlights.first).to eq('end')
21
22
  expect(cop.config_to_allow_offenses).to eq('AlignWith' => opposite)
22
23
  end
@@ -88,7 +89,7 @@ describe Rubocop::Cop::Lint::EndAlignment, :config do
88
89
  'end'])
89
90
  expect(cop.offenses.size).to eq(1)
90
91
  expect(cop.messages.first)
91
- .to eq('end at 6, 0 is not aligned with if at 4, 4')
92
+ .to eq('`end` at 6, 0 is not aligned with `if` at 4, 4')
92
93
  expect(cop.highlights.first).to eq('end')
93
94
  expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
94
95
  end
@@ -130,6 +131,8 @@ describe Rubocop::Cop::Lint::EndAlignment, :config do
130
131
  include_examples 'aligned', 'var ||= if', 'test', 'end'
131
132
  include_examples 'aligned', 'var &&= if', 'test', 'end'
132
133
  include_examples 'aligned', 'var += if', 'test', 'end'
134
+ include_examples 'aligned', 'h[k] = if', 'test', 'end'
135
+ include_examples 'aligned', 'h.k = if', 'test', 'end'
133
136
  end
134
137
  end
135
138
  end
@@ -16,18 +16,19 @@ describe Rubocop::Cop::Lint::InvalidCharacterLiteral do
16
16
  #
17
17
  # https://github.com/ruby/ruby/blob/v2_1_0/parse.y#L7276
18
18
  # https://github.com/whitequark/parser/blob/v2.1.2/lib/parser/lexer.rl#L1660
19
- context 'with a non-escaped whitespace character literal ' do
19
+ context 'with a non-escaped whitespace character literal' do
20
20
  let(:source) { 'p(? )' }
21
21
 
22
22
  it 'registers an offense' do
23
- pending 'Is there a way to emit this warning without syntax errors?'
23
+ pending 'Is there a way to emit this warning without syntax errors?' do
24
24
 
25
- inspect_source(cop, source)
25
+ inspect_source(cop, source)
26
26
 
27
- expect(cop.offenses.size).to eq(1)
28
- expect(cop.offenses.first.message)
27
+ expect(cop.offenses.size).to eq(1)
28
+ expect(cop.offenses.first.message)
29
29
  .to eq('Invalid character literal; use ?\s')
30
- expect(cop.highlights).to eq([' '])
30
+ expect(cop.highlights).to eq([' '])
31
+ end
31
32
  end
32
33
  end
33
34
  end
@@ -120,4 +120,40 @@ describe Rubocop::Cop::Lint::RescueException do
120
120
  'end'])
121
121
  expect(cop.offenses).to be_empty
122
122
  end
123
+
124
+ context 'without exception capture' do
125
+ let(:source) do
126
+ ['begin',
127
+ 'rescue Exception',
128
+ 'end']
129
+ end
130
+
131
+ let(:corrected_source) do
132
+ ['begin', # rubocop:disable WordArray
133
+ 'rescue',
134
+ 'end'].join("\n")
135
+ end
136
+
137
+ it 'autocorrects by unspecifying the exception class' do
138
+ expect(autocorrect_source(cop, source)).to eq(corrected_source)
139
+ end
140
+ end
141
+
142
+ context 'with exception capture' do
143
+ let(:source) do
144
+ ['begin',
145
+ 'rescue Exception => e',
146
+ 'end']
147
+ end
148
+
149
+ let(:corrected_source) do
150
+ ['begin',
151
+ 'rescue => e',
152
+ 'end'].join("\n")
153
+ end
154
+
155
+ it 'autocorrects by unspecifying the exception class' do
156
+ expect(autocorrect_source(cop, source)).to eq(corrected_source)
157
+ end
158
+ end
123
159
  end
@@ -22,7 +22,7 @@ describe Rubocop::Cop::Lint::ShadowingOuterLocalVariable do
22
22
  inspect_source(cop, source)
23
23
  expect(cop.offenses.size).to eq(1)
24
24
  expect(cop.offenses.first.message)
25
- .to include('Shadowing outer local variable - `foo`')
25
+ .to eq('Shadowing outer local variable - `foo`.')
26
26
  expect(cop.offenses.first.line).to eq(4)
27
27
  end
28
28
 
@@ -46,7 +46,7 @@ describe Rubocop::Cop::Lint::ShadowingOuterLocalVariable do
46
46
  inspect_source(cop, source)
47
47
  expect(cop.offenses.size).to eq(1)
48
48
  expect(cop.offenses.first.message)
49
- .to include('Shadowing outer local variable - `foo`')
49
+ .to eq('Shadowing outer local variable - `foo`.')
50
50
  expect(cop.offenses.first.line).to eq(4)
51
51
  end
52
52
 
@@ -72,7 +72,7 @@ describe Rubocop::Cop::Lint::ShadowingOuterLocalVariable do
72
72
  inspect_source(cop, source)
73
73
  expect(cop.offenses.size).to eq(1)
74
74
  expect(cop.offenses.first.message)
75
- .to include('Shadowing outer local variable - `foo`')
75
+ .to eq('Shadowing outer local variable - `foo`.')
76
76
  expect(cop.offenses.first.line).to eq(4)
77
77
  end
78
78
 
@@ -97,7 +97,7 @@ describe Rubocop::Cop::Lint::ShadowingOuterLocalVariable do
97
97
  inspect_source(cop, source)
98
98
  expect(cop.offenses.size).to eq(1)
99
99
  expect(cop.offenses.first.message)
100
- .to include('Shadowing outer local variable - `foo`')
100
+ .to eq('Shadowing outer local variable - `foo`.')
101
101
  expect(cop.offenses.first.line).to eq(4)
102
102
  end
103
103
 
@@ -0,0 +1,179 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Rubocop::Cop::Lint::UnderscorePrefixedVariableName do
6
+ subject(:cop) { described_class.new }
7
+
8
+ before do
9
+ inspect_source(cop, source)
10
+ end
11
+
12
+ context 'when an underscore-prefixed variable is used' do
13
+ let(:source) { <<-END }
14
+ def some_method
15
+ _foo = 1
16
+ puts _foo
17
+ end
18
+ END
19
+
20
+ it 'registers an offense' do
21
+ expect(cop.offenses.size).to eq(1)
22
+ expect(cop.offenses.first.message)
23
+ .to eq('Do not use prefix `_` for a variable that is used.')
24
+ expect(cop.offenses.first.severity.name).to eq(:warning)
25
+ expect(cop.offenses.first.line).to eq(2)
26
+ expect(cop.highlights).to eq(['_foo'])
27
+ end
28
+ end
29
+
30
+ context 'when non-underscore-prefixed variable is used' do
31
+ let(:source) { <<-END }
32
+ def some_method
33
+ foo = 1
34
+ puts foo
35
+ end
36
+ END
37
+
38
+ it 'accepts' do
39
+ expect(cop.offenses).to be_empty
40
+ end
41
+ end
42
+
43
+ context 'when an underscore-prefixed variable is reassigned' do
44
+ let(:source) { <<-END }
45
+ def some_method
46
+ _foo = 1
47
+ _foo = 2
48
+ end
49
+ END
50
+
51
+ it 'accepts' do
52
+ expect(cop.offenses).to be_empty
53
+ end
54
+ end
55
+
56
+ context 'when an underscore-prefixed method argument is used' do
57
+ let(:source) { <<-END }
58
+ def some_method(_foo)
59
+ puts _foo
60
+ end
61
+ END
62
+
63
+ it 'registers an offense' do
64
+ expect(cop.offenses.size).to eq(1)
65
+ expect(cop.offenses.first.line).to eq(1)
66
+ expect(cop.highlights).to eq(['_foo'])
67
+ end
68
+ end
69
+
70
+ context 'when an underscore-prefixed block argument is used' do
71
+ let(:source) { <<-END }
72
+ 1.times do |_foo|
73
+ puts _foo
74
+ end
75
+ END
76
+
77
+ it 'registers an offense' do
78
+ expect(cop.offenses.size).to eq(1)
79
+ expect(cop.offenses.first.line).to eq(1)
80
+ expect(cop.highlights).to eq(['_foo'])
81
+ end
82
+ end
83
+
84
+ context 'when an underscore-prefixed variable in top-level scope is used' do
85
+ let(:source) { <<-END }
86
+ _foo = 1
87
+ puts _foo
88
+ END
89
+
90
+ it 'registers an offense' do
91
+ expect(cop.offenses.size).to eq(1)
92
+ expect(cop.offenses.first.line).to eq(1)
93
+ expect(cop.highlights).to eq(['_foo'])
94
+ end
95
+ end
96
+
97
+ context 'when an underscore-prefixed variable is captured by a block' do
98
+ let(:source) { <<-END }
99
+ _foo = 1
100
+ 1.times do
101
+ _foo = 2
102
+ end
103
+ END
104
+
105
+ it 'accepts' do
106
+ expect(cop.offenses).to be_empty
107
+ end
108
+ end
109
+
110
+ context 'when an underscore-prefixed named capture variable is used' do
111
+ let(:source) { <<-END }
112
+ /(?<_foo>\\w+)/ =~ 'FOO'
113
+ puts _foo
114
+ END
115
+
116
+ it 'registers an offense' do
117
+ expect(cop.offenses.size).to eq(1)
118
+ expect(cop.offenses.first.line).to eq(1)
119
+ expect(cop.highlights).to eq(['/(?<_foo>\\w+)/'])
120
+ end
121
+ end
122
+
123
+ context 'in a method calling `super` without arguments' do
124
+ context 'when an underscore-prefixed argument is not used explicitly' do
125
+ let(:source) { <<-END }
126
+ def some_method(*_)
127
+ super
128
+ end
129
+ END
130
+
131
+ it 'accepts' do
132
+ expect(cop.offenses).to be_empty
133
+ end
134
+ end
135
+
136
+ context 'when an underscore-prefixed argument is used explicitly' do
137
+ let(:source) { <<-END }
138
+ def some_method(*_)
139
+ super
140
+ puts _
141
+ end
142
+ END
143
+
144
+ it 'registers an offense' do
145
+ expect(cop.offenses.size).to eq(1)
146
+ expect(cop.offenses.first.line).to eq(1)
147
+ expect(cop.highlights).to eq(['_'])
148
+ end
149
+ end
150
+ end
151
+
152
+ context 'in a method calling `super` with arguments' do
153
+ context 'when an underscore-prefixed argument is not used' do
154
+ let(:source) { <<-END }
155
+ def some_method(*_)
156
+ super(:something)
157
+ end
158
+ END
159
+
160
+ it 'accepts' do
161
+ expect(cop.offenses).to be_empty
162
+ end
163
+ end
164
+
165
+ context 'when an underscore-prefixed argument is used explicitly' do
166
+ let(:source) { <<-END }
167
+ def some_method(*_)
168
+ super(*_)
169
+ end
170
+ END
171
+
172
+ it 'registers an offense' do
173
+ expect(cop.offenses.size).to eq(1)
174
+ expect(cop.offenses.first.line).to eq(1)
175
+ expect(cop.highlights).to eq(['_'])
176
+ end
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,147 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Rubocop::Cop::Lint::UnusedBlockArgument do
6
+ subject(:cop) { described_class.new }
7
+
8
+ before do
9
+ inspect_source(cop, source)
10
+ end
11
+
12
+ context 'when a block takes multiple arguments' do
13
+ context 'and an argument is unused' do
14
+ let(:source) { <<-END }
15
+ hash = { foo: 'FOO', bar: 'BAR' }
16
+ hash.each do |key, value|
17
+ puts key
18
+ end
19
+ END
20
+
21
+ it 'registers an offense' do
22
+ expect(cop.offenses.size).to eq(1)
23
+ expect(cop.offenses.first.message).to eq(
24
+ 'Unused block argument - `value`. ' \
25
+ "If it's necessary, use `_` or `_value` as an argument name " \
26
+ "to indicate that it won't be used."
27
+ )
28
+ expect(cop.offenses.first.severity.name).to eq(:warning)
29
+ expect(cop.offenses.first.line).to eq(2)
30
+ expect(cop.highlights).to eq(['value'])
31
+ end
32
+ end
33
+
34
+ context 'and all the arguments are unused' do
35
+ let(:source) { <<-END }
36
+ hash = { foo: 'FOO', bar: 'BAR' }
37
+ hash.each do |key, value|
38
+ puts :something
39
+ end
40
+ END
41
+
42
+ it 'registers offenses and suggests omitting them' do
43
+ expect(cop.offenses.size).to eq(2)
44
+ expect(cop.offenses.first.message).to eq(
45
+ 'Unused block argument - `key`. ' \
46
+ "You can omit all the arguments if you don't care about them."
47
+ )
48
+ expect(cop.offenses.first.line).to eq(2)
49
+ expect(cop.highlights).to eq(%w(key value))
50
+ end
51
+ end
52
+ end
53
+
54
+ context 'when a block takes single argument' do
55
+ context 'and the argument is unused' do
56
+ let(:source) { <<-END }
57
+ 1.times do |index|
58
+ puts :something
59
+ end
60
+ END
61
+
62
+ it 'registers an offense and suggests omitting that' do
63
+ expect(cop.offenses.size).to eq(1)
64
+ expect(cop.offenses.first.message).to eq(
65
+ 'Unused block argument - `index`. ' \
66
+ "You can omit the argument if you don't care about it."
67
+ )
68
+ expect(cop.offenses.first.line).to eq(1)
69
+ expect(cop.highlights).to eq(['index'])
70
+ end
71
+ end
72
+ end
73
+
74
+ context 'when a lambda block takes arguments' do
75
+ context 'and all the arguments are unused' do
76
+ let(:source) { <<-END }
77
+ -> (foo, bar) { do_something }
78
+ END
79
+
80
+ it 'registers offenses and suggests using a proc' do
81
+ expect(cop.offenses.size).to eq(2)
82
+ expect(cop.offenses.first.message).to eq(
83
+ 'Unused block argument - `foo`. ' \
84
+ "If it's necessary, use `_` or `_foo` as an argument name " \
85
+ "to indicate that it won't be used. " \
86
+ 'Also consider using a proc without arguments instead of a lambda ' \
87
+ "if you want it to accept any arguments but don't care about them."
88
+
89
+ )
90
+ expect(cop.offenses.first.line).to eq(1)
91
+ expect(cop.highlights).to eq(%w(foo bar))
92
+ end
93
+ end
94
+
95
+ context 'and an arguments is unused' do
96
+ let(:source) { <<-END }
97
+ -> (foo, bar) { puts bar }
98
+ END
99
+
100
+ it 'registers an offense' do
101
+ expect(cop.offenses.size).to eq(1)
102
+ expect(cop.offenses.first.message).to eq(
103
+ 'Unused block argument - `foo`. ' \
104
+ "If it's necessary, use `_` or `_foo` as an argument name " \
105
+ "to indicate that it won't be used."
106
+ )
107
+ expect(cop.offenses.first.line).to eq(1)
108
+ expect(cop.highlights).to eq(['foo'])
109
+ end
110
+ end
111
+ end
112
+
113
+ context 'when an underscore-prefixed block argument is not used' do
114
+ let(:source) { <<-END }
115
+ 1.times do |_index|
116
+ puts 'foo'
117
+ end
118
+ END
119
+
120
+ it 'accepts' do
121
+ expect(cop.offenses).to be_empty
122
+ end
123
+ end
124
+
125
+ context 'when a method argument is not used' do
126
+ let(:source) { <<-END }
127
+ def some_method(foo)
128
+ end
129
+ END
130
+
131
+ it 'does not care' do
132
+ expect(cop.offenses).to be_empty
133
+ end
134
+ end
135
+
136
+ context 'when a variable is not used' do
137
+ let(:source) { <<-END }
138
+ 1.times do
139
+ foo = 1
140
+ end
141
+ END
142
+
143
+ it 'does not care' do
144
+ expect(cop.offenses).to be_empty
145
+ end
146
+ end
147
+ end