rubocop 0.13.1 → 0.14.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 (222) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +3 -0
  3. data/CHANGELOG.md +42 -0
  4. data/README.md +59 -54
  5. data/config/default.yml +124 -95
  6. data/config/disabled.yml +4 -0
  7. data/config/enabled.yml +286 -264
  8. data/lib/rubocop.rb +10 -3
  9. data/lib/rubocop/cli.rb +10 -171
  10. data/lib/rubocop/cop/cop.rb +14 -11
  11. data/lib/rubocop/cop/lint/debugger.rb +35 -0
  12. data/lib/rubocop/cop/lint/syntax.rb +34 -0
  13. data/lib/rubocop/cop/offence.rb +15 -23
  14. data/lib/rubocop/cop/style/align_array.rb +1 -10
  15. data/lib/rubocop/cop/style/align_hash.rb +14 -2
  16. data/lib/rubocop/cop/style/align_parameters.rb +1 -11
  17. data/lib/rubocop/cop/style/and_or.rb +13 -11
  18. data/lib/rubocop/cop/style/attr.rb +1 -1
  19. data/lib/rubocop/cop/style/autocorrect_alignment.rb +31 -6
  20. data/lib/rubocop/cop/style/blocks.rb +14 -6
  21. data/lib/rubocop/cop/style/character_literal.rb +1 -1
  22. data/lib/rubocop/cop/style/class_length.rb +38 -0
  23. data/lib/rubocop/cop/style/collection_methods.rb +1 -1
  24. data/lib/rubocop/cop/style/colon_method_call.rb +1 -1
  25. data/lib/rubocop/cop/style/configurable_naming.rb +47 -0
  26. data/lib/rubocop/cop/style/def_parentheses.rb +2 -2
  27. data/lib/rubocop/cop/style/empty_literal.rb +1 -1
  28. data/lib/rubocop/cop/style/even_odd.rb +7 -3
  29. data/lib/rubocop/cop/style/hash_methods.rb +1 -1
  30. data/lib/rubocop/cop/style/hash_syntax.rb +36 -7
  31. data/lib/rubocop/cop/style/lambda_call.rb +36 -11
  32. data/lib/rubocop/cop/style/method_call_parentheses.rb +1 -1
  33. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +41 -0
  34. data/lib/rubocop/cop/style/method_length.rb +2 -13
  35. data/lib/rubocop/cop/style/method_name.rb +42 -0
  36. data/lib/rubocop/cop/style/numeric_literals.rb +4 -0
  37. data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
  38. data/lib/rubocop/cop/style/raise_args.rb +60 -0
  39. data/lib/rubocop/cop/style/redundant_exception.rb +32 -0
  40. data/lib/rubocop/cop/style/redundant_return.rb +14 -5
  41. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  42. data/lib/rubocop/cop/style/semicolon.rb +13 -4
  43. data/lib/rubocop/cop/style/signal_exception.rb +63 -14
  44. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  45. data/lib/rubocop/cop/style/string_help.rb +4 -1
  46. data/lib/rubocop/cop/style/string_literals.rb +34 -11
  47. data/lib/rubocop/cop/style/surrounding_space.rb +64 -17
  48. data/lib/rubocop/cop/style/tab.rb +1 -1
  49. data/lib/rubocop/cop/style/variable_name.rb +44 -0
  50. data/lib/rubocop/cop/style/when_then.rb +1 -1
  51. data/lib/rubocop/cop/style/while_until_do.rb +1 -1
  52. data/lib/rubocop/cop/team.rb +4 -7
  53. data/lib/rubocop/cop/util.rb +12 -0
  54. data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
  55. data/lib/rubocop/formatter/emacs_style_formatter.rb +5 -2
  56. data/lib/rubocop/formatter/json_formatter.rb +1 -0
  57. data/lib/rubocop/formatter/progress_formatter.rb +9 -13
  58. data/lib/rubocop/formatter/simple_text_formatter.rb +63 -15
  59. data/lib/rubocop/options.rb +184 -0
  60. data/lib/rubocop/processed_source.rb +4 -0
  61. data/lib/rubocop/version.rb +1 -1
  62. data/rubocop.gemspec +1 -1
  63. data/spec/rubocop/cli_spec.rb +870 -1001
  64. data/spec/rubocop/config_spec.rb +13 -13
  65. data/spec/rubocop/config_store_spec.rb +38 -37
  66. data/spec/rubocop/cop/commissioner_spec.rb +42 -46
  67. data/spec/rubocop/cop/cop_spec.rb +72 -77
  68. data/spec/rubocop/cop/corrector_spec.rb +51 -55
  69. data/spec/rubocop/cop/lint/assignment_in_condition_spec.rb +100 -106
  70. data/spec/rubocop/cop/lint/block_alignment_spec.rb +403 -409
  71. data/spec/rubocop/cop/lint/debugger_spec.rb +39 -0
  72. data/spec/rubocop/cop/lint/empty_ensure_spec.rb +19 -25
  73. data/spec/rubocop/cop/lint/end_alignment_spec.rb +50 -56
  74. data/spec/rubocop/cop/lint/end_in_method_spec.rb +20 -26
  75. data/spec/rubocop/cop/lint/ensure_return_spec.rb +30 -36
  76. data/spec/rubocop/cop/lint/eval_spec.rb +25 -31
  77. data/spec/rubocop/cop/lint/handle_exceptions_spec.rb +22 -28
  78. data/spec/rubocop/cop/lint/literal_in_condition_spec.rb +51 -57
  79. data/spec/rubocop/cop/lint/loop_spec.rb +17 -23
  80. data/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +51 -57
  81. data/spec/rubocop/cop/lint/rescue_exception_spec.rb +105 -111
  82. data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +230 -236
  83. data/spec/rubocop/cop/lint/syntax_spec.rb +33 -0
  84. data/spec/rubocop/cop/lint/unreachable_code_spec.rb +51 -57
  85. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +1544 -1550
  86. data/spec/rubocop/cop/lint/useless_comparison_spec.rb +21 -27
  87. data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +119 -125
  88. data/spec/rubocop/cop/lint/void_spec.rb +50 -56
  89. data/spec/rubocop/cop/offence_spec.rb +101 -131
  90. data/spec/rubocop/cop/rails/has_and_belongs_to_many_spec.rb +6 -12
  91. data/spec/rubocop/cop/rails/read_attribute_spec.rb +6 -12
  92. data/spec/rubocop/cop/rails/validation_spec.rb +13 -19
  93. data/spec/rubocop/cop/style/access_control_spec.rb +146 -152
  94. data/spec/rubocop/cop/style/alias_spec.rb +30 -36
  95. data/spec/rubocop/cop/style/align_array_spec.rb +57 -56
  96. data/spec/rubocop/cop/style/align_hash_spec.rb +256 -237
  97. data/spec/rubocop/cop/style/align_parameters_spec.rb +216 -202
  98. data/spec/rubocop/cop/style/and_or_spec.rb +51 -55
  99. data/spec/rubocop/cop/style/ascii_comments_spec.rb +14 -20
  100. data/spec/rubocop/cop/style/ascii_identifiers_spec.rb +26 -32
  101. data/spec/rubocop/cop/style/attr_spec.rb +11 -17
  102. data/spec/rubocop/cop/style/begin_block_spec.rb +6 -12
  103. data/spec/rubocop/cop/style/block_comments_spec.rb +13 -19
  104. data/spec/rubocop/cop/style/block_nesting_spec.rb +134 -140
  105. data/spec/rubocop/cop/style/blocks_spec.rb +81 -84
  106. data/spec/rubocop/cop/style/case_equality_spec.rb +5 -11
  107. data/spec/rubocop/cop/style/case_indentation_spec.rb +71 -77
  108. data/spec/rubocop/cop/style/character_literal_spec.rb +31 -37
  109. data/spec/rubocop/cop/style/class_and_module_camel_case_spec.rb +31 -37
  110. data/spec/rubocop/cop/style/class_length_spec.rb +65 -0
  111. data/spec/rubocop/cop/style/class_methods_spec.rb +35 -41
  112. data/spec/rubocop/cop/style/class_vars_spec.rb +11 -17
  113. data/spec/rubocop/cop/style/collection_methods_spec.rb +41 -47
  114. data/spec/rubocop/cop/style/colon_method_call_spec.rb +45 -51
  115. data/spec/rubocop/cop/style/comment_annotation_spec.rb +79 -85
  116. data/spec/rubocop/cop/style/constant_name_spec.rb +45 -51
  117. data/spec/rubocop/cop/style/def_with_parentheses_spec.rb +28 -34
  118. data/spec/rubocop/cop/style/def_without_parentheses_spec.rb +23 -29
  119. data/spec/rubocop/cop/style/documentation_spec.rb +60 -66
  120. data/spec/rubocop/cop/style/dot_position_spec.rb +50 -56
  121. data/spec/rubocop/cop/style/empty_line_between_defs_spec.rb +109 -110
  122. data/spec/rubocop/cop/style/empty_lines_spec.rb +28 -34
  123. data/spec/rubocop/cop/style/empty_literal_spec.rb +80 -86
  124. data/spec/rubocop/cop/style/encoding_spec.rb +50 -56
  125. data/spec/rubocop/cop/style/end_block_spec.rb +6 -12
  126. data/spec/rubocop/cop/style/end_of_line_spec.rb +13 -19
  127. data/spec/rubocop/cop/style/even_odd_spec.rb +69 -41
  128. data/spec/rubocop/cop/style/favor_join_spec.rb +21 -27
  129. data/spec/rubocop/cop/style/favor_modifier_spec.rb +180 -176
  130. data/spec/rubocop/cop/style/favor_sprintf_spec.rb +41 -47
  131. data/spec/rubocop/cop/style/favor_unless_over_negated_if_spec.rb +63 -69
  132. data/spec/rubocop/cop/style/favor_until_over_negated_while_spec.rb +32 -38
  133. data/spec/rubocop/cop/style/final_newline_spec.rb +16 -22
  134. data/spec/rubocop/cop/style/for_spec.rb +18 -24
  135. data/spec/rubocop/cop/style/global_vars_spec.rb +23 -29
  136. data/spec/rubocop/cop/style/hash_methods_spec.rb +39 -45
  137. data/spec/rubocop/cop/style/hash_syntax_spec.rb +93 -65
  138. data/spec/rubocop/cop/style/if_with_semicolon_spec.rb +11 -17
  139. data/spec/rubocop/cop/style/indentation_width_spec.rb +377 -383
  140. data/spec/rubocop/cop/style/lambda_call_spec.rb +40 -22
  141. data/spec/rubocop/cop/style/lambda_spec.rb +30 -34
  142. data/spec/rubocop/cop/style/leading_comment_space_spec.rb +44 -50
  143. data/spec/rubocop/cop/style/line_length_spec.rb +11 -17
  144. data/spec/rubocop/cop/style/method_call_parentheses_spec.rb +14 -20
  145. data/spec/rubocop/cop/style/method_called_on_do_end_block_spec.rb +60 -0
  146. data/spec/rubocop/cop/style/method_length_spec.rb +127 -133
  147. data/spec/rubocop/cop/style/method_name_spec.rb +103 -0
  148. data/spec/rubocop/cop/style/module_function_spec.rb +16 -22
  149. data/spec/rubocop/cop/style/multiline_block_chain_spec.rb +64 -70
  150. data/spec/rubocop/cop/style/multiline_if_then_spec.rb +74 -80
  151. data/spec/rubocop/cop/style/nil_comparison_spec.rb +20 -26
  152. data/spec/rubocop/cop/style/not_spec.rb +13 -19
  153. data/spec/rubocop/cop/style/numeric_literals_spec.rb +36 -35
  154. data/spec/rubocop/cop/style/one_line_conditional_spec.rb +5 -11
  155. data/spec/rubocop/cop/style/op_method_spec.rb +61 -67
  156. data/spec/rubocop/cop/style/parameter_lists_spec.rb +31 -37
  157. data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +62 -68
  158. data/spec/rubocop/cop/style/perl_backrefs_spec.rb +9 -15
  159. data/spec/rubocop/cop/style/proc_spec.rb +13 -19
  160. data/spec/rubocop/cop/style/raise_args_spec.rb +55 -0
  161. data/spec/rubocop/cop/style/reduce_arguments_spec.rb +49 -55
  162. data/spec/rubocop/cop/style/redundant_begin_spec.rb +47 -53
  163. data/spec/rubocop/cop/style/redundant_exception_spec.rb +27 -0
  164. data/spec/rubocop/cop/style/redundant_return_spec.rb +164 -72
  165. data/spec/rubocop/cop/style/redundant_self_spec.rb +109 -115
  166. data/spec/rubocop/cop/style/regexp_literal_spec.rb +57 -63
  167. data/spec/rubocop/cop/style/rescue_modifier_spec.rb +99 -105
  168. data/spec/rubocop/cop/style/semicolon_spec.rb +95 -90
  169. data/spec/rubocop/cop/style/signal_exception_spec.rb +248 -95
  170. data/spec/rubocop/cop/style/single_line_methods_spec.rb +45 -51
  171. data/spec/rubocop/cop/style/space_after_colon_spec.rb +14 -20
  172. data/spec/rubocop/cop/style/space_after_comma_spec.rb +16 -22
  173. data/spec/rubocop/cop/style/space_after_control_keyword_spec.rb +48 -54
  174. data/spec/rubocop/cop/style/space_after_method_name_spec.rb +49 -55
  175. data/spec/rubocop/cop/style/space_after_semicolon_spec.rb +10 -16
  176. data/spec/rubocop/cop/style/space_around_block_braces_spec.rb +118 -50
  177. data/spec/rubocop/cop/style/space_around_equals_in_default_parameter_spec.rb +18 -24
  178. data/spec/rubocop/cop/style/space_around_operators_spec.rb +204 -210
  179. data/spec/rubocop/cop/style/space_before_modifier_keyword_spec.rb +36 -42
  180. data/spec/rubocop/cop/style/space_inside_brackets_spec.rb +39 -45
  181. data/spec/rubocop/cop/style/space_inside_hash_literal_braces_spec.rb +62 -68
  182. data/spec/rubocop/cop/style/space_inside_parens_spec.rb +18 -24
  183. data/spec/rubocop/cop/style/special_global_vars_spec.rb +40 -46
  184. data/spec/rubocop/cop/style/string_literals_spec.rb +148 -70
  185. data/spec/rubocop/cop/style/symbol_array_spec.rb +28 -34
  186. data/spec/rubocop/cop/style/symbol_name_spec.rb +132 -138
  187. data/spec/rubocop/cop/style/tab_spec.rb +9 -15
  188. data/spec/rubocop/cop/style/ternary_operator_spec.rb +25 -31
  189. data/spec/rubocop/cop/style/trailing_blank_lines_spec.rb +10 -16
  190. data/spec/rubocop/cop/style/trailing_whitespace_spec.rb +14 -20
  191. data/spec/rubocop/cop/style/trivial_accessors_spec.rb +374 -380
  192. data/spec/rubocop/cop/style/unless_else_spec.rb +17 -23
  193. data/spec/rubocop/cop/style/variable_interpolation_spec.rb +36 -42
  194. data/spec/rubocop/cop/style/variable_name_spec.rb +83 -0
  195. data/spec/rubocop/cop/style/when_then_spec.rb +31 -37
  196. data/spec/rubocop/cop/style/while_until_do_spec.rb +47 -53
  197. data/spec/rubocop/cop/style/word_array_spec.rb +49 -55
  198. data/spec/rubocop/cop/team_spec.rb +125 -127
  199. data/spec/rubocop/cop/variable_inspector/assignment_spec.rb +176 -180
  200. data/spec/rubocop/cop/variable_inspector/locatable_spec.rb +580 -586
  201. data/spec/rubocop/cop/variable_inspector/scope_spec.rb +140 -146
  202. data/spec/rubocop/cop/variable_inspector/variable_spec.rb +52 -58
  203. data/spec/rubocop/cop/variable_inspector/variable_table_spec.rb +217 -223
  204. data/spec/rubocop/cop/variable_inspector_spec.rb +15 -19
  205. data/spec/rubocop/formatter/clang_style_formatter_spec.rb +35 -36
  206. data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +2 -2
  207. data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +24 -1
  208. data/spec/rubocop/formatter/file_list_formatter_spec.rb +1 -1
  209. data/spec/rubocop/formatter/formatter_set_spec.rb +3 -3
  210. data/spec/rubocop/formatter/json_formatter_spec.rb +7 -2
  211. data/spec/rubocop/formatter/offence_count_formatter_spec.rb +1 -1
  212. data/spec/rubocop/formatter/progress_formatter_spec.rb +1 -1
  213. data/spec/rubocop/formatter/simple_text_formatter_spec.rb +55 -6
  214. data/spec/rubocop/options_spec.rb +233 -0
  215. data/spec/rubocop/processed_source_spec.rb +85 -42
  216. data/spec/rubocop/source_parser_spec.rb +108 -110
  217. data/spec/rubocop/target_finder_spec.rb +125 -127
  218. data/spec/rubocop/token_spec.rb +15 -17
  219. metadata +32 -8
  220. data/lib/rubocop/backports/bsearch.rb +0 -39
  221. data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +0 -77
  222. data/spec/rubocop/cop/style/method_and_variable_snake_case_spec.rb +0 -102
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Rubocop::Cop::Lint::Syntax do
6
+ describe '.offences_from_diagnostic' do
7
+ subject(:offence) { described_class.offence_from_diagnostic(diagnostic) }
8
+ let(:diagnostic) { Parser::Diagnostic.new(level, message, location) }
9
+ let(:level) { :warning }
10
+ let(:message) { 'This is a message' }
11
+ let(:location) { double('location').as_null_object }
12
+
13
+ it 'returns an offence' do
14
+ expect(offence).to be_a(Rubocop::Cop::Offence)
15
+ end
16
+
17
+ it "sets diagnostic's level to offence's severity" do
18
+ expect(offence.severity).to eq(level)
19
+ end
20
+
21
+ it "sets diagnostic's message to offence's message" do
22
+ expect(offence.message).to eq(message)
23
+ end
24
+
25
+ it "sets diagnostic's location to offence's location" do
26
+ expect(offence.location).to eq(location)
27
+ end
28
+
29
+ it 'sets Sytanx as cop name' do
30
+ expect(offence.cop_name).to eq('Syntax')
31
+ end
32
+ end
33
+ end
@@ -2,68 +2,62 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- module Rubocop
6
- module Cop
7
- module Lint
8
- describe UnreachableCode do
9
- subject(:uc) { UnreachableCode.new }
10
-
11
- UnreachableCode::NODE_TYPES.each do |t|
12
- it "registers an offence for #{t} before other statements" do
13
- inspect_source(uc,
14
- ['foo = 5',
15
- "#{t}",
16
- 'bar'
17
- ])
18
- expect(uc.offences.size).to eq(1)
19
- end
5
+ describe Rubocop::Cop::Lint::UnreachableCode do
6
+ subject(:cop) { described_class.new }
7
+
8
+ described_class::NODE_TYPES.each do |t|
9
+ it "registers an offence for #{t} before other statements" do
10
+ inspect_source(cop,
11
+ ['foo = 5',
12
+ "#{t}",
13
+ 'bar'
14
+ ])
15
+ expect(cop.offences.size).to eq(1)
16
+ end
20
17
 
21
- it "accepts code with conditional #{t}" do
22
- inspect_source(uc,
23
- ['foo = 5',
24
- "#{t} if test",
25
- 'bar'
26
- ])
27
- expect(uc.offences).to be_empty
28
- end
18
+ it "accepts code with conditional #{t}" do
19
+ inspect_source(cop,
20
+ ['foo = 5',
21
+ "#{t} if test",
22
+ 'bar'
23
+ ])
24
+ expect(cop.offences).to be_empty
25
+ end
29
26
 
30
- it "accepts #{t} as the final expression" do
31
- inspect_source(uc,
32
- ['foo = 5',
33
- "#{t} if test"
34
- ])
35
- expect(uc.offences).to be_empty
36
- end
37
- end
27
+ it "accepts #{t} as the final expression" do
28
+ inspect_source(cop,
29
+ ['foo = 5',
30
+ "#{t} if test"
31
+ ])
32
+ expect(cop.offences).to be_empty
33
+ end
34
+ end
38
35
 
39
- UnreachableCode::FLOW_COMMANDS.each do |t|
40
- it "registers an offence for #{t} before other statements" do
41
- inspect_source(uc,
42
- ['foo = 5',
43
- "#{t} something",
44
- 'bar'
45
- ])
46
- expect(uc.offences.size).to eq(1)
47
- end
36
+ described_class::FLOW_COMMANDS.each do |t|
37
+ it "registers an offence for #{t} before other statements" do
38
+ inspect_source(cop,
39
+ ['foo = 5',
40
+ "#{t} something",
41
+ 'bar'
42
+ ])
43
+ expect(cop.offences.size).to eq(1)
44
+ end
48
45
 
49
- it "accepts code with conditional #{t}" do
50
- inspect_source(uc,
51
- ['foo = 5',
52
- "#{t} something if test",
53
- 'bar'
54
- ])
55
- expect(uc.offences).to be_empty
56
- end
46
+ it "accepts code with conditional #{t}" do
47
+ inspect_source(cop,
48
+ ['foo = 5',
49
+ "#{t} something if test",
50
+ 'bar'
51
+ ])
52
+ expect(cop.offences).to be_empty
53
+ end
57
54
 
58
- it "accepts #{t} as the final expression" do
59
- inspect_source(uc,
60
- ['foo = 5',
61
- "#{t} something if test"
62
- ])
63
- expect(uc.offences).to be_empty
64
- end
65
- end
66
- end
55
+ it "accepts #{t} as the final expression" do
56
+ inspect_source(cop,
57
+ ['foo = 5',
58
+ "#{t} something if test"
59
+ ])
60
+ expect(cop.offences).to be_empty
67
61
  end
68
62
  end
69
63
  end
@@ -2,1557 +2,1551 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- module Rubocop
6
- module Cop
7
- module Lint
8
- describe UselessAssignment do
9
- subject(:cop) { UselessAssignment.new }
10
-
11
- context 'when a variable is assigned and unreferenced in a method' do
12
- let(:source) do
13
- [
14
- 'class SomeClass',
15
- ' foo = 1',
16
- ' puts foo',
17
- ' def some_method',
18
- ' foo = 2',
19
- ' bar = 3',
20
- ' puts bar',
21
- ' end',
22
- 'end'
23
- ]
24
- end
25
-
26
- it 'registers an offence' do
27
- inspect_source(cop, source)
28
- expect(cop.offences.size).to eq(1)
29
- expect(cop.offences.first.message)
30
- .to eq('Useless assignment to variable - foo')
31
- expect(cop.offences.first.line).to eq(5)
32
- expect(cop.highlights).to eq(['foo'])
33
- end
34
-
35
- include_examples 'mimics MRI 2.0'
36
- end
37
-
38
- context 'when a variable is assigned and unreferenced ' +
39
- 'in a singleton method defined with self keyword' do
40
- let(:source) do
41
- [
42
- 'class SomeClass',
43
- ' foo = 1',
44
- ' puts foo',
45
- ' def self.some_method',
46
- ' foo = 2',
47
- ' bar = 3',
48
- ' puts bar',
49
- ' end',
50
- 'end'
51
- ]
52
- end
53
-
54
- it 'registers an offence' do
55
- inspect_source(cop, source)
56
- expect(cop.offences.size).to eq(1)
57
- expect(cop.offences.first.message)
58
- .to eq('Useless assignment to variable - foo')
59
- expect(cop.offences.first.line).to eq(5)
60
- expect(cop.highlights).to eq(['foo'])
61
- end
62
-
63
- include_examples 'mimics MRI 2.0'
64
- end
65
-
66
- context 'when a variable is assigned and unreferenced ' +
67
- 'in a singleton method defined with variable name' do
68
- let(:source) do
69
- [
70
- '1.times do',
71
- ' foo = 1',
72
- ' puts foo',
73
- ' instance = Object.new',
74
- ' def instance.some_method',
75
- ' foo = 2',
76
- ' bar = 3',
77
- ' puts bar',
78
- ' end',
79
- 'end'
80
- ]
81
- end
82
-
83
- it 'registers an offence' do
84
- inspect_source(cop, source)
85
- expect(cop.offences.size).to eq(1)
86
- expect(cop.offences.first.message)
87
- .to eq('Useless assignment to variable - foo')
88
- expect(cop.offences.first.line).to eq(6)
89
- expect(cop.highlights).to eq(['foo'])
90
- end
91
-
92
- include_examples 'mimics MRI 2.0'
93
- end
94
-
95
- context 'when a variable is assigned and unreferenced in a class' do
96
- let(:source) do
97
- [
98
- '1.times do',
99
- ' foo = 1',
100
- ' puts foo',
101
- ' class SomeClass',
102
- ' foo = 2',
103
- ' bar = 3',
104
- ' puts bar',
105
- ' end',
106
- 'end'
107
- ]
108
- end
109
-
110
- it 'registers an offence' do
111
- inspect_source(cop, source)
112
- expect(cop.offences.size).to eq(1)
113
- expect(cop.offences.first.message)
114
- .to eq('Useless assignment to variable - foo')
115
- expect(cop.offences.first.line).to eq(5)
116
- expect(cop.highlights).to eq(['foo'])
117
- end
118
-
119
- include_examples 'mimics MRI 2.0'
120
- end
121
-
122
- context 'when a variable is assigned and unreferenced in a class ' +
123
- 'subclassing another class stored in local variable' do
124
- let(:source) do
125
- [
126
- '1.times do',
127
- ' foo = 1',
128
- ' puts foo',
129
- ' array_class = Array',
130
- ' class SomeClass < array_class',
131
- ' foo = 2',
132
- ' bar = 3',
133
- ' puts bar',
134
- ' end',
135
- 'end'
136
- ]
137
- end
138
-
139
- it 'registers an offence' do
140
- inspect_source(cop, source)
141
- expect(cop.offences.size).to eq(1)
142
- expect(cop.offences.first.message)
143
- .to eq('Useless assignment to variable - foo')
144
- expect(cop.offences.first.line).to eq(6)
145
- expect(cop.highlights).to eq(['foo'])
146
- end
147
-
148
- include_examples 'mimics MRI 2.0'
149
- end
150
-
151
- context 'when a variable is assigned and unreferenced ' +
152
- 'in a singleton class' do
153
- let(:source) do
154
- [
155
- '1.times do',
156
- ' foo = 1',
157
- ' puts foo',
158
- ' instance = Object.new',
159
- ' class << instance',
160
- ' foo = 2',
161
- ' bar = 3',
162
- ' puts bar',
163
- ' end',
164
- 'end'
165
- ]
166
- end
167
-
168
- it 'registers an offence' do
169
- inspect_source(cop, source)
170
- expect(cop.offences.size).to eq(1)
171
- expect(cop.offences.first.message)
172
- .to eq('Useless assignment to variable - foo')
173
- expect(cop.offences.first.line).to eq(6)
174
- expect(cop.highlights).to eq(['foo'])
175
- end
176
-
177
- include_examples 'mimics MRI 2.0'
178
- end
179
-
180
- context 'when a variable is assigned and unreferenced in a module' do
181
- let(:source) do
182
- [
183
- '1.times do',
184
- ' foo = 1',
185
- ' puts foo',
186
- ' module SomeModule',
187
- ' foo = 2',
188
- ' bar = 3',
189
- ' puts bar',
190
- ' end',
191
- 'end'
192
- ]
193
- end
194
-
195
- it 'registers an offence' do
196
- inspect_source(cop, source)
197
- expect(cop.offences.size).to eq(1)
198
- expect(cop.offences.first.message)
199
- .to eq('Useless assignment to variable - foo')
200
- expect(cop.offences.first.line).to eq(5)
201
- expect(cop.highlights).to eq(['foo'])
202
- end
203
-
204
- include_examples 'mimics MRI 2.0'
205
- end
206
-
207
- context 'when a variable is assigned and unreferenced in top level' do
208
- let(:source) do
209
- [
210
- 'foo = 1',
211
- 'bar = 2',
212
- 'puts bar'
213
- ]
214
- end
215
-
216
- it 'registers an offence' do
217
- inspect_source(cop, source)
218
- expect(cop.offences.size).to eq(1)
219
- expect(cop.offences.first.message)
220
- .to eq('Useless assignment to variable - foo')
221
- expect(cop.offences.first.line).to eq(1)
222
- expect(cop.highlights).to eq(['foo'])
223
- end
224
-
225
- include_examples 'mimics MRI 2.0'
226
- end
227
-
228
- context 'when a variable is assigned multiple times ' +
229
- 'but unreferenced' do
230
- let(:source) do
231
- [
232
- 'def some_method',
233
- ' foo = 1',
234
- ' bar = 2',
235
- ' foo = 3',
236
- ' puts bar',
237
- 'end'
238
- ]
239
- end
240
-
241
- it 'registers offences for each asignment' do
242
- inspect_source(cop, source)
243
- expect(cop.offences.size).to eq(2)
244
-
245
- expect(cop.offences[0].message)
246
- .to eq('Useless assignment to variable - foo')
247
- expect(cop.offences[0].line).to eq(2)
248
-
249
- expect(cop.offences[1].message)
250
- .to eq('Useless assignment to variable - foo')
251
- expect(cop.offences[1].line).to eq(4)
252
-
253
- expect(cop.highlights).to eq(%w(foo foo))
254
- end
255
- end
256
-
257
- context 'when a referenced variable is reassigned ' +
258
- 'but not re-referenced' do
259
- let(:source) do
260
- [
261
- 'def some_method',
262
- ' foo = 1',
263
- ' puts foo',
264
- ' foo = 3',
265
- 'end'
266
- ]
267
- end
268
-
269
- it 'registers an offence for the non-re-referenced assignment' do
270
- inspect_source(cop, source)
271
- expect(cop.offences.size).to eq(1)
272
- expect(cop.offences.first.message)
273
- .to eq('Useless assignment to variable - foo')
274
- expect(cop.offences.first.line).to eq(4)
275
- expect(cop.highlights).to eq(['foo'])
276
- end
277
- end
278
-
279
- context 'when an unreferenced variable is reassigned ' +
280
- 'and re-referenced' do
281
- let(:source) do
282
- [
283
- 'def some_method',
284
- ' foo = 1',
285
- ' foo = 3',
286
- ' puts foo',
287
- 'end'
288
- ]
289
- end
290
-
291
- it 'registers an offence for the unreferenced assignment' do
292
- inspect_source(cop, source)
293
- expect(cop.offences.size).to eq(1)
294
- expect(cop.offences.first.message)
295
- .to eq('Useless assignment to variable - foo')
296
- expect(cop.offences.first.line).to eq(2)
297
- expect(cop.highlights).to eq(['foo'])
298
- end
299
- end
300
-
301
- context 'when an unreferenced variable is reassigned in a block' do
302
- let(:source) do
303
- [
304
- 'def const_name(node)',
305
- ' const_names = []',
306
- ' const_node = node',
307
- '',
308
- ' loop do',
309
- ' namespace_node, name = *const_node',
310
- ' const_names << name',
311
- ' break unless namespace_node',
312
- ' break if namespace_node.type == :cbase',
313
- ' const_node = namespace_node',
314
- ' end',
315
- '',
316
- " const_names.reverse.join('::')",
317
- 'end'
318
- ]
319
- end
320
-
321
- include_examples 'accepts'
322
- include_examples 'mimics MRI 2.0'
323
- end
324
-
325
- context 'when a referenced variable is reassigned in a block' do
326
- let(:source) do
327
- [
328
- 'def some_method',
329
- ' foo = 1',
330
- ' puts foo',
331
- ' 1.times do',
332
- ' foo = 2',
333
- ' end',
334
- 'end'
335
- ]
336
- end
337
-
338
- include_examples 'accepts'
339
- include_examples 'mimics MRI 2.0'
340
- end
341
-
342
- context 'when a block local variable is declared but not assigned' do
343
- let(:source) do
344
- [
345
- '1.times do |i; foo|',
346
- 'end'
347
- ]
348
- end
349
-
350
- it 'registers an offence for the declaration' do
351
- inspect_source(cop, source)
352
- expect(cop.offences.size).to eq(1)
353
- expect(cop.offences.first.message)
354
- .to eq('Useless assignment to variable - foo')
355
- expect(cop.offences.first.line).to eq(1)
356
- expect(cop.highlights).to eq(['foo'])
357
- end
358
-
359
- include_examples 'mimics MRI 2.0'
360
- end
361
-
362
- context 'when a block local variable is assigned and unreferenced' do
363
- let(:source) do
364
- [
365
- '1.times do |i; foo|',
366
- ' foo = 2',
367
- 'end'
368
- ]
369
- end
370
-
371
- it 'registers offences for the assignment' do
372
- inspect_source(cop, source)
373
- expect(cop.offences.size).to eq(1)
374
- expect(cop.offences.first.message)
375
- .to eq('Useless assignment to variable - foo')
376
- expect(cop.offences.first.line).to eq(2)
377
- expect(cop.highlights).to eq(['foo'])
378
- end
379
- end
380
-
381
- context 'when a variable is assigned in loop body and unreferenced' do
382
- let(:source) do
383
- [
384
- 'def some_method',
385
- ' while true',
386
- ' foo = 1',
387
- ' end',
388
- 'end'
389
- ]
390
- end
391
-
392
- it 'registers an offence' do
393
- inspect_source(cop, source)
394
- expect(cop.offences.size).to eq(1)
395
- expect(cop.offences.first.message)
396
- .to eq('Useless assignment to variable - foo')
397
- expect(cop.offences.first.line).to eq(3)
398
- expect(cop.highlights).to eq(['foo'])
399
- end
400
-
401
- include_examples 'mimics MRI 2.0'
402
- end
403
-
404
- context 'when a variable is reassigned at the end of loop body ' +
405
- 'and would be referenced in next iteration' do
406
- let(:source) do
407
- [
408
- 'def some_method',
409
- ' total = 0',
410
- ' foo = 0',
411
- '',
412
- ' while total < 100',
413
- ' total += foo',
414
- ' foo += 1',
415
- ' end',
416
- '',
417
- ' total',
418
- 'end'
419
- ]
420
- end
421
-
422
- include_examples 'accepts'
423
- include_examples 'mimics MRI 2.0'
424
- end
425
-
426
- context 'when a variable is reassigned at the end of loop body ' +
427
- 'and would be referenced in loop condition' do
428
- let(:source) do
429
- [
430
- 'def some_method',
431
- ' total = 0',
432
- ' foo = 0',
433
- '',
434
- ' while foo < 100',
435
- ' total += 1',
436
- ' foo += 1',
437
- ' end',
438
- '',
439
- ' total',
440
- 'end'
441
- ]
442
- end
443
-
444
- include_examples 'accepts'
445
- include_examples 'mimics MRI 2.0'
446
- end
447
-
448
- context "when a variable is reassigned in loop body but won't " +
449
- 'be referenced either next iteration or loop condition' do
450
- let(:source) do
451
- [
452
- 'def some_method',
453
- ' total = 0',
454
- ' foo = 0',
455
- '',
456
- ' while total < 100',
457
- ' total += 1',
458
- ' foo += 1',
459
- ' end',
460
- '',
461
- ' total',
462
- 'end'
463
- ]
464
- end
465
-
466
- it 'registers an offence' do
467
- inspect_source(cop, source)
468
- expect(cop.offences.size).to eq(1)
469
- expect(cop.offences.first.message)
470
- .to eq('Useless assignment to variable - foo')
471
- expect(cop.offences.first.line).to eq(7)
472
- expect(cop.highlights).to eq(['foo'])
473
- end
474
- end
475
-
476
- context 'when a referenced variable is reassigned ' +
477
- 'but not re-referenced in a method defined in loop' do
478
- let(:source) do
479
- [
480
- 'while true',
481
- ' def some_method',
482
- ' foo = 1',
483
- ' puts foo',
484
- ' foo = 3',
485
- ' end',
486
- 'end'
487
- ]
488
- end
489
-
490
- it 'registers an offence' do
491
- inspect_source(cop, source)
492
- expect(cop.offences.size).to eq(1)
493
- expect(cop.offences.first.message)
494
- .to eq('Useless assignment to variable - foo')
495
- expect(cop.offences.first.line).to eq(5)
496
- expect(cop.highlights).to eq(['foo'])
497
- end
498
- end
499
-
500
- context 'when a variable that has same name as outer scope variable ' +
501
- 'is not referenced in a method defined in loop' do
502
- let(:source) do
503
- [
504
- 'foo = 1',
505
- '',
506
- 'while foo < 100',
507
- ' foo += 1',
508
- ' def some_method',
509
- ' foo = 1',
510
- ' end',
511
- 'end'
512
- ]
513
- end
514
-
515
- it 'registers an offence' do
516
- inspect_source(cop, source)
517
- expect(cop.offences.size).to eq(1)
518
- expect(cop.offences.first.message)
519
- .to eq('Useless assignment to variable - foo')
520
- expect(cop.offences.first.line).to eq(6)
521
- expect(cop.highlights).to eq(['foo'])
522
- end
523
- end
524
-
525
- context 'when a variable is assigned in single branch if ' +
526
- 'and unreferenced' do
527
- let(:source) do
528
- [
529
- 'def some_method(flag)',
530
- ' if flag',
531
- ' foo = 1',
532
- ' end',
533
- 'end'
534
- ]
535
- end
536
-
537
- it 'registers an offence' do
538
- inspect_source(cop, source)
539
- expect(cop.offences.size).to eq(1)
540
- expect(cop.offences.first.message)
541
- .to eq('Useless assignment to variable - foo')
542
- expect(cop.offences.first.line).to eq(3)
543
- expect(cop.highlights).to eq(['foo'])
544
- end
545
-
546
- include_examples 'mimics MRI 2.0'
547
- end
548
-
549
- context 'when a unreferenced variable is reassigned in same branch ' +
550
- 'and referenced after the branching' do
551
- let(:source) do
552
- [
553
- 'def some_method(flag)',
554
- ' if flag',
555
- ' foo = 1',
556
- ' foo = 2',
557
- ' end',
558
- '',
559
- ' foo',
560
- 'end'
561
- ]
562
- end
563
-
564
- it 'registers an offence for the unreferenced assignment' do
565
- inspect_source(cop, source)
566
- expect(cop.offences.size).to eq(1)
567
- expect(cop.offences.first.message)
568
- .to eq('Useless assignment to variable - foo')
569
- expect(cop.offences.first.line).to eq(3)
570
- expect(cop.highlights).to eq(['foo'])
571
- end
572
- end
573
-
574
- context 'when a variable is reassigned in single branch if ' +
575
- 'and referenced after the branching' do
576
- let(:source) do
577
- [
578
- 'def some_method(flag)',
579
- ' foo = 1',
580
- '',
581
- ' if flag',
582
- ' foo = 2',
583
- ' end',
584
- '',
585
- ' foo',
586
- 'end'
587
- ]
588
- end
589
-
590
- include_examples 'accepts'
591
- include_examples 'mimics MRI 2.0'
592
- end
593
-
594
- context 'when a variable is assigned in each branch of if ' +
595
- 'and referenced after the branching' do
596
- let(:source) do
597
- [
598
- 'def some_method(flag)',
599
- ' if flag',
600
- ' foo = 2',
601
- ' else',
602
- ' foo = 3',
603
- ' end',
604
- '',
605
- ' foo',
606
- 'end'
607
- ]
608
- end
609
-
610
- include_examples 'accepts'
611
- include_examples 'mimics MRI 2.0'
612
- end
613
-
614
- context 'when a variable is reassigned in single branch if ' +
615
- 'and referenced in the branch' do
616
- let(:source) do
617
- [
618
- 'def some_method(flag)',
619
- ' foo = 1',
620
- '',
621
- ' if flag',
622
- ' foo = 2',
623
- ' puts foo',
624
- ' end',
625
- 'end'
626
- ]
627
- end
628
-
629
- it 'registers an offence for the unreferenced assignment' do
630
- inspect_source(cop, source)
631
- expect(cop.offences.size).to eq(1)
632
- expect(cop.offences.first.message)
633
- .to eq('Useless assignment to variable - foo')
634
- expect(cop.offences.first.line).to eq(2)
635
- expect(cop.highlights).to eq(['foo'])
636
- end
637
- end
638
-
639
- context 'when a variable is assigned in each branch of if ' +
640
- 'and referenced in the else branch' do
641
- let(:source) do
642
- [
643
- 'def some_method(flag)',
644
- ' if flag',
645
- ' foo = 2',
646
- ' else',
647
- ' foo = 3',
648
- ' puts foo',
649
- ' end',
650
- 'end'
651
- ]
652
- end
653
-
654
- it 'registers an offence for the assignment in the if branch' do
655
- inspect_source(cop, source)
656
- expect(cop.offences.size).to eq(1)
657
- expect(cop.offences.first.message)
658
- .to eq('Useless assignment to variable - foo')
659
- expect(cop.offences.first.line).to eq(3)
660
- expect(cop.highlights).to eq(['foo'])
661
- end
662
- end
663
-
664
- context 'when a variable is assigned in branch of modifier if ' +
665
- 'that references the variable in its conditional clause' +
666
- 'and referenced after the branching' do
667
- let(:source) do
668
- [
669
- 'def some_method(flag)',
670
- ' foo = 1 unless foo',
671
- ' puts foo',
672
- 'end'
673
- ]
674
- end
675
-
676
- include_examples 'accepts'
677
- include_examples 'mimics MRI 2.0'
678
- end
679
-
680
- context 'when a variable is assigned in branch of modifier if ' +
681
- 'that references the variable in its conditional clause' +
682
- 'and unreferenced' do
683
- let(:source) do
684
- [
685
- 'def some_method(flag)',
686
- ' foo = 1 unless foo',
687
- 'end'
688
- ]
689
- end
690
-
691
- it 'registers an offence' do
692
- inspect_source(cop, source)
693
- expect(cop.offences.size).to eq(1)
694
- expect(cop.offences.first.message)
695
- .to eq('Useless assignment to variable - foo')
696
- expect(cop.offences.first.line).to eq(2)
697
- expect(cop.highlights).to eq(['foo'])
698
- end
699
- end
700
-
701
- context 'when a variable is assigned on each side of && ' +
702
- 'and referenced after the &&' do
703
- let(:source) do
704
- [
705
- 'def some_method',
706
- ' (foo = do_something_returns_object_or_nil) && (foo = 1)',
707
- ' foo',
708
- 'end'
709
- ]
710
- end
711
-
712
- include_examples 'accepts'
713
- include_examples 'mimics MRI 2.0'
714
- end
715
-
716
- context 'when a unreferenced variable is reassigned ' +
717
- 'on the left side of && and referenced after the &&' do
718
- let(:source) do
719
- [
720
- 'def some_method',
721
- ' foo = 1',
722
- ' (foo = do_something_returns_object_or_nil) && do_something',
723
- ' foo',
724
- 'end'
725
- ]
726
- end
727
-
728
- it 'registers an offence for the unreferenced assignment' do
729
- inspect_source(cop, source)
730
- expect(cop.offences.size).to eq(1)
731
- expect(cop.offences.first.message)
732
- .to eq('Useless assignment to variable - foo')
733
- expect(cop.offences.first.line).to eq(2)
734
- expect(cop.highlights).to eq(['foo'])
735
- end
736
- end
737
-
738
- context 'when a unreferenced variable is reassigned ' +
739
- 'on the right side of && and referenced after the &&' do
740
- let(:source) do
741
- [
742
- 'def some_method',
743
- ' foo = 1',
744
- ' do_something_returns_object_or_nil && foo = 2',
745
- ' foo',
746
- 'end'
747
- ]
748
- end
749
-
750
- include_examples 'accepts'
751
- include_examples 'mimics MRI 2.0'
752
- end
753
-
754
- context 'when a variable is reassigned ' +
755
- 'while referencing itself in rhs and referenced' do
756
- let(:source) do
757
- [
758
- 'def some_method',
759
- ' foo = [1, 2]',
760
- ' foo = foo.map { |i| i + 1 }',
761
- ' puts foo',
762
- 'end'
763
- ]
764
- end
765
-
766
- include_examples 'accepts'
767
- include_examples 'mimics MRI 2.0'
768
- end
769
-
770
- context 'when a variable is reassigned ' +
771
- 'with binary operator assignment and referenced' do
772
- let(:source) do
773
- [
774
- 'def some_method',
775
- ' foo = 1',
776
- ' foo += 1',
777
- ' foo',
778
- 'end'
779
- ]
780
- end
781
-
782
- include_examples 'accepts'
783
- include_examples 'mimics MRI 2.0'
784
- end
785
-
786
- context 'when a variable is reassigned ' +
787
- 'with logical operator assignment and referenced' do
788
- let(:source) do
789
- [
790
- 'def some_method',
791
- ' foo = do_something_returns_object_or_nil',
792
- ' foo ||= 1',
793
- ' foo',
794
- 'end'
795
- ]
796
- end
797
-
798
- include_examples 'accepts'
799
- include_examples 'mimics MRI 2.0'
800
- end
801
-
802
- context 'when a variable is reassigned with binary operator ' +
803
- 'assignment while assigning to itself in rhs ' +
804
- 'then referenced' do
805
- let(:source) do
806
- [
807
- 'def some_method',
808
- ' foo = 1',
809
- ' foo += foo = 2',
810
- ' foo',
811
- 'end'
812
- ]
813
- end
814
-
815
- it 'registers an offence for the assignment in rhs' do
816
- inspect_source(cop, source)
817
- expect(cop.offences.size).to eq(1)
818
- expect(cop.offences.first.message)
819
- .to eq('Useless assignment to variable - foo')
820
- expect(cop.offences.first.line).to eq(3)
821
- expect(cop.highlights).to eq(['foo'])
822
- end
823
- end
824
-
825
- context 'when a variable is assigned first with ||= and referenced' do
826
- let(:source) do
827
- [
828
- 'def some_method',
829
- ' foo ||= 1',
830
- ' foo',
831
- 'end'
832
- ]
833
- end
834
-
835
- include_examples 'accepts'
836
- include_examples 'mimics MRI 2.0'
837
- end
838
-
839
- context 'when a variable is assigned with ||= ' +
840
- 'at the last expression of the scope' do
841
- let(:source) do
842
- [
843
- 'def some_method',
844
- ' foo = do_something_returns_object_or_nil',
845
- ' foo ||= 1',
846
- 'end'
847
- ]
848
- end
849
-
850
- it 'registers an offence' do
851
- inspect_source(cop, source)
852
- expect(cop.offences.size).to eq(1)
853
- expect(cop.offences.first.message).to eq(
854
- 'Useless assignment to variable - foo. Use just operator ||.'
855
- )
856
- expect(cop.offences.first.line).to eq(3)
857
- expect(cop.highlights).to eq(['foo'])
858
- end
859
- end
860
-
861
- context 'when a variable is assigned with ||= ' +
862
- 'before the last expression of the scope' do
863
- let(:source) do
864
- [
865
- 'def some_method',
866
- ' foo = do_something_returns_object_or_nil',
867
- ' foo ||= 1',
868
- ' some_return_value',
869
- 'end'
870
- ]
871
- end
872
-
873
- it 'registers an offence' do
874
- inspect_source(cop, source)
875
- expect(cop.offences.size).to eq(1)
876
- expect(cop.offences.first.message)
877
- .to eq('Useless assignment to variable - foo')
878
- expect(cop.offences.first.line).to eq(3)
879
- expect(cop.highlights).to eq(['foo'])
880
- end
881
- end
882
-
883
- context 'when a variable is assigned with multiple assignment ' +
884
- 'and unreferenced' do
885
- let(:source) do
886
- [
887
- 'def some_method',
888
- ' foo, bar = do_something',
889
- ' puts foo',
890
- 'end'
891
- ]
892
- end
893
-
894
- it 'registers an offence' do
895
- inspect_source(cop, source)
896
- expect(cop.offences.size).to eq(1)
897
- expect(cop.offences.first.message).to eq(
898
- 'Useless assignment to variable - bar. ' +
899
- 'Use _ or _bar as a variable name ' +
900
- "to indicate that it won't be used."
901
- )
902
- expect(cop.offences.first.line).to eq(2)
903
- expect(cop.highlights).to eq(['bar'])
904
- end
905
-
906
- include_examples 'mimics MRI 2.0'
907
- end
908
-
909
- context 'when a variable is reassigned with multiple assignment ' +
910
- 'while referencing itself in rhs and referenced' do
911
- let(:source) do
912
- [
913
- 'def some_method',
914
- ' foo = 1',
915
- ' foo, bar = do_something(foo)',
916
- ' puts foo, bar',
917
- 'end'
918
- ]
919
- end
920
-
921
- include_examples 'accepts'
922
- include_examples 'mimics MRI 2.0'
923
- end
924
-
925
- context 'when a variable is assigned in loop body ' +
926
- 'and referenced in post while condition' do
927
- let(:source) do
928
- [
929
- 'begin',
930
- ' a = (a || 0) + 1',
931
- ' puts a',
932
- 'end while a <= 2'
933
- ]
934
- end
935
-
936
- include_examples 'accepts'
937
- include_examples 'mimics MRI 2.0'
938
- end
939
-
940
- context 'when a variable is assigned in loop body ' +
941
- 'and referenced in post until condition' do
942
- let(:source) do
943
- [
944
- 'begin',
945
- ' a = (a || 0) + 1',
946
- ' puts a',
947
- 'end until a > 2'
948
- ]
949
- end
950
-
951
- include_examples 'accepts'
952
- include_examples 'mimics MRI 2.0'
953
- end
954
-
955
- context 'when a variable is assigned ' +
956
- 'in main body of begin with rescue but unreferenced' do
957
- let(:source) do
958
- [
959
- 'begin',
960
- ' do_something',
961
- ' foo = true',
962
- 'rescue',
963
- ' do_anything',
964
- 'end'
965
- ]
966
- end
967
-
968
- it 'registers an offence' do
969
- inspect_source(cop, source)
970
- expect(cop.offences.size).to eq(1)
971
- expect(cop.offences.first.message)
972
- .to eq('Useless assignment to variable - foo')
973
- expect(cop.offences.first.line).to eq(3)
974
- expect(cop.highlights).to eq(['foo'])
975
- end
976
-
977
- include_examples 'mimics MRI 2.0'
978
- end
979
-
980
- context 'when a variable is assigned in main body of begin, rescue ' +
981
- 'and else then referenced after the begin' do
982
- let(:source) do
983
- [
984
- 'begin',
985
- ' do_something',
986
- ' foo = :in_begin',
987
- 'rescue FirstError',
988
- ' foo = :in_first_rescue',
989
- 'rescue SecondError',
990
- ' foo = :in_second_rescue',
991
- 'else',
992
- ' foo = :in_else',
993
- 'end',
994
- '',
995
- 'puts foo'
996
- ]
997
- end
998
-
999
- include_examples 'accepts'
1000
- include_examples 'mimics MRI 2.0'
1001
- end
1002
-
1003
- context 'when a variable is reassigned multiple times ' +
1004
- 'in main body of begin then referenced after the begin' do
1005
- let(:source) do
1006
- [
1007
- 'begin',
1008
- ' status = :initial',
1009
- ' connect_sometimes_fails!',
1010
- ' status = :connected',
1011
- ' fetch_sometimes_fails!',
1012
- ' status = :fetched',
1013
- 'rescue',
1014
- ' do_something',
1015
- 'end',
1016
- '',
1017
- 'puts status'
1018
- ]
1019
- end
1020
-
1021
- include_examples 'accepts'
1022
- include_examples 'mimics MRI 2.0'
1023
- end
1024
-
1025
- context 'when a variable is reassigned multiple times ' +
1026
- 'in main body of begin then referenced in rescue' do
1027
- let(:source) do
1028
- [
1029
- 'begin',
1030
- ' status = :initial',
1031
- ' connect_sometimes_fails!',
1032
- ' status = :connected',
1033
- ' fetch_sometimes_fails!',
1034
- ' status = :fetched',
1035
- 'rescue',
1036
- ' puts status',
1037
- 'end'
1038
- ]
1039
- end
1040
-
1041
- include_examples 'accepts'
1042
- include_examples 'mimics MRI 2.0'
1043
- end
1044
-
1045
- context 'when a variable is reassigned multiple times ' +
1046
- 'in main body of begin then referenced in ensure' do
1047
- let(:source) do
1048
- [
1049
- 'begin',
1050
- ' status = :initial',
1051
- ' connect_sometimes_fails!',
1052
- ' status = :connected',
1053
- ' fetch_sometimes_fails!',
1054
- ' status = :fetched',
1055
- 'ensure',
1056
- ' puts status',
1057
- 'end'
1058
- ]
1059
- end
1060
-
1061
- include_examples 'accepts'
1062
- include_examples 'mimics MRI 2.0'
1063
- end
1064
-
1065
- context 'when a variable is reassigned multiple times in rescue ' +
1066
- 'and referenced after the begin' do
1067
- let(:source) do
1068
- [
1069
- 'foo = false',
1070
- '',
1071
- 'begin',
1072
- ' do_something',
1073
- 'rescue',
1074
- ' foo = true',
1075
- ' foo = true',
1076
- 'end',
1077
- '',
1078
- 'puts foo'
1079
- ]
1080
- end
1081
-
1082
- it 'registers an offence' do
1083
- inspect_source(cop, source)
1084
- expect(cop.offences.size).to eq(1)
1085
- expect(cop.offences.first.message)
1086
- .to eq('Useless assignment to variable - foo')
1087
- expect(cop.offences.first.line).to eq(6)
1088
- expect(cop.highlights).to eq(['foo'])
1089
- end
1090
- end
1091
-
1092
- context 'when a variable is reassigned multiple times ' +
1093
- 'in rescue with ensure then referenced after the begin' do
1094
- let(:source) do
1095
- [
1096
- 'foo = false',
1097
- '',
1098
- 'begin',
1099
- ' do_something',
1100
- 'rescue',
1101
- ' foo = true',
1102
- ' foo = true',
1103
- 'ensure',
1104
- ' do_anything',
1105
- 'end',
1106
- '',
1107
- 'puts foo'
1108
- ]
1109
- end
1110
-
1111
- it 'registers an offence' do
1112
- inspect_source(cop, source)
1113
- expect(cop.offences.size).to eq(1)
1114
- expect(cop.offences.first.message)
1115
- .to eq('Useless assignment to variable - foo')
1116
- expect(cop.offences.first.line).to eq(6)
1117
- expect(cop.highlights).to eq(['foo'])
1118
- end
1119
- end
1120
-
1121
- context 'when a variable is reassigned multiple times ' +
1122
- 'in ensure with rescue then referenced after the begin' do
1123
- let(:source) do
1124
- [
1125
- 'begin',
1126
- ' do_something',
1127
- 'rescue',
1128
- ' do_anything',
1129
- 'ensure',
1130
- ' foo = true',
1131
- ' foo = true',
1132
- 'end',
1133
- '',
1134
- 'puts foo'
1135
- ]
1136
- end
1137
-
1138
- it 'registers an offence' do
1139
- inspect_source(cop, source)
1140
- expect(cop.offences.size).to eq(1)
1141
- expect(cop.offences.first.message)
1142
- .to eq('Useless assignment to variable - foo')
1143
- expect(cop.offences.first.line).to eq(6)
1144
- expect(cop.highlights).to eq(['foo'])
1145
- end
1146
- end
1147
-
1148
- context 'when a variable is assigned at the end of rescue ' +
1149
- 'and would be referenced with retry' do
1150
- let(:source) do
1151
- [
1152
- 'retried = false',
1153
- '',
1154
- 'begin',
1155
- ' do_something',
1156
- 'rescue',
1157
- ' fail if retried',
1158
- ' retried = true',
1159
- ' retry',
1160
- 'end'
1161
- ]
1162
- end
1163
-
1164
- include_examples 'accepts'
1165
- include_examples 'mimics MRI 2.0'
1166
- end
1167
-
1168
- context 'when a variable is assigned ' +
1169
- 'in main body of begin, rescue and else ' +
1170
- 'and reassigned in ensure then referenced after the begin' do
1171
- let(:source) do
1172
- [
1173
- 'begin',
1174
- ' do_something',
1175
- ' foo = :in_begin',
1176
- 'rescue FirstError',
1177
- ' foo = :in_first_rescue',
1178
- 'rescue SecondError',
1179
- ' foo = :in_second_rescue',
1180
- 'else',
1181
- ' foo = :in_else',
1182
- 'ensure',
1183
- ' foo = :in_ensure',
1184
- 'end',
1185
- '',
1186
- 'puts foo'
1187
- ]
1188
- end
1189
-
1190
- it 'registers offences for each assignment before ensure' do
1191
- inspect_source(cop, source)
1192
- expect(cop.offences.size).to eq(4)
1193
-
1194
- expect(cop.offences[0].line).to eq(3)
1195
- expect(cop.offences[1].line).to eq(5)
1196
- expect(cop.offences[2].line).to eq(7)
1197
- expect(cop.offences[3].line).to eq(9)
1198
- end
1199
- end
1200
-
1201
- context 'when a method argument is reassigned ' +
1202
- 'and zero arity super is called' do
1203
- let(:source) do
1204
- [
1205
- 'def some_method(foo)',
1206
- ' foo = 1',
1207
- ' super',
1208
- 'end'
1209
- ]
1210
- end
1211
-
1212
- include_examples 'accepts'
1213
- include_examples 'mimics MRI 2.0'
1214
- end
1215
-
1216
- context 'when a local variable is unreferenced ' +
1217
- 'and zero arity super is called' do
1218
- let(:source) do
1219
- [
1220
- 'def some_method(bar)',
1221
- ' foo = 1',
1222
- ' super',
1223
- 'end'
1224
- ]
1225
- end
1226
-
1227
- it 'registers an offence' do
1228
- inspect_source(cop, source)
1229
- expect(cop.offences.size).to eq(1)
1230
- expect(cop.offences.first.message)
1231
- .to eq('Useless assignment to variable - foo')
1232
- expect(cop.offences.first.line).to eq(2)
1233
- expect(cop.highlights).to eq(['foo'])
1234
- end
1235
-
1236
- include_examples 'mimics MRI 2.0'
1237
- end
1238
-
1239
- context 'when a method argument is reassigned ' +
1240
- 'but not passed to super' do
1241
- let(:source) do
1242
- [
1243
- 'def some_method(foo, bar)',
1244
- ' foo = 1',
1245
- ' super(bar)',
1246
- 'end'
1247
- ]
1248
- end
1249
-
1250
- it 'registers an offence' do
1251
- inspect_source(cop, source)
1252
- expect(cop.offences.size).to eq(1)
1253
- expect(cop.offences.first.message)
1254
- .to eq('Useless assignment to variable - foo')
1255
- expect(cop.offences.first.line).to eq(2)
1256
- expect(cop.highlights).to eq(['foo'])
1257
- end
1258
- end
1259
-
1260
- context 'when a named capture is unreferenced in top level' do
1261
- let(:source) do
1262
- [
1263
- "/(?<foo>\w+)/ =~ 'FOO'",
1264
- ]
1265
- end
1266
-
1267
- it 'registers an offence' do
1268
- inspect_source(cop, source)
1269
- expect(cop.offences.size).to eq(1)
1270
- expect(cop.offences.first.message)
1271
- .to eq('Useless assignment to variable - foo')
1272
- expect(cop.offences.first.line).to eq(1)
1273
- end
1274
-
1275
- include_examples 'mimics MRI 2.0'
1276
- end
1277
-
1278
- context 'when a named capture is unreferenced ' +
1279
- 'in other than top level' do
1280
- let(:source) do
1281
- [
1282
- 'def some_method',
1283
- " /(?<foo>\w+)/ =~ 'FOO'",
1284
- 'end'
1285
- ]
1286
- end
1287
-
1288
- it 'registers an offence' do
1289
- inspect_source(cop, source)
1290
- expect(cop.offences.size).to eq(1)
1291
- expect(cop.offences.first.message)
1292
- .to eq('Useless assignment to variable - foo')
1293
- expect(cop.offences.first.line).to eq(2)
1294
- expect(cop.highlights).to eq(["/(?<foo>\w+)/"])
1295
- end
1296
-
1297
- # MRI 2.0 accepts this case, but I have no idea why it does so
1298
- # and there's no convincing reason to conform to this behavior,
1299
- # so RuboCop does not mimic MRI in this case.
1300
- end
1301
-
1302
- context 'when a named capture is referenced' do
1303
- let(:source) do
1304
- [
1305
- 'def some_method',
1306
- " /(?<foo>\w+)(?<bar>\s+)/ =~ 'FOO'",
1307
- ' puts foo',
1308
- ' puts bar',
1309
- 'end'
1310
- ]
1311
- end
1312
-
1313
- include_examples 'accepts'
1314
- include_examples 'mimics MRI 2.0'
1315
- end
1316
-
1317
- context 'when a variable is referenced ' +
1318
- 'in rhs of named capture expression' do
1319
- let(:source) do
1320
- [
1321
- 'def some_method',
1322
- " foo = 'some string'",
1323
- " /(?<foo>\w+)/ =~ foo",
1324
- ' puts foo',
1325
- 'end'
1326
- ]
1327
- end
1328
-
1329
- include_examples 'accepts'
1330
- end
1331
-
1332
- context 'when a variable is assigned in begin ' +
1333
- 'and referenced outside' do
1334
- let(:source) do
1335
- [
1336
- 'def some_method',
1337
- ' begin',
1338
- ' foo = 1',
1339
- ' end',
1340
- ' puts foo',
1341
- 'end'
1342
- ]
1343
- end
1344
-
1345
- include_examples 'accepts'
1346
- include_examples 'mimics MRI 2.0'
1347
- end
1348
-
1349
- context 'when a variable is shadowed by a block argument ' +
1350
- 'and unreferenced' do
1351
- let(:source) do
1352
- [
1353
- 'def some_method',
1354
- ' foo = 1',
1355
- ' 1.times do |foo|',
1356
- ' puts foo',
1357
- ' end',
1358
- 'end'
1359
- ]
1360
- end
1361
-
1362
- it 'registers an offence' do
1363
- inspect_source(cop, source)
1364
- expect(cop.offences.size).to eq(1)
1365
- expect(cop.offences.first.message)
1366
- .to eq('Useless assignment to variable - foo')
1367
- expect(cop.offences.first.line).to eq(2)
1368
- expect(cop.highlights).to eq(['foo'])
1369
- end
1370
-
1371
- include_examples 'mimics MRI 2.0', 'unused variable'
1372
- end
1373
-
1374
- context 'when a variable is not used and the name starts with _' do
1375
- let(:source) do
1376
- [
1377
- 'def some_method',
1378
- ' _foo = 1',
1379
- ' bar = 2',
1380
- ' puts bar',
1381
- 'end'
1382
- ]
1383
- end
1384
-
1385
- include_examples 'accepts'
1386
- include_examples 'mimics MRI 2.0'
1387
- end
1388
-
1389
- context 'when a method argument is not used' do
1390
- let(:source) do
1391
- [
1392
- 'def some_method(arg)',
1393
- 'end'
1394
- ]
1395
- end
1396
-
1397
- include_examples 'accepts'
1398
- include_examples 'mimics MRI 2.0'
1399
- end
1400
-
1401
- context 'when an optional method argument is not used' do
1402
- let(:source) do
1403
- [
1404
- 'def some_method(arg = nil)',
1405
- 'end'
1406
- ]
1407
- end
1408
-
1409
- include_examples 'accepts'
1410
- include_examples 'mimics MRI 2.0'
1411
- end
1412
-
1413
- context 'when a block method argument is not used' do
1414
- let(:source) do
1415
- [
1416
- 'def some_method(&block)',
1417
- 'end'
1418
- ]
1419
- end
1420
-
1421
- include_examples 'accepts'
1422
- include_examples 'mimics MRI 2.0'
1423
- end
1424
-
1425
- context 'when a splat method argument is not used' do
1426
- let(:source) do
1427
- [
1428
- 'def some_method(*args)',
1429
- 'end'
1430
- ]
1431
- end
1432
-
1433
- include_examples 'accepts'
1434
- include_examples 'mimics MRI 2.0'
1435
- end
1436
-
1437
- context 'when a optional keyword method argument is not used' do
1438
- let(:source) do
1439
- [
1440
- 'def some_method(name: value)',
1441
- 'end'
1442
- ]
1443
- end
1444
-
1445
- include_examples 'accepts'
1446
- include_examples 'mimics MRI 2.0'
1447
- end
1448
-
1449
- context 'when a keyword splat method argument is used' do
1450
- let(:source) do
1451
- [
1452
- 'def some_method(name: value, **rest_keywords)',
1453
- ' p rest_keywords',
1454
- 'end'
1455
- ]
1456
- end
1457
-
1458
- include_examples 'accepts'
1459
- include_examples 'mimics MRI 2.0'
1460
- end
1461
-
1462
- context 'when a keyword splat method argument is not used' do
1463
- let(:source) do
1464
- [
1465
- 'def some_method(name: value, **rest_keywords)',
1466
- 'end'
1467
- ]
1468
- end
1469
-
1470
- include_examples 'accepts'
1471
- include_examples 'mimics MRI 2.0'
1472
- end
1473
-
1474
- context 'when a block argument is not used' do
1475
- let(:source) do
1476
- [
1477
- '1.times do |i|',
1478
- 'end'
1479
- ]
1480
- end
1481
-
1482
- include_examples 'accepts'
1483
- include_examples 'mimics MRI 2.0'
1484
- end
1485
-
1486
- context 'when there is only one AST node and it is unused variable' do
1487
- let(:source) do
1488
- [
1489
- 'foo = 1'
1490
- ]
1491
- end
1492
-
1493
- it 'registers an offence' do
1494
- inspect_source(cop, source)
1495
- expect(cop.offences.size).to eq(1)
1496
- expect(cop.offences.first.message)
1497
- .to eq('Useless assignment to variable - foo')
1498
- expect(cop.offences.first.line).to eq(1)
1499
- expect(cop.highlights).to eq(['foo'])
1500
- end
1501
-
1502
- include_examples 'mimics MRI 2.0'
1503
- end
1504
-
1505
- context 'when a variable is assigned ' +
1506
- 'while being passed to a method taking block' do
1507
-
1508
- context 'and the variable is used' do
1509
- let(:source) do
1510
- [
1511
- 'some_method(foo = 1) do',
1512
- 'end',
1513
- 'puts foo'
1514
- ]
1515
- end
1516
-
1517
- include_examples 'accepts'
1518
- include_examples 'mimics MRI 2.0'
1519
- end
1520
-
1521
- context 'and the variable is not used' do
1522
- let(:source) do
1523
- [
1524
- 'some_method(foo = 1) do',
1525
- 'end'
1526
- ]
1527
- end
1528
-
1529
- it 'registers an offence' do
1530
- inspect_source(cop, source)
1531
- expect(cop.offences.size).to eq(1)
1532
- expect(cop.offences.first.message)
1533
- .to eq('Useless assignment to variable - foo')
1534
- expect(cop.offences.first.line).to eq(1)
1535
- expect(cop.highlights).to eq(['foo'])
1536
- end
1537
-
1538
- include_examples 'mimics MRI 2.0'
1539
- end
1540
- end
1541
-
1542
- context 'when a variabled is assigned ' +
1543
- 'and passed to a method followed by method taking block' do
1544
- let(:source) do
1545
- [
1546
- "pattern = '*.rb'",
1547
- 'Dir.glob(pattern).map do |path|',
1548
- 'end'
1549
- ]
1550
- end
1551
-
1552
- include_examples 'accepts'
1553
- include_examples 'mimics MRI 2.0'
1554
- end
5
+ describe Rubocop::Cop::Lint::UselessAssignment do
6
+ subject(:cop) { described_class.new }
7
+
8
+ context 'when a variable is assigned and unreferenced in a method' do
9
+ let(:source) do
10
+ [
11
+ 'class SomeClass',
12
+ ' foo = 1',
13
+ ' puts foo',
14
+ ' def some_method',
15
+ ' foo = 2',
16
+ ' bar = 3',
17
+ ' puts bar',
18
+ ' end',
19
+ 'end'
20
+ ]
21
+ end
22
+
23
+ it 'registers an offence' do
24
+ inspect_source(cop, source)
25
+ expect(cop.offences.size).to eq(1)
26
+ expect(cop.offences.first.message)
27
+ .to eq('Useless assignment to variable - foo')
28
+ expect(cop.offences.first.line).to eq(5)
29
+ expect(cop.highlights).to eq(['foo'])
30
+ end
31
+
32
+ include_examples 'mimics MRI 2.0'
33
+ end
34
+
35
+ context 'when a variable is assigned and unreferenced ' +
36
+ 'in a singleton method defined with self keyword' do
37
+ let(:source) do
38
+ [
39
+ 'class SomeClass',
40
+ ' foo = 1',
41
+ ' puts foo',
42
+ ' def self.some_method',
43
+ ' foo = 2',
44
+ ' bar = 3',
45
+ ' puts bar',
46
+ ' end',
47
+ 'end'
48
+ ]
49
+ end
50
+
51
+ it 'registers an offence' do
52
+ inspect_source(cop, source)
53
+ expect(cop.offences.size).to eq(1)
54
+ expect(cop.offences.first.message)
55
+ .to eq('Useless assignment to variable - foo')
56
+ expect(cop.offences.first.line).to eq(5)
57
+ expect(cop.highlights).to eq(['foo'])
58
+ end
59
+
60
+ include_examples 'mimics MRI 2.0'
61
+ end
62
+
63
+ context 'when a variable is assigned and unreferenced ' +
64
+ 'in a singleton method defined with variable name' do
65
+ let(:source) do
66
+ [
67
+ '1.times do',
68
+ ' foo = 1',
69
+ ' puts foo',
70
+ ' instance = Object.new',
71
+ ' def instance.some_method',
72
+ ' foo = 2',
73
+ ' bar = 3',
74
+ ' puts bar',
75
+ ' end',
76
+ 'end'
77
+ ]
78
+ end
79
+
80
+ it 'registers an offence' do
81
+ inspect_source(cop, source)
82
+ expect(cop.offences.size).to eq(1)
83
+ expect(cop.offences.first.message)
84
+ .to eq('Useless assignment to variable - foo')
85
+ expect(cop.offences.first.line).to eq(6)
86
+ expect(cop.highlights).to eq(['foo'])
87
+ end
88
+
89
+ include_examples 'mimics MRI 2.0'
90
+ end
91
+
92
+ context 'when a variable is assigned and unreferenced in a class' do
93
+ let(:source) do
94
+ [
95
+ '1.times do',
96
+ ' foo = 1',
97
+ ' puts foo',
98
+ ' class SomeClass',
99
+ ' foo = 2',
100
+ ' bar = 3',
101
+ ' puts bar',
102
+ ' end',
103
+ 'end'
104
+ ]
105
+ end
106
+
107
+ it 'registers an offence' do
108
+ inspect_source(cop, source)
109
+ expect(cop.offences.size).to eq(1)
110
+ expect(cop.offences.first.message)
111
+ .to eq('Useless assignment to variable - foo')
112
+ expect(cop.offences.first.line).to eq(5)
113
+ expect(cop.highlights).to eq(['foo'])
114
+ end
115
+
116
+ include_examples 'mimics MRI 2.0'
117
+ end
118
+
119
+ context 'when a variable is assigned and unreferenced in a class ' +
120
+ 'subclassing another class stored in local variable' do
121
+ let(:source) do
122
+ [
123
+ '1.times do',
124
+ ' foo = 1',
125
+ ' puts foo',
126
+ ' array_class = Array',
127
+ ' class SomeClass < array_class',
128
+ ' foo = 2',
129
+ ' bar = 3',
130
+ ' puts bar',
131
+ ' end',
132
+ 'end'
133
+ ]
134
+ end
135
+
136
+ it 'registers an offence' do
137
+ inspect_source(cop, source)
138
+ expect(cop.offences.size).to eq(1)
139
+ expect(cop.offences.first.message)
140
+ .to eq('Useless assignment to variable - foo')
141
+ expect(cop.offences.first.line).to eq(6)
142
+ expect(cop.highlights).to eq(['foo'])
143
+ end
144
+
145
+ include_examples 'mimics MRI 2.0'
146
+ end
147
+
148
+ context 'when a variable is assigned and unreferenced ' +
149
+ 'in a singleton class' do
150
+ let(:source) do
151
+ [
152
+ '1.times do',
153
+ ' foo = 1',
154
+ ' puts foo',
155
+ ' instance = Object.new',
156
+ ' class << instance',
157
+ ' foo = 2',
158
+ ' bar = 3',
159
+ ' puts bar',
160
+ ' end',
161
+ 'end'
162
+ ]
163
+ end
164
+
165
+ it 'registers an offence' do
166
+ inspect_source(cop, source)
167
+ expect(cop.offences.size).to eq(1)
168
+ expect(cop.offences.first.message)
169
+ .to eq('Useless assignment to variable - foo')
170
+ expect(cop.offences.first.line).to eq(6)
171
+ expect(cop.highlights).to eq(['foo'])
172
+ end
173
+
174
+ include_examples 'mimics MRI 2.0'
175
+ end
176
+
177
+ context 'when a variable is assigned and unreferenced in a module' do
178
+ let(:source) do
179
+ [
180
+ '1.times do',
181
+ ' foo = 1',
182
+ ' puts foo',
183
+ ' module SomeModule',
184
+ ' foo = 2',
185
+ ' bar = 3',
186
+ ' puts bar',
187
+ ' end',
188
+ 'end'
189
+ ]
190
+ end
191
+
192
+ it 'registers an offence' do
193
+ inspect_source(cop, source)
194
+ expect(cop.offences.size).to eq(1)
195
+ expect(cop.offences.first.message)
196
+ .to eq('Useless assignment to variable - foo')
197
+ expect(cop.offences.first.line).to eq(5)
198
+ expect(cop.highlights).to eq(['foo'])
199
+ end
200
+
201
+ include_examples 'mimics MRI 2.0'
202
+ end
203
+
204
+ context 'when a variable is assigned and unreferenced in top level' do
205
+ let(:source) do
206
+ [
207
+ 'foo = 1',
208
+ 'bar = 2',
209
+ 'puts bar'
210
+ ]
211
+ end
212
+
213
+ it 'registers an offence' do
214
+ inspect_source(cop, source)
215
+ expect(cop.offences.size).to eq(1)
216
+ expect(cop.offences.first.message)
217
+ .to eq('Useless assignment to variable - foo')
218
+ expect(cop.offences.first.line).to eq(1)
219
+ expect(cop.highlights).to eq(['foo'])
220
+ end
221
+
222
+ include_examples 'mimics MRI 2.0'
223
+ end
224
+
225
+ context 'when a variable is assigned multiple times ' +
226
+ 'but unreferenced' do
227
+ let(:source) do
228
+ [
229
+ 'def some_method',
230
+ ' foo = 1',
231
+ ' bar = 2',
232
+ ' foo = 3',
233
+ ' puts bar',
234
+ 'end'
235
+ ]
236
+ end
237
+
238
+ it 'registers offences for each asignment' do
239
+ inspect_source(cop, source)
240
+ expect(cop.offences.size).to eq(2)
241
+
242
+ expect(cop.offences[0].message)
243
+ .to eq('Useless assignment to variable - foo')
244
+ expect(cop.offences[0].line).to eq(2)
245
+
246
+ expect(cop.offences[1].message)
247
+ .to eq('Useless assignment to variable - foo')
248
+ expect(cop.offences[1].line).to eq(4)
249
+
250
+ expect(cop.highlights).to eq(%w(foo foo))
251
+ end
252
+ end
253
+
254
+ context 'when a referenced variable is reassigned ' +
255
+ 'but not re-referenced' do
256
+ let(:source) do
257
+ [
258
+ 'def some_method',
259
+ ' foo = 1',
260
+ ' puts foo',
261
+ ' foo = 3',
262
+ 'end'
263
+ ]
264
+ end
265
+
266
+ it 'registers an offence for the non-re-referenced assignment' do
267
+ inspect_source(cop, source)
268
+ expect(cop.offences.size).to eq(1)
269
+ expect(cop.offences.first.message)
270
+ .to eq('Useless assignment to variable - foo')
271
+ expect(cop.offences.first.line).to eq(4)
272
+ expect(cop.highlights).to eq(['foo'])
273
+ end
274
+ end
275
+
276
+ context 'when an unreferenced variable is reassigned ' +
277
+ 'and re-referenced' do
278
+ let(:source) do
279
+ [
280
+ 'def some_method',
281
+ ' foo = 1',
282
+ ' foo = 3',
283
+ ' puts foo',
284
+ 'end'
285
+ ]
286
+ end
287
+
288
+ it 'registers an offence for the unreferenced assignment' do
289
+ inspect_source(cop, source)
290
+ expect(cop.offences.size).to eq(1)
291
+ expect(cop.offences.first.message)
292
+ .to eq('Useless assignment to variable - foo')
293
+ expect(cop.offences.first.line).to eq(2)
294
+ expect(cop.highlights).to eq(['foo'])
295
+ end
296
+ end
297
+
298
+ context 'when an unreferenced variable is reassigned in a block' do
299
+ let(:source) do
300
+ [
301
+ 'def const_name(node)',
302
+ ' const_names = []',
303
+ ' const_node = node',
304
+ '',
305
+ ' loop do',
306
+ ' namespace_node, name = *const_node',
307
+ ' const_names << name',
308
+ ' break unless namespace_node',
309
+ ' break if namespace_node.type == :cbase',
310
+ ' const_node = namespace_node',
311
+ ' end',
312
+ '',
313
+ " const_names.reverse.join('::')",
314
+ 'end'
315
+ ]
316
+ end
317
+
318
+ include_examples 'accepts'
319
+ include_examples 'mimics MRI 2.0'
320
+ end
321
+
322
+ context 'when a referenced variable is reassigned in a block' do
323
+ let(:source) do
324
+ [
325
+ 'def some_method',
326
+ ' foo = 1',
327
+ ' puts foo',
328
+ ' 1.times do',
329
+ ' foo = 2',
330
+ ' end',
331
+ 'end'
332
+ ]
333
+ end
334
+
335
+ include_examples 'accepts'
336
+ include_examples 'mimics MRI 2.0'
337
+ end
338
+
339
+ context 'when a block local variable is declared but not assigned' do
340
+ let(:source) do
341
+ [
342
+ '1.times do |i; foo|',
343
+ 'end'
344
+ ]
345
+ end
346
+
347
+ it 'registers an offence for the declaration' do
348
+ inspect_source(cop, source)
349
+ expect(cop.offences.size).to eq(1)
350
+ expect(cop.offences.first.message)
351
+ .to eq('Useless assignment to variable - foo')
352
+ expect(cop.offences.first.line).to eq(1)
353
+ expect(cop.highlights).to eq(['foo'])
354
+ end
355
+
356
+ include_examples 'mimics MRI 2.0'
357
+ end
358
+
359
+ context 'when a block local variable is assigned and unreferenced' do
360
+ let(:source) do
361
+ [
362
+ '1.times do |i; foo|',
363
+ ' foo = 2',
364
+ 'end'
365
+ ]
366
+ end
367
+
368
+ it 'registers offences for the assignment' do
369
+ inspect_source(cop, source)
370
+ expect(cop.offences.size).to eq(1)
371
+ expect(cop.offences.first.message)
372
+ .to eq('Useless assignment to variable - foo')
373
+ expect(cop.offences.first.line).to eq(2)
374
+ expect(cop.highlights).to eq(['foo'])
375
+ end
376
+ end
377
+
378
+ context 'when a variable is assigned in loop body and unreferenced' do
379
+ let(:source) do
380
+ [
381
+ 'def some_method',
382
+ ' while true',
383
+ ' foo = 1',
384
+ ' end',
385
+ 'end'
386
+ ]
387
+ end
388
+
389
+ it 'registers an offence' do
390
+ inspect_source(cop, source)
391
+ expect(cop.offences.size).to eq(1)
392
+ expect(cop.offences.first.message)
393
+ .to eq('Useless assignment to variable - foo')
394
+ expect(cop.offences.first.line).to eq(3)
395
+ expect(cop.highlights).to eq(['foo'])
396
+ end
397
+
398
+ include_examples 'mimics MRI 2.0'
399
+ end
400
+
401
+ context 'when a variable is reassigned at the end of loop body ' +
402
+ 'and would be referenced in next iteration' do
403
+ let(:source) do
404
+ [
405
+ 'def some_method',
406
+ ' total = 0',
407
+ ' foo = 0',
408
+ '',
409
+ ' while total < 100',
410
+ ' total += foo',
411
+ ' foo += 1',
412
+ ' end',
413
+ '',
414
+ ' total',
415
+ 'end'
416
+ ]
417
+ end
418
+
419
+ include_examples 'accepts'
420
+ include_examples 'mimics MRI 2.0'
421
+ end
422
+
423
+ context 'when a variable is reassigned at the end of loop body ' +
424
+ 'and would be referenced in loop condition' do
425
+ let(:source) do
426
+ [
427
+ 'def some_method',
428
+ ' total = 0',
429
+ ' foo = 0',
430
+ '',
431
+ ' while foo < 100',
432
+ ' total += 1',
433
+ ' foo += 1',
434
+ ' end',
435
+ '',
436
+ ' total',
437
+ 'end'
438
+ ]
439
+ end
440
+
441
+ include_examples 'accepts'
442
+ include_examples 'mimics MRI 2.0'
443
+ end
444
+
445
+ context "when a variable is reassigned in loop body but won't " +
446
+ 'be referenced either next iteration or loop condition' do
447
+ let(:source) do
448
+ [
449
+ 'def some_method',
450
+ ' total = 0',
451
+ ' foo = 0',
452
+ '',
453
+ ' while total < 100',
454
+ ' total += 1',
455
+ ' foo += 1',
456
+ ' end',
457
+ '',
458
+ ' total',
459
+ 'end'
460
+ ]
461
+ end
462
+
463
+ it 'registers an offence' do
464
+ inspect_source(cop, source)
465
+ expect(cop.offences.size).to eq(1)
466
+ expect(cop.offences.first.message)
467
+ .to eq('Useless assignment to variable - foo')
468
+ expect(cop.offences.first.line).to eq(7)
469
+ expect(cop.highlights).to eq(['foo'])
470
+ end
471
+ end
472
+
473
+ context 'when a referenced variable is reassigned ' +
474
+ 'but not re-referenced in a method defined in loop' do
475
+ let(:source) do
476
+ [
477
+ 'while true',
478
+ ' def some_method',
479
+ ' foo = 1',
480
+ ' puts foo',
481
+ ' foo = 3',
482
+ ' end',
483
+ 'end'
484
+ ]
485
+ end
486
+
487
+ it 'registers an offence' do
488
+ inspect_source(cop, source)
489
+ expect(cop.offences.size).to eq(1)
490
+ expect(cop.offences.first.message)
491
+ .to eq('Useless assignment to variable - foo')
492
+ expect(cop.offences.first.line).to eq(5)
493
+ expect(cop.highlights).to eq(['foo'])
494
+ end
495
+ end
496
+
497
+ context 'when a variable that has same name as outer scope variable ' +
498
+ 'is not referenced in a method defined in loop' do
499
+ let(:source) do
500
+ [
501
+ 'foo = 1',
502
+ '',
503
+ 'while foo < 100',
504
+ ' foo += 1',
505
+ ' def some_method',
506
+ ' foo = 1',
507
+ ' end',
508
+ 'end'
509
+ ]
510
+ end
511
+
512
+ it 'registers an offence' do
513
+ inspect_source(cop, source)
514
+ expect(cop.offences.size).to eq(1)
515
+ expect(cop.offences.first.message)
516
+ .to eq('Useless assignment to variable - foo')
517
+ expect(cop.offences.first.line).to eq(6)
518
+ expect(cop.highlights).to eq(['foo'])
519
+ end
520
+ end
521
+
522
+ context 'when a variable is assigned in single branch if ' +
523
+ 'and unreferenced' do
524
+ let(:source) do
525
+ [
526
+ 'def some_method(flag)',
527
+ ' if flag',
528
+ ' foo = 1',
529
+ ' end',
530
+ 'end'
531
+ ]
532
+ end
533
+
534
+ it 'registers an offence' do
535
+ inspect_source(cop, source)
536
+ expect(cop.offences.size).to eq(1)
537
+ expect(cop.offences.first.message)
538
+ .to eq('Useless assignment to variable - foo')
539
+ expect(cop.offences.first.line).to eq(3)
540
+ expect(cop.highlights).to eq(['foo'])
541
+ end
542
+
543
+ include_examples 'mimics MRI 2.0'
544
+ end
545
+
546
+ context 'when a unreferenced variable is reassigned in same branch ' +
547
+ 'and referenced after the branching' do
548
+ let(:source) do
549
+ [
550
+ 'def some_method(flag)',
551
+ ' if flag',
552
+ ' foo = 1',
553
+ ' foo = 2',
554
+ ' end',
555
+ '',
556
+ ' foo',
557
+ 'end'
558
+ ]
559
+ end
560
+
561
+ it 'registers an offence for the unreferenced assignment' do
562
+ inspect_source(cop, source)
563
+ expect(cop.offences.size).to eq(1)
564
+ expect(cop.offences.first.message)
565
+ .to eq('Useless assignment to variable - foo')
566
+ expect(cop.offences.first.line).to eq(3)
567
+ expect(cop.highlights).to eq(['foo'])
568
+ end
569
+ end
570
+
571
+ context 'when a variable is reassigned in single branch if ' +
572
+ 'and referenced after the branching' do
573
+ let(:source) do
574
+ [
575
+ 'def some_method(flag)',
576
+ ' foo = 1',
577
+ '',
578
+ ' if flag',
579
+ ' foo = 2',
580
+ ' end',
581
+ '',
582
+ ' foo',
583
+ 'end'
584
+ ]
585
+ end
586
+
587
+ include_examples 'accepts'
588
+ include_examples 'mimics MRI 2.0'
589
+ end
590
+
591
+ context 'when a variable is assigned in each branch of if ' +
592
+ 'and referenced after the branching' do
593
+ let(:source) do
594
+ [
595
+ 'def some_method(flag)',
596
+ ' if flag',
597
+ ' foo = 2',
598
+ ' else',
599
+ ' foo = 3',
600
+ ' end',
601
+ '',
602
+ ' foo',
603
+ 'end'
604
+ ]
605
+ end
606
+
607
+ include_examples 'accepts'
608
+ include_examples 'mimics MRI 2.0'
609
+ end
610
+
611
+ context 'when a variable is reassigned in single branch if ' +
612
+ 'and referenced in the branch' do
613
+ let(:source) do
614
+ [
615
+ 'def some_method(flag)',
616
+ ' foo = 1',
617
+ '',
618
+ ' if flag',
619
+ ' foo = 2',
620
+ ' puts foo',
621
+ ' end',
622
+ 'end'
623
+ ]
624
+ end
625
+
626
+ it 'registers an offence for the unreferenced assignment' do
627
+ inspect_source(cop, source)
628
+ expect(cop.offences.size).to eq(1)
629
+ expect(cop.offences.first.message)
630
+ .to eq('Useless assignment to variable - foo')
631
+ expect(cop.offences.first.line).to eq(2)
632
+ expect(cop.highlights).to eq(['foo'])
633
+ end
634
+ end
635
+
636
+ context 'when a variable is assigned in each branch of if ' +
637
+ 'and referenced in the else branch' do
638
+ let(:source) do
639
+ [
640
+ 'def some_method(flag)',
641
+ ' if flag',
642
+ ' foo = 2',
643
+ ' else',
644
+ ' foo = 3',
645
+ ' puts foo',
646
+ ' end',
647
+ 'end'
648
+ ]
649
+ end
650
+
651
+ it 'registers an offence for the assignment in the if branch' do
652
+ inspect_source(cop, source)
653
+ expect(cop.offences.size).to eq(1)
654
+ expect(cop.offences.first.message)
655
+ .to eq('Useless assignment to variable - foo')
656
+ expect(cop.offences.first.line).to eq(3)
657
+ expect(cop.highlights).to eq(['foo'])
658
+ end
659
+ end
660
+
661
+ context 'when a variable is assigned in branch of modifier if ' +
662
+ 'that references the variable in its conditional clause' +
663
+ 'and referenced after the branching' do
664
+ let(:source) do
665
+ [
666
+ 'def some_method(flag)',
667
+ ' foo = 1 unless foo',
668
+ ' puts foo',
669
+ 'end'
670
+ ]
671
+ end
672
+
673
+ include_examples 'accepts'
674
+ include_examples 'mimics MRI 2.0'
675
+ end
676
+
677
+ context 'when a variable is assigned in branch of modifier if ' +
678
+ 'that references the variable in its conditional clause' +
679
+ 'and unreferenced' do
680
+ let(:source) do
681
+ [
682
+ 'def some_method(flag)',
683
+ ' foo = 1 unless foo',
684
+ 'end'
685
+ ]
686
+ end
687
+
688
+ it 'registers an offence' do
689
+ inspect_source(cop, source)
690
+ expect(cop.offences.size).to eq(1)
691
+ expect(cop.offences.first.message)
692
+ .to eq('Useless assignment to variable - foo')
693
+ expect(cop.offences.first.line).to eq(2)
694
+ expect(cop.highlights).to eq(['foo'])
695
+ end
696
+ end
697
+
698
+ context 'when a variable is assigned on each side of && ' +
699
+ 'and referenced after the &&' do
700
+ let(:source) do
701
+ [
702
+ 'def some_method',
703
+ ' (foo = do_something_returns_object_or_nil) && (foo = 1)',
704
+ ' foo',
705
+ 'end'
706
+ ]
707
+ end
708
+
709
+ include_examples 'accepts'
710
+ include_examples 'mimics MRI 2.0'
711
+ end
712
+
713
+ context 'when a unreferenced variable is reassigned ' +
714
+ 'on the left side of && and referenced after the &&' do
715
+ let(:source) do
716
+ [
717
+ 'def some_method',
718
+ ' foo = 1',
719
+ ' (foo = do_something_returns_object_or_nil) && do_something',
720
+ ' foo',
721
+ 'end'
722
+ ]
723
+ end
724
+
725
+ it 'registers an offence for the unreferenced assignment' do
726
+ inspect_source(cop, source)
727
+ expect(cop.offences.size).to eq(1)
728
+ expect(cop.offences.first.message)
729
+ .to eq('Useless assignment to variable - foo')
730
+ expect(cop.offences.first.line).to eq(2)
731
+ expect(cop.highlights).to eq(['foo'])
732
+ end
733
+ end
734
+
735
+ context 'when a unreferenced variable is reassigned ' +
736
+ 'on the right side of && and referenced after the &&' do
737
+ let(:source) do
738
+ [
739
+ 'def some_method',
740
+ ' foo = 1',
741
+ ' do_something_returns_object_or_nil && foo = 2',
742
+ ' foo',
743
+ 'end'
744
+ ]
745
+ end
746
+
747
+ include_examples 'accepts'
748
+ include_examples 'mimics MRI 2.0'
749
+ end
750
+
751
+ context 'when a variable is reassigned ' +
752
+ 'while referencing itself in rhs and referenced' do
753
+ let(:source) do
754
+ [
755
+ 'def some_method',
756
+ ' foo = [1, 2]',
757
+ ' foo = foo.map { |i| i + 1 }',
758
+ ' puts foo',
759
+ 'end'
760
+ ]
761
+ end
762
+
763
+ include_examples 'accepts'
764
+ include_examples 'mimics MRI 2.0'
765
+ end
766
+
767
+ context 'when a variable is reassigned ' +
768
+ 'with binary operator assignment and referenced' do
769
+ let(:source) do
770
+ [
771
+ 'def some_method',
772
+ ' foo = 1',
773
+ ' foo += 1',
774
+ ' foo',
775
+ 'end'
776
+ ]
777
+ end
778
+
779
+ include_examples 'accepts'
780
+ include_examples 'mimics MRI 2.0'
781
+ end
782
+
783
+ context 'when a variable is reassigned ' +
784
+ 'with logical operator assignment and referenced' do
785
+ let(:source) do
786
+ [
787
+ 'def some_method',
788
+ ' foo = do_something_returns_object_or_nil',
789
+ ' foo ||= 1',
790
+ ' foo',
791
+ 'end'
792
+ ]
793
+ end
794
+
795
+ include_examples 'accepts'
796
+ include_examples 'mimics MRI 2.0'
797
+ end
798
+
799
+ context 'when a variable is reassigned with binary operator ' +
800
+ 'assignment while assigning to itself in rhs ' +
801
+ 'then referenced' do
802
+ let(:source) do
803
+ [
804
+ 'def some_method',
805
+ ' foo = 1',
806
+ ' foo += foo = 2',
807
+ ' foo',
808
+ 'end'
809
+ ]
810
+ end
811
+
812
+ it 'registers an offence for the assignment in rhs' do
813
+ inspect_source(cop, source)
814
+ expect(cop.offences.size).to eq(1)
815
+ expect(cop.offences.first.message)
816
+ .to eq('Useless assignment to variable - foo')
817
+ expect(cop.offences.first.line).to eq(3)
818
+ expect(cop.highlights).to eq(['foo'])
819
+ end
820
+ end
821
+
822
+ context 'when a variable is assigned first with ||= and referenced' do
823
+ let(:source) do
824
+ [
825
+ 'def some_method',
826
+ ' foo ||= 1',
827
+ ' foo',
828
+ 'end'
829
+ ]
830
+ end
831
+
832
+ include_examples 'accepts'
833
+ include_examples 'mimics MRI 2.0'
834
+ end
835
+
836
+ context 'when a variable is assigned with ||= ' +
837
+ 'at the last expression of the scope' do
838
+ let(:source) do
839
+ [
840
+ 'def some_method',
841
+ ' foo = do_something_returns_object_or_nil',
842
+ ' foo ||= 1',
843
+ 'end'
844
+ ]
845
+ end
846
+
847
+ it 'registers an offence' do
848
+ inspect_source(cop, source)
849
+ expect(cop.offences.size).to eq(1)
850
+ expect(cop.offences.first.message).to eq(
851
+ 'Useless assignment to variable - foo. Use just operator ||.'
852
+ )
853
+ expect(cop.offences.first.line).to eq(3)
854
+ expect(cop.highlights).to eq(['foo'])
855
+ end
856
+ end
857
+
858
+ context 'when a variable is assigned with ||= ' +
859
+ 'before the last expression of the scope' do
860
+ let(:source) do
861
+ [
862
+ 'def some_method',
863
+ ' foo = do_something_returns_object_or_nil',
864
+ ' foo ||= 1',
865
+ ' some_return_value',
866
+ 'end'
867
+ ]
868
+ end
869
+
870
+ it 'registers an offence' do
871
+ inspect_source(cop, source)
872
+ expect(cop.offences.size).to eq(1)
873
+ expect(cop.offences.first.message)
874
+ .to eq('Useless assignment to variable - foo')
875
+ expect(cop.offences.first.line).to eq(3)
876
+ expect(cop.highlights).to eq(['foo'])
877
+ end
878
+ end
879
+
880
+ context 'when a variable is assigned with multiple assignment ' +
881
+ 'and unreferenced' do
882
+ let(:source) do
883
+ [
884
+ 'def some_method',
885
+ ' foo, bar = do_something',
886
+ ' puts foo',
887
+ 'end'
888
+ ]
889
+ end
890
+
891
+ it 'registers an offence' do
892
+ inspect_source(cop, source)
893
+ expect(cop.offences.size).to eq(1)
894
+ expect(cop.offences.first.message).to eq(
895
+ 'Useless assignment to variable - bar. ' +
896
+ 'Use _ or _bar as a variable name ' +
897
+ "to indicate that it won't be used."
898
+ )
899
+ expect(cop.offences.first.line).to eq(2)
900
+ expect(cop.highlights).to eq(['bar'])
901
+ end
902
+
903
+ include_examples 'mimics MRI 2.0'
904
+ end
905
+
906
+ context 'when a variable is reassigned with multiple assignment ' +
907
+ 'while referencing itself in rhs and referenced' do
908
+ let(:source) do
909
+ [
910
+ 'def some_method',
911
+ ' foo = 1',
912
+ ' foo, bar = do_something(foo)',
913
+ ' puts foo, bar',
914
+ 'end'
915
+ ]
916
+ end
917
+
918
+ include_examples 'accepts'
919
+ include_examples 'mimics MRI 2.0'
920
+ end
921
+
922
+ context 'when a variable is assigned in loop body ' +
923
+ 'and referenced in post while condition' do
924
+ let(:source) do
925
+ [
926
+ 'begin',
927
+ ' a = (a || 0) + 1',
928
+ ' puts a',
929
+ 'end while a <= 2'
930
+ ]
931
+ end
932
+
933
+ include_examples 'accepts'
934
+ include_examples 'mimics MRI 2.0'
935
+ end
936
+
937
+ context 'when a variable is assigned in loop body ' +
938
+ 'and referenced in post until condition' do
939
+ let(:source) do
940
+ [
941
+ 'begin',
942
+ ' a = (a || 0) + 1',
943
+ ' puts a',
944
+ 'end until a > 2'
945
+ ]
946
+ end
947
+
948
+ include_examples 'accepts'
949
+ include_examples 'mimics MRI 2.0'
950
+ end
951
+
952
+ context 'when a variable is assigned ' +
953
+ 'in main body of begin with rescue but unreferenced' do
954
+ let(:source) do
955
+ [
956
+ 'begin',
957
+ ' do_something',
958
+ ' foo = true',
959
+ 'rescue',
960
+ ' do_anything',
961
+ 'end'
962
+ ]
963
+ end
964
+
965
+ it 'registers an offence' do
966
+ inspect_source(cop, source)
967
+ expect(cop.offences.size).to eq(1)
968
+ expect(cop.offences.first.message)
969
+ .to eq('Useless assignment to variable - foo')
970
+ expect(cop.offences.first.line).to eq(3)
971
+ expect(cop.highlights).to eq(['foo'])
972
+ end
973
+
974
+ include_examples 'mimics MRI 2.0'
975
+ end
976
+
977
+ context 'when a variable is assigned in main body of begin, rescue ' +
978
+ 'and else then referenced after the begin' do
979
+ let(:source) do
980
+ [
981
+ 'begin',
982
+ ' do_something',
983
+ ' foo = :in_begin',
984
+ 'rescue FirstError',
985
+ ' foo = :in_first_rescue',
986
+ 'rescue SecondError',
987
+ ' foo = :in_second_rescue',
988
+ 'else',
989
+ ' foo = :in_else',
990
+ 'end',
991
+ '',
992
+ 'puts foo'
993
+ ]
994
+ end
995
+
996
+ include_examples 'accepts'
997
+ include_examples 'mimics MRI 2.0'
998
+ end
999
+
1000
+ context 'when a variable is reassigned multiple times ' +
1001
+ 'in main body of begin then referenced after the begin' do
1002
+ let(:source) do
1003
+ [
1004
+ 'begin',
1005
+ ' status = :initial',
1006
+ ' connect_sometimes_fails!',
1007
+ ' status = :connected',
1008
+ ' fetch_sometimes_fails!',
1009
+ ' status = :fetched',
1010
+ 'rescue',
1011
+ ' do_something',
1012
+ 'end',
1013
+ '',
1014
+ 'puts status'
1015
+ ]
1016
+ end
1017
+
1018
+ include_examples 'accepts'
1019
+ include_examples 'mimics MRI 2.0'
1020
+ end
1021
+
1022
+ context 'when a variable is reassigned multiple times ' +
1023
+ 'in main body of begin then referenced in rescue' do
1024
+ let(:source) do
1025
+ [
1026
+ 'begin',
1027
+ ' status = :initial',
1028
+ ' connect_sometimes_fails!',
1029
+ ' status = :connected',
1030
+ ' fetch_sometimes_fails!',
1031
+ ' status = :fetched',
1032
+ 'rescue',
1033
+ ' puts status',
1034
+ 'end'
1035
+ ]
1036
+ end
1037
+
1038
+ include_examples 'accepts'
1039
+ include_examples 'mimics MRI 2.0'
1040
+ end
1041
+
1042
+ context 'when a variable is reassigned multiple times ' +
1043
+ 'in main body of begin then referenced in ensure' do
1044
+ let(:source) do
1045
+ [
1046
+ 'begin',
1047
+ ' status = :initial',
1048
+ ' connect_sometimes_fails!',
1049
+ ' status = :connected',
1050
+ ' fetch_sometimes_fails!',
1051
+ ' status = :fetched',
1052
+ 'ensure',
1053
+ ' puts status',
1054
+ 'end'
1055
+ ]
1056
+ end
1057
+
1058
+ include_examples 'accepts'
1059
+ include_examples 'mimics MRI 2.0'
1060
+ end
1061
+
1062
+ context 'when a variable is reassigned multiple times in rescue ' +
1063
+ 'and referenced after the begin' do
1064
+ let(:source) do
1065
+ [
1066
+ 'foo = false',
1067
+ '',
1068
+ 'begin',
1069
+ ' do_something',
1070
+ 'rescue',
1071
+ ' foo = true',
1072
+ ' foo = true',
1073
+ 'end',
1074
+ '',
1075
+ 'puts foo'
1076
+ ]
1077
+ end
1078
+
1079
+ it 'registers an offence' do
1080
+ inspect_source(cop, source)
1081
+ expect(cop.offences.size).to eq(1)
1082
+ expect(cop.offences.first.message)
1083
+ .to eq('Useless assignment to variable - foo')
1084
+ expect(cop.offences.first.line).to eq(6)
1085
+ expect(cop.highlights).to eq(['foo'])
1086
+ end
1087
+ end
1088
+
1089
+ context 'when a variable is reassigned multiple times ' +
1090
+ 'in rescue with ensure then referenced after the begin' do
1091
+ let(:source) do
1092
+ [
1093
+ 'foo = false',
1094
+ '',
1095
+ 'begin',
1096
+ ' do_something',
1097
+ 'rescue',
1098
+ ' foo = true',
1099
+ ' foo = true',
1100
+ 'ensure',
1101
+ ' do_anything',
1102
+ 'end',
1103
+ '',
1104
+ 'puts foo'
1105
+ ]
1106
+ end
1107
+
1108
+ it 'registers an offence' do
1109
+ inspect_source(cop, source)
1110
+ expect(cop.offences.size).to eq(1)
1111
+ expect(cop.offences.first.message)
1112
+ .to eq('Useless assignment to variable - foo')
1113
+ expect(cop.offences.first.line).to eq(6)
1114
+ expect(cop.highlights).to eq(['foo'])
1115
+ end
1116
+ end
1117
+
1118
+ context 'when a variable is reassigned multiple times ' +
1119
+ 'in ensure with rescue then referenced after the begin' do
1120
+ let(:source) do
1121
+ [
1122
+ 'begin',
1123
+ ' do_something',
1124
+ 'rescue',
1125
+ ' do_anything',
1126
+ 'ensure',
1127
+ ' foo = true',
1128
+ ' foo = true',
1129
+ 'end',
1130
+ '',
1131
+ 'puts foo'
1132
+ ]
1133
+ end
1134
+
1135
+ it 'registers an offence' do
1136
+ inspect_source(cop, source)
1137
+ expect(cop.offences.size).to eq(1)
1138
+ expect(cop.offences.first.message)
1139
+ .to eq('Useless assignment to variable - foo')
1140
+ expect(cop.offences.first.line).to eq(6)
1141
+ expect(cop.highlights).to eq(['foo'])
1142
+ end
1143
+ end
1144
+
1145
+ context 'when a variable is assigned at the end of rescue ' +
1146
+ 'and would be referenced with retry' do
1147
+ let(:source) do
1148
+ [
1149
+ 'retried = false',
1150
+ '',
1151
+ 'begin',
1152
+ ' do_something',
1153
+ 'rescue',
1154
+ ' fail if retried',
1155
+ ' retried = true',
1156
+ ' retry',
1157
+ 'end'
1158
+ ]
1159
+ end
1160
+
1161
+ include_examples 'accepts'
1162
+ include_examples 'mimics MRI 2.0'
1163
+ end
1164
+
1165
+ context 'when a variable is assigned ' +
1166
+ 'in main body of begin, rescue and else ' +
1167
+ 'and reassigned in ensure then referenced after the begin' do
1168
+ let(:source) do
1169
+ [
1170
+ 'begin',
1171
+ ' do_something',
1172
+ ' foo = :in_begin',
1173
+ 'rescue FirstError',
1174
+ ' foo = :in_first_rescue',
1175
+ 'rescue SecondError',
1176
+ ' foo = :in_second_rescue',
1177
+ 'else',
1178
+ ' foo = :in_else',
1179
+ 'ensure',
1180
+ ' foo = :in_ensure',
1181
+ 'end',
1182
+ '',
1183
+ 'puts foo'
1184
+ ]
1185
+ end
1186
+
1187
+ it 'registers offences for each assignment before ensure' do
1188
+ inspect_source(cop, source)
1189
+ expect(cop.offences.size).to eq(4)
1190
+
1191
+ expect(cop.offences[0].line).to eq(3)
1192
+ expect(cop.offences[1].line).to eq(5)
1193
+ expect(cop.offences[2].line).to eq(7)
1194
+ expect(cop.offences[3].line).to eq(9)
1195
+ end
1196
+ end
1197
+
1198
+ context 'when a method argument is reassigned ' +
1199
+ 'and zero arity super is called' do
1200
+ let(:source) do
1201
+ [
1202
+ 'def some_method(foo)',
1203
+ ' foo = 1',
1204
+ ' super',
1205
+ 'end'
1206
+ ]
1207
+ end
1208
+
1209
+ include_examples 'accepts'
1210
+ include_examples 'mimics MRI 2.0'
1211
+ end
1212
+
1213
+ context 'when a local variable is unreferenced ' +
1214
+ 'and zero arity super is called' do
1215
+ let(:source) do
1216
+ [
1217
+ 'def some_method(bar)',
1218
+ ' foo = 1',
1219
+ ' super',
1220
+ 'end'
1221
+ ]
1222
+ end
1223
+
1224
+ it 'registers an offence' do
1225
+ inspect_source(cop, source)
1226
+ expect(cop.offences.size).to eq(1)
1227
+ expect(cop.offences.first.message)
1228
+ .to eq('Useless assignment to variable - foo')
1229
+ expect(cop.offences.first.line).to eq(2)
1230
+ expect(cop.highlights).to eq(['foo'])
1231
+ end
1232
+
1233
+ include_examples 'mimics MRI 2.0'
1234
+ end
1235
+
1236
+ context 'when a method argument is reassigned ' +
1237
+ 'but not passed to super' do
1238
+ let(:source) do
1239
+ [
1240
+ 'def some_method(foo, bar)',
1241
+ ' foo = 1',
1242
+ ' super(bar)',
1243
+ 'end'
1244
+ ]
1245
+ end
1246
+
1247
+ it 'registers an offence' do
1248
+ inspect_source(cop, source)
1249
+ expect(cop.offences.size).to eq(1)
1250
+ expect(cop.offences.first.message)
1251
+ .to eq('Useless assignment to variable - foo')
1252
+ expect(cop.offences.first.line).to eq(2)
1253
+ expect(cop.highlights).to eq(['foo'])
1254
+ end
1255
+ end
1256
+
1257
+ context 'when a named capture is unreferenced in top level' do
1258
+ let(:source) do
1259
+ [
1260
+ "/(?<foo>\w+)/ =~ 'FOO'",
1261
+ ]
1262
+ end
1263
+
1264
+ it 'registers an offence' do
1265
+ inspect_source(cop, source)
1266
+ expect(cop.offences.size).to eq(1)
1267
+ expect(cop.offences.first.message)
1268
+ .to eq('Useless assignment to variable - foo')
1269
+ expect(cop.offences.first.line).to eq(1)
1270
+ end
1271
+
1272
+ include_examples 'mimics MRI 2.0'
1273
+ end
1274
+
1275
+ context 'when a named capture is unreferenced ' +
1276
+ 'in other than top level' do
1277
+ let(:source) do
1278
+ [
1279
+ 'def some_method',
1280
+ " /(?<foo>\w+)/ =~ 'FOO'",
1281
+ 'end'
1282
+ ]
1283
+ end
1284
+
1285
+ it 'registers an offence' do
1286
+ inspect_source(cop, source)
1287
+ expect(cop.offences.size).to eq(1)
1288
+ expect(cop.offences.first.message)
1289
+ .to eq('Useless assignment to variable - foo')
1290
+ expect(cop.offences.first.line).to eq(2)
1291
+ expect(cop.highlights).to eq(["/(?<foo>\w+)/"])
1292
+ end
1293
+
1294
+ # MRI 2.0 accepts this case, but I have no idea why it does so
1295
+ # and there's no convincing reason to conform to this behavior,
1296
+ # so RuboCop does not mimic MRI in this case.
1297
+ end
1298
+
1299
+ context 'when a named capture is referenced' do
1300
+ let(:source) do
1301
+ [
1302
+ 'def some_method',
1303
+ " /(?<foo>\w+)(?<bar>\s+)/ =~ 'FOO'",
1304
+ ' puts foo',
1305
+ ' puts bar',
1306
+ 'end'
1307
+ ]
1308
+ end
1309
+
1310
+ include_examples 'accepts'
1311
+ include_examples 'mimics MRI 2.0'
1312
+ end
1313
+
1314
+ context 'when a variable is referenced ' +
1315
+ 'in rhs of named capture expression' do
1316
+ let(:source) do
1317
+ [
1318
+ 'def some_method',
1319
+ " foo = 'some string'",
1320
+ " /(?<foo>\w+)/ =~ foo",
1321
+ ' puts foo',
1322
+ 'end'
1323
+ ]
1324
+ end
1325
+
1326
+ include_examples 'accepts'
1327
+ end
1328
+
1329
+ context 'when a variable is assigned in begin ' +
1330
+ 'and referenced outside' do
1331
+ let(:source) do
1332
+ [
1333
+ 'def some_method',
1334
+ ' begin',
1335
+ ' foo = 1',
1336
+ ' end',
1337
+ ' puts foo',
1338
+ 'end'
1339
+ ]
1340
+ end
1341
+
1342
+ include_examples 'accepts'
1343
+ include_examples 'mimics MRI 2.0'
1344
+ end
1345
+
1346
+ context 'when a variable is shadowed by a block argument ' +
1347
+ 'and unreferenced' do
1348
+ let(:source) do
1349
+ [
1350
+ 'def some_method',
1351
+ ' foo = 1',
1352
+ ' 1.times do |foo|',
1353
+ ' puts foo',
1354
+ ' end',
1355
+ 'end'
1356
+ ]
1357
+ end
1358
+
1359
+ it 'registers an offence' do
1360
+ inspect_source(cop, source)
1361
+ expect(cop.offences.size).to eq(1)
1362
+ expect(cop.offences.first.message)
1363
+ .to eq('Useless assignment to variable - foo')
1364
+ expect(cop.offences.first.line).to eq(2)
1365
+ expect(cop.highlights).to eq(['foo'])
1366
+ end
1367
+
1368
+ include_examples 'mimics MRI 2.0', 'unused variable'
1369
+ end
1370
+
1371
+ context 'when a variable is not used and the name starts with _' do
1372
+ let(:source) do
1373
+ [
1374
+ 'def some_method',
1375
+ ' _foo = 1',
1376
+ ' bar = 2',
1377
+ ' puts bar',
1378
+ 'end'
1379
+ ]
1380
+ end
1381
+
1382
+ include_examples 'accepts'
1383
+ include_examples 'mimics MRI 2.0'
1384
+ end
1385
+
1386
+ context 'when a method argument is not used' do
1387
+ let(:source) do
1388
+ [
1389
+ 'def some_method(arg)',
1390
+ 'end'
1391
+ ]
1392
+ end
1393
+
1394
+ include_examples 'accepts'
1395
+ include_examples 'mimics MRI 2.0'
1396
+ end
1397
+
1398
+ context 'when an optional method argument is not used' do
1399
+ let(:source) do
1400
+ [
1401
+ 'def some_method(arg = nil)',
1402
+ 'end'
1403
+ ]
1404
+ end
1405
+
1406
+ include_examples 'accepts'
1407
+ include_examples 'mimics MRI 2.0'
1408
+ end
1409
+
1410
+ context 'when a block method argument is not used' do
1411
+ let(:source) do
1412
+ [
1413
+ 'def some_method(&block)',
1414
+ 'end'
1415
+ ]
1416
+ end
1417
+
1418
+ include_examples 'accepts'
1419
+ include_examples 'mimics MRI 2.0'
1420
+ end
1421
+
1422
+ context 'when a splat method argument is not used' do
1423
+ let(:source) do
1424
+ [
1425
+ 'def some_method(*args)',
1426
+ 'end'
1427
+ ]
1428
+ end
1429
+
1430
+ include_examples 'accepts'
1431
+ include_examples 'mimics MRI 2.0'
1432
+ end
1433
+
1434
+ context 'when a optional keyword method argument is not used' do
1435
+ let(:source) do
1436
+ [
1437
+ 'def some_method(name: value)',
1438
+ 'end'
1439
+ ]
1440
+ end
1441
+
1442
+ include_examples 'accepts'
1443
+ include_examples 'mimics MRI 2.0'
1444
+ end
1445
+
1446
+ context 'when a keyword splat method argument is used' do
1447
+ let(:source) do
1448
+ [
1449
+ 'def some_method(name: value, **rest_keywords)',
1450
+ ' p rest_keywords',
1451
+ 'end'
1452
+ ]
1453
+ end
1454
+
1455
+ include_examples 'accepts'
1456
+ include_examples 'mimics MRI 2.0'
1457
+ end
1458
+
1459
+ context 'when a keyword splat method argument is not used' do
1460
+ let(:source) do
1461
+ [
1462
+ 'def some_method(name: value, **rest_keywords)',
1463
+ 'end'
1464
+ ]
1465
+ end
1466
+
1467
+ include_examples 'accepts'
1468
+ include_examples 'mimics MRI 2.0'
1469
+ end
1470
+
1471
+ context 'when a block argument is not used' do
1472
+ let(:source) do
1473
+ [
1474
+ '1.times do |i|',
1475
+ 'end'
1476
+ ]
1477
+ end
1478
+
1479
+ include_examples 'accepts'
1480
+ include_examples 'mimics MRI 2.0'
1481
+ end
1482
+
1483
+ context 'when there is only one AST node and it is unused variable' do
1484
+ let(:source) do
1485
+ [
1486
+ 'foo = 1'
1487
+ ]
1488
+ end
1489
+
1490
+ it 'registers an offence' do
1491
+ inspect_source(cop, source)
1492
+ expect(cop.offences.size).to eq(1)
1493
+ expect(cop.offences.first.message)
1494
+ .to eq('Useless assignment to variable - foo')
1495
+ expect(cop.offences.first.line).to eq(1)
1496
+ expect(cop.highlights).to eq(['foo'])
1497
+ end
1498
+
1499
+ include_examples 'mimics MRI 2.0'
1500
+ end
1501
+
1502
+ context 'when a variable is assigned ' +
1503
+ 'while being passed to a method taking block' do
1504
+
1505
+ context 'and the variable is used' do
1506
+ let(:source) do
1507
+ [
1508
+ 'some_method(foo = 1) do',
1509
+ 'end',
1510
+ 'puts foo'
1511
+ ]
1555
1512
  end
1513
+
1514
+ include_examples 'accepts'
1515
+ include_examples 'mimics MRI 2.0'
1556
1516
  end
1517
+
1518
+ context 'and the variable is not used' do
1519
+ let(:source) do
1520
+ [
1521
+ 'some_method(foo = 1) do',
1522
+ 'end'
1523
+ ]
1524
+ end
1525
+
1526
+ it 'registers an offence' do
1527
+ inspect_source(cop, source)
1528
+ expect(cop.offences.size).to eq(1)
1529
+ expect(cop.offences.first.message)
1530
+ .to eq('Useless assignment to variable - foo')
1531
+ expect(cop.offences.first.line).to eq(1)
1532
+ expect(cop.highlights).to eq(['foo'])
1533
+ end
1534
+
1535
+ include_examples 'mimics MRI 2.0'
1536
+ end
1537
+ end
1538
+
1539
+ context 'when a variabled is assigned ' +
1540
+ 'and passed to a method followed by method taking block' do
1541
+ let(:source) do
1542
+ [
1543
+ "pattern = '*.rb'",
1544
+ 'Dir.glob(pattern).map do |path|',
1545
+ 'end'
1546
+ ]
1547
+ end
1548
+
1549
+ include_examples 'accepts'
1550
+ include_examples 'mimics MRI 2.0'
1557
1551
  end
1558
1552
  end