rubocop 0.12.0 → 0.13.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 (279) hide show
  1. data/CHANGELOG.md +40 -0
  2. data/README.md +49 -8
  3. data/config/default.yml +40 -0
  4. data/config/enabled.yml +37 -9
  5. data/lib/rubocop.rb +24 -10
  6. data/lib/rubocop/cli.rb +41 -106
  7. data/lib/rubocop/config.rb +3 -2
  8. data/lib/rubocop/cop/commissioner.rb +15 -5
  9. data/lib/rubocop/cop/cop.rb +47 -32
  10. data/lib/rubocop/cop/lint/assignment_in_condition.rb +2 -2
  11. data/lib/rubocop/cop/lint/block_alignment.rb +30 -9
  12. data/lib/rubocop/cop/lint/empty_ensure.rb +1 -1
  13. data/lib/rubocop/cop/lint/end_alignment.rb +4 -4
  14. data/lib/rubocop/cop/lint/end_in_method.rb +1 -1
  15. data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
  16. data/lib/rubocop/cop/lint/eval.rb +1 -3
  17. data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -1
  18. data/lib/rubocop/cop/lint/literal_in_condition.rb +6 -4
  19. data/lib/rubocop/cop/lint/loop.rb +1 -1
  20. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
  21. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  22. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +5 -8
  23. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
  24. data/lib/rubocop/cop/lint/useless_assignment.rb +57 -60
  25. data/lib/rubocop/cop/lint/useless_comparison.rb +1 -1
  26. data/lib/rubocop/cop/lint/useless_setter_call.rb +85 -0
  27. data/lib/rubocop/cop/lint/void.rb +6 -8
  28. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -1
  29. data/lib/rubocop/cop/rails/read_attribute.rb +1 -1
  30. data/lib/rubocop/cop/rails/validation.rb +1 -1
  31. data/lib/rubocop/cop/style/access_control.rb +4 -6
  32. data/lib/rubocop/cop/style/alias.rb +1 -3
  33. data/lib/rubocop/cop/style/align_array.rb +47 -0
  34. data/lib/rubocop/cop/style/align_hash.rb +145 -0
  35. data/lib/rubocop/cop/style/align_parameters.rb +9 -3
  36. data/lib/rubocop/cop/style/and_or.rb +3 -4
  37. data/lib/rubocop/cop/style/ascii_comments.rb +1 -3
  38. data/lib/rubocop/cop/style/ascii_identifiers.rb +1 -1
  39. data/lib/rubocop/cop/style/attr.rb +1 -4
  40. data/lib/rubocop/cop/style/begin_block.rb +1 -1
  41. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  42. data/lib/rubocop/cop/style/block_nesting.rb +2 -2
  43. data/lib/rubocop/cop/style/blocks.rb +14 -2
  44. data/lib/rubocop/cop/style/case_equality.rb +1 -3
  45. data/lib/rubocop/cop/style/case_indentation.rb +1 -1
  46. data/lib/rubocop/cop/style/character_literal.rb +1 -2
  47. data/lib/rubocop/cop/style/class_and_module_camel_case.rb +1 -1
  48. data/lib/rubocop/cop/style/class_methods.rb +1 -3
  49. data/lib/rubocop/cop/style/{avoid_class_vars.rb → class_vars.rb} +6 -2
  50. data/lib/rubocop/cop/style/collection_methods.rb +7 -9
  51. data/lib/rubocop/cop/style/colon_method_call.rb +1 -2
  52. data/lib/rubocop/cop/style/comment_annotation.rb +6 -6
  53. data/lib/rubocop/cop/style/constant_name.rb +1 -3
  54. data/lib/rubocop/cop/style/def_parentheses.rb +4 -12
  55. data/lib/rubocop/cop/style/documentation.rb +2 -2
  56. data/lib/rubocop/cop/style/dot_position.rb +2 -4
  57. data/lib/rubocop/cop/style/empty_line_between_defs.rb +21 -6
  58. data/lib/rubocop/cop/style/empty_lines.rb +1 -1
  59. data/lib/rubocop/cop/style/empty_literal.rb +3 -12
  60. data/lib/rubocop/cop/style/encoding.rb +6 -6
  61. data/lib/rubocop/cop/style/end_block.rb +1 -1
  62. data/lib/rubocop/cop/style/end_of_line.rb +5 -5
  63. data/lib/rubocop/cop/style/even_odd.rb +2 -2
  64. data/lib/rubocop/cop/style/favor_join.rb +1 -3
  65. data/lib/rubocop/cop/style/favor_modifier.rb +7 -3
  66. data/lib/rubocop/cop/style/favor_sprintf.rb +1 -1
  67. data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +1 -1
  68. data/lib/rubocop/cop/style/final_newline.rb +23 -0
  69. data/lib/rubocop/cop/style/{avoid_for.rb → for.rb} +2 -2
  70. data/lib/rubocop/cop/style/{avoid_global_vars.rb → global_vars.rb} +19 -6
  71. data/lib/rubocop/cop/style/hash_methods.rb +3 -5
  72. data/lib/rubocop/cop/style/hash_syntax.rb +4 -4
  73. data/lib/rubocop/cop/style/if_then_else.rb +1 -1
  74. data/lib/rubocop/cop/style/indentation_width.rb +4 -4
  75. data/lib/rubocop/cop/style/lambda.rb +2 -2
  76. data/lib/rubocop/cop/style/leading_comment_space.rb +1 -1
  77. data/lib/rubocop/cop/style/line_length.rb +7 -8
  78. data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +1 -1
  79. data/lib/rubocop/cop/style/method_call_parentheses.rb +1 -4
  80. data/lib/rubocop/cop/style/method_length.rb +4 -4
  81. data/lib/rubocop/cop/style/module_function.rb +1 -3
  82. data/lib/rubocop/cop/style/multiline_block_chain.rb +44 -0
  83. data/lib/rubocop/cop/style/nil_comparison.rb +1 -3
  84. data/lib/rubocop/cop/style/not.rb +1 -1
  85. data/lib/rubocop/cop/style/numeric_literals.rb +26 -6
  86. data/lib/rubocop/cop/style/op_method.rb +2 -2
  87. data/lib/rubocop/cop/style/parameter_lists.rb +4 -4
  88. data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -2
  89. data/lib/rubocop/cop/style/perl_backrefs.rb +26 -0
  90. data/lib/rubocop/cop/style/proc.rb +1 -3
  91. data/lib/rubocop/cop/style/reduce_arguments.rb +7 -5
  92. data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
  93. data/lib/rubocop/cop/style/redundant_return.rb +9 -2
  94. data/lib/rubocop/cop/style/redundant_self.rb +9 -2
  95. data/lib/rubocop/cop/style/regexp_literal.rb +7 -8
  96. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
  97. data/lib/rubocop/cop/style/semicolon.rb +10 -10
  98. data/lib/rubocop/cop/style/signal_exception.rb +2 -4
  99. data/lib/rubocop/cop/style/single_line_methods.rb +2 -4
  100. data/lib/rubocop/cop/style/space_after_comma_etc.rb +1 -1
  101. data/lib/rubocop/cop/style/space_after_control_keyword.rb +1 -1
  102. data/lib/rubocop/cop/style/space_after_method_name.rb +1 -1
  103. data/lib/rubocop/cop/style/space_before_modifier_keyword.rb +34 -0
  104. data/lib/rubocop/cop/style/{avoid_perlisms.rb → special_global_vars.rb} +17 -8
  105. data/lib/rubocop/cop/style/string_literals.rb +1 -2
  106. data/lib/rubocop/cop/style/surrounding_space.rb +9 -8
  107. data/lib/rubocop/cop/style/symbol_array.rb +1 -1
  108. data/lib/rubocop/cop/style/symbol_name.rb +9 -2
  109. data/lib/rubocop/cop/style/tab.rb +5 -5
  110. data/lib/rubocop/cop/style/ternary_operator.rb +2 -6
  111. data/lib/rubocop/cop/style/trailing_blank_lines.rb +32 -0
  112. data/lib/rubocop/cop/style/trailing_whitespace.rb +5 -6
  113. data/lib/rubocop/cop/style/trivial_accessors.rb +5 -5
  114. data/lib/rubocop/cop/style/unless_else.rb +1 -1
  115. data/lib/rubocop/cop/style/variable_interpolation.rb +1 -3
  116. data/lib/rubocop/cop/style/when_then.rb +1 -4
  117. data/lib/rubocop/cop/style/while_until_do.rb +7 -5
  118. data/lib/rubocop/cop/style/word_array.rb +1 -1
  119. data/lib/rubocop/cop/team.rb +100 -0
  120. data/lib/rubocop/cop/variable_inspector.rb +323 -235
  121. data/lib/rubocop/cop/variable_inspector/assignment.rb +103 -0
  122. data/lib/rubocop/cop/variable_inspector/locatable.rb +162 -0
  123. data/lib/rubocop/cop/variable_inspector/reference.rb +31 -0
  124. data/lib/rubocop/cop/variable_inspector/scope.rb +70 -0
  125. data/lib/rubocop/cop/variable_inspector/variable.rb +87 -0
  126. data/lib/rubocop/cop/variable_inspector/variable_table.rb +129 -0
  127. data/lib/rubocop/formatter/json_formatter.rb +8 -8
  128. data/lib/rubocop/formatter/progress_formatter.rb +4 -4
  129. data/lib/rubocop/processed_source.rb +22 -1
  130. data/lib/rubocop/version.rb +1 -1
  131. data/rubocop.gemspec +1 -1
  132. data/spec/rubocop/cli_spec.rb +32 -30
  133. data/spec/rubocop/config_spec.rb +4 -6
  134. data/spec/rubocop/cop/commissioner_spec.rb +4 -5
  135. data/spec/rubocop/cop/cop_spec.rb +8 -26
  136. data/spec/rubocop/cop/lint/assignment_in_condition_spec.rb +5 -9
  137. data/spec/rubocop/cop/lint/block_alignment_spec.rb +105 -57
  138. data/spec/rubocop/cop/lint/empty_ensure_spec.rb +1 -1
  139. data/spec/rubocop/cop/lint/end_alignment_spec.rb +1 -1
  140. data/spec/rubocop/cop/lint/end_in_method_spec.rb +1 -1
  141. data/spec/rubocop/cop/lint/ensure_return_spec.rb +1 -1
  142. data/spec/rubocop/cop/lint/eval_spec.rb +3 -3
  143. data/spec/rubocop/cop/lint/handle_exceptions_spec.rb +2 -2
  144. data/spec/rubocop/cop/lint/literal_in_condition_spec.rb +1 -1
  145. data/spec/rubocop/cop/lint/loop_spec.rb +1 -1
  146. data/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +1 -1
  147. data/spec/rubocop/cop/lint/rescue_exception_spec.rb +5 -5
  148. data/spec/rubocop/cop/lint/unreachable_code_spec.rb +1 -1
  149. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +1545 -108
  150. data/spec/rubocop/cop/lint/useless_comparison_spec.rb +1 -1
  151. data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +101 -0
  152. data/spec/rubocop/cop/lint/void_spec.rb +1 -1
  153. data/spec/rubocop/cop/offence_spec.rb +4 -4
  154. data/spec/rubocop/cop/rails/has_and_belongs_to_many_spec.rb +1 -1
  155. data/spec/rubocop/cop/rails/read_attribute_spec.rb +1 -1
  156. data/spec/rubocop/cop/rails/validation_spec.rb +1 -1
  157. data/spec/rubocop/cop/style/access_control_spec.rb +20 -20
  158. data/spec/rubocop/cop/style/alias_spec.rb +3 -3
  159. data/spec/rubocop/cop/style/align_array_spec.rb +62 -0
  160. data/spec/rubocop/cop/style/align_hash_spec.rb +267 -0
  161. data/spec/rubocop/cop/style/align_parameters_spec.rb +2 -2
  162. data/spec/rubocop/cop/style/and_or_spec.rb +1 -1
  163. data/spec/rubocop/cop/style/ascii_comments_spec.rb +2 -2
  164. data/spec/rubocop/cop/style/ascii_identifiers_spec.rb +2 -2
  165. data/spec/rubocop/cop/style/attr_spec.rb +1 -1
  166. data/spec/rubocop/cop/style/begin_block_spec.rb +1 -1
  167. data/spec/rubocop/cop/style/block_comments_spec.rb +1 -1
  168. data/spec/rubocop/cop/style/block_nesting_spec.rb +3 -3
  169. data/spec/rubocop/cop/style/blocks_spec.rb +25 -1
  170. data/spec/rubocop/cop/style/case_equality_spec.rb +1 -1
  171. data/spec/rubocop/cop/style/case_indentation_spec.rb +5 -5
  172. data/spec/rubocop/cop/style/character_literal_spec.rb +1 -1
  173. data/spec/rubocop/cop/style/class_and_module_camel_case_spec.rb +1 -1
  174. data/spec/rubocop/cop/style/class_methods_spec.rb +1 -1
  175. data/spec/rubocop/cop/style/class_vars_spec.rb +25 -0
  176. data/spec/rubocop/cop/style/collection_methods_spec.rb +5 -6
  177. data/spec/rubocop/cop/style/colon_method_call_spec.rb +0 -3
  178. data/spec/rubocop/cop/style/comment_annotation_spec.rb +20 -18
  179. data/spec/rubocop/cop/style/constant_name_spec.rb +1 -1
  180. data/spec/rubocop/cop/style/def_with_parentheses_spec.rb +1 -1
  181. data/spec/rubocop/cop/style/def_without_parentheses_spec.rb +1 -1
  182. data/spec/rubocop/cop/style/documentation_spec.rb +1 -1
  183. data/spec/rubocop/cop/style/dot_position_spec.rb +5 -5
  184. data/spec/rubocop/cop/style/empty_line_between_defs_spec.rb +35 -4
  185. data/spec/rubocop/cop/style/empty_lines_spec.rb +1 -1
  186. data/spec/rubocop/cop/style/empty_literal_spec.rb +7 -7
  187. data/spec/rubocop/cop/style/encoding_spec.rb +11 -5
  188. data/spec/rubocop/cop/style/end_block_spec.rb +1 -1
  189. data/spec/rubocop/cop/style/end_of_line_spec.rb +4 -3
  190. data/spec/rubocop/cop/style/even_odd_spec.rb +1 -1
  191. data/spec/rubocop/cop/style/favor_join_spec.rb +2 -2
  192. data/spec/rubocop/cop/style/favor_modifier_spec.rb +13 -10
  193. data/spec/rubocop/cop/style/favor_sprintf_spec.rb +4 -4
  194. data/spec/rubocop/cop/style/favor_unless_over_negated_if_spec.rb +1 -1
  195. data/spec/rubocop/cop/style/favor_until_over_negated_while_spec.rb +4 -4
  196. data/spec/rubocop/cop/style/final_newline_spec.rb +25 -0
  197. data/spec/rubocop/cop/style/{avoid_for_spec.rb → for_spec.rb} +8 -12
  198. data/spec/rubocop/cop/style/{avoid_global_vars_spec.rb → global_vars_spec.rb} +13 -3
  199. data/spec/rubocop/cop/style/hash_methods_spec.rb +1 -1
  200. data/spec/rubocop/cop/style/hash_syntax_spec.rb +20 -9
  201. data/spec/rubocop/cop/style/if_with_semicolon_spec.rb +3 -3
  202. data/spec/rubocop/cop/style/indentation_width_spec.rb +19 -19
  203. data/spec/rubocop/cop/style/lambda_spec.rb +6 -6
  204. data/spec/rubocop/cop/style/leading_comment_space_spec.rb +1 -1
  205. data/spec/rubocop/cop/style/line_length_spec.rb +3 -3
  206. data/spec/rubocop/cop/style/method_and_variable_snake_case_spec.rb +8 -9
  207. data/spec/rubocop/cop/style/method_call_parentheses_spec.rb +1 -1
  208. data/spec/rubocop/cop/style/method_length_spec.rb +18 -17
  209. data/spec/rubocop/cop/style/module_function_spec.rb +1 -1
  210. data/spec/rubocop/cop/style/multiline_block_chain_spec.rb +84 -0
  211. data/spec/rubocop/cop/style/multiline_if_then_spec.rb +2 -2
  212. data/spec/rubocop/cop/style/nil_comparison_spec.rb +1 -1
  213. data/spec/rubocop/cop/style/not_spec.rb +1 -1
  214. data/spec/rubocop/cop/style/numeric_literals_spec.rb +15 -25
  215. data/spec/rubocop/cop/style/one_line_conditional_spec.rb +2 -2
  216. data/spec/rubocop/cop/style/op_method_spec.rb +3 -3
  217. data/spec/rubocop/cop/style/parameter_lists_spec.rb +5 -5
  218. data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +4 -8
  219. data/spec/rubocop/cop/style/perl_backrefs_spec.rb +23 -0
  220. data/spec/rubocop/cop/style/proc_spec.rb +1 -1
  221. data/spec/rubocop/cop/style/reduce_arguments_spec.rb +18 -11
  222. data/spec/rubocop/cop/style/redundant_begin_spec.rb +1 -1
  223. data/spec/rubocop/cop/style/redundant_return_spec.rb +16 -1
  224. data/spec/rubocop/cop/style/redundant_self_spec.rb +6 -1
  225. data/spec/rubocop/cop/style/regexp_literal_spec.rb +19 -23
  226. data/spec/rubocop/cop/style/rescue_modifier_spec.rb +3 -3
  227. data/spec/rubocop/cop/style/semicolon_spec.rb +3 -3
  228. data/spec/rubocop/cop/style/signal_exception_spec.rb +1 -1
  229. data/spec/rubocop/cop/style/single_line_methods_spec.rb +22 -18
  230. data/spec/rubocop/cop/style/space_after_colon_spec.rb +4 -4
  231. data/spec/rubocop/cop/style/space_after_comma_spec.rb +4 -4
  232. data/spec/rubocop/cop/style/space_after_control_keyword_spec.rb +1 -1
  233. data/spec/rubocop/cop/style/space_after_method_name_spec.rb +1 -1
  234. data/spec/rubocop/cop/style/space_after_semicolon_spec.rb +3 -3
  235. data/spec/rubocop/cop/style/space_around_braces_spec.rb +13 -12
  236. data/spec/rubocop/cop/style/space_around_equals_in_default_parameter_spec.rb +3 -3
  237. data/spec/rubocop/cop/style/space_around_operators_spec.rb +25 -25
  238. data/spec/rubocop/cop/style/space_before_modifier_keyword_spec.rb +53 -0
  239. data/spec/rubocop/cop/style/space_inside_brackets_spec.rb +9 -9
  240. data/spec/rubocop/cop/style/space_inside_hash_literal_braces_spec.rb +47 -61
  241. data/spec/rubocop/cop/style/space_inside_parens_spec.rb +4 -4
  242. data/spec/rubocop/cop/style/special_global_vars_spec.rb +52 -0
  243. data/spec/rubocop/cop/style/string_literals_spec.rb +5 -5
  244. data/spec/rubocop/cop/style/symbol_array_spec.rb +1 -1
  245. data/spec/rubocop/cop/style/symbol_name_spec.rb +27 -18
  246. data/spec/rubocop/cop/style/tab_spec.rb +1 -1
  247. data/spec/rubocop/cop/style/ternary_operator_spec.rb +2 -2
  248. data/spec/rubocop/cop/style/trailing_blank_lines_spec.rb +24 -0
  249. data/spec/rubocop/cop/style/trailing_whitespace_spec.rb +7 -7
  250. data/spec/rubocop/cop/style/trivial_accessors_spec.rb +6 -14
  251. data/spec/rubocop/cop/style/unless_else_spec.rb +3 -3
  252. data/spec/rubocop/cop/style/variable_interpolation_spec.rb +5 -5
  253. data/spec/rubocop/cop/style/when_then_spec.rb +15 -15
  254. data/spec/rubocop/cop/style/while_until_do_spec.rb +3 -3
  255. data/spec/rubocop/cop/style/word_array_spec.rb +1 -1
  256. data/spec/rubocop/cop/team_spec.rb +158 -0
  257. data/spec/rubocop/cop/variable_inspector/assignment_spec.rb +217 -0
  258. data/spec/rubocop/cop/variable_inspector/locatable_spec.rb +740 -0
  259. data/spec/rubocop/cop/variable_inspector/scope_spec.rb +191 -0
  260. data/spec/rubocop/cop/variable_inspector/variable_spec.rb +79 -0
  261. data/spec/rubocop/cop/variable_inspector/variable_table_spec.rb +275 -0
  262. data/spec/rubocop/cop/variable_inspector_spec.rb +13 -533
  263. data/spec/rubocop/formatter/clang_style_formatter_spec.rb +4 -4
  264. data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +1 -1
  265. data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +3 -3
  266. data/spec/rubocop/formatter/file_list_formatter_spec.rb +3 -3
  267. data/spec/rubocop/formatter/progress_formatter_spec.rb +1 -1
  268. data/spec/spec_helper.rb +5 -1
  269. data/spec/support/ast_helper.rb +15 -0
  270. data/spec/support/shared_context.rb +18 -0
  271. data/spec/support/shared_examples.rb +1 -1
  272. metadata +95 -32
  273. checksums.yaml +0 -7
  274. data/lib/rubocop/cop/lint/unused_local_variable.rb +0 -32
  275. data/lib/rubocop/cop/style/avoid_perl_backrefs.rb +0 -19
  276. data/spec/rubocop/cop/lint/unused_local_variable_spec.rb +0 -588
  277. data/spec/rubocop/cop/style/avoid_class_vars_spec.rb +0 -27
  278. data/spec/rubocop/cop/style/avoid_perl_backrefs_spec.rb +0 -20
  279. data/spec/rubocop/cop/style/avoid_perlisms_spec.rb +0 -47
@@ -6,7 +6,7 @@ module Rubocop
6
6
  module Cop
7
7
  module Lint
8
8
  describe EmptyEnsure do
9
- let(:cop) { EmptyEnsure.new }
9
+ subject(:cop) { EmptyEnsure.new }
10
10
 
11
11
  it 'registers an offence for empty ensure' do
12
12
  inspect_source(cop,
@@ -7,7 +7,7 @@ module Rubocop
7
7
  module Cop
8
8
  module Lint
9
9
  describe EndAlignment do
10
- let(:cop) { EndAlignment.new }
10
+ subject(:cop) { EndAlignment.new }
11
11
 
12
12
  it 'registers an offence for mismatched class end' do
13
13
  inspect_source(cop,
@@ -6,7 +6,7 @@ module Rubocop
6
6
  module Cop
7
7
  module Lint
8
8
  describe EndInMethod do
9
- let(:cop) { EndInMethod.new }
9
+ subject(:cop) { EndInMethod.new }
10
10
 
11
11
  it 'reports an offence for def with an END inside' do
12
12
  src = ['def test',
@@ -6,7 +6,7 @@ module Rubocop
6
6
  module Cop
7
7
  module Lint
8
8
  describe EnsureReturn do
9
- let(:cop) { EnsureReturn.new }
9
+ subject(:cop) { EnsureReturn.new }
10
10
 
11
11
  it 'registers an offence for return in ensure' do
12
12
  inspect_source(cop,
@@ -6,13 +6,13 @@ module Rubocop
6
6
  module Cop
7
7
  module Lint
8
8
  describe Eval do
9
- let(:a) { Eval.new }
9
+ subject(:a) { Eval.new }
10
10
 
11
11
  it 'registers an offence for eval as function' do
12
12
  inspect_source(a,
13
13
  ['eval(something)'])
14
14
  expect(a.offences.size).to eq(1)
15
- expect(a.offences.map(&:message))
15
+ expect(a.messages)
16
16
  .to eq([Eval::MSG])
17
17
  end
18
18
 
@@ -20,7 +20,7 @@ module Rubocop
20
20
  inspect_source(a,
21
21
  ['eval something'])
22
22
  expect(a.offences.size).to eq(1)
23
- expect(a.offences.map(&:message))
23
+ expect(a.messages)
24
24
  .to eq([Eval::MSG])
25
25
  end
26
26
 
@@ -6,7 +6,7 @@ module Rubocop
6
6
  module Cop
7
7
  module Lint
8
8
  describe HandleExceptions do
9
- let(:he) { HandleExceptions.new }
9
+ subject(:he) { HandleExceptions.new }
10
10
 
11
11
  it 'registers an offence for empty rescue block' do
12
12
  inspect_source(he,
@@ -16,7 +16,7 @@ module Rubocop
16
16
  ' #do nothing',
17
17
  'end'])
18
18
  expect(he.offences.size).to eq(1)
19
- expect(he.offences.map(&:message))
19
+ expect(he.messages)
20
20
  .to eq([HandleExceptions::MSG])
21
21
  end
22
22
 
@@ -6,7 +6,7 @@ module Rubocop
6
6
  module Cop
7
7
  module Lint
8
8
  describe LiteralInCondition do
9
- let(:cop) { LiteralInCondition.new }
9
+ subject(:cop) { LiteralInCondition.new }
10
10
 
11
11
  %w(1 2.0 [1] {}).each do |lit|
12
12
  it "registers an offence for literal #{lit} in &&" do
@@ -6,7 +6,7 @@ module Rubocop
6
6
  module Cop
7
7
  module Lint
8
8
  describe Loop do
9
- let(:loop) { Loop.new }
9
+ subject(:loop) { Loop.new }
10
10
 
11
11
  it 'registers an offence for begin/end/while' do
12
12
  inspect_source(loop, ['begin something; top; end while test'])
@@ -6,7 +6,7 @@ module Rubocop
6
6
  module Cop
7
7
  module Lint
8
8
  describe ParenthesesAsGroupedExpression do
9
- let(:cop) { ParenthesesAsGroupedExpression.new }
9
+ subject(:cop) { ParenthesesAsGroupedExpression.new }
10
10
 
11
11
  it 'registers an offence for method call with space before the ' +
12
12
  'parenthesis' do
@@ -6,7 +6,7 @@ module Rubocop
6
6
  module Cop
7
7
  module Lint
8
8
  describe RescueException do
9
- let(:re) { RescueException.new }
9
+ subject(:re) { RescueException.new }
10
10
 
11
11
  it 'registers an offence for rescue from Exception' do
12
12
  inspect_source(re,
@@ -16,7 +16,7 @@ module Rubocop
16
16
  ' #do nothing',
17
17
  'end'])
18
18
  expect(re.offences.size).to eq(1)
19
- expect(re.offences.map(&:message))
19
+ expect(re.messages)
20
20
  .to eq([RescueException::MSG])
21
21
  end
22
22
 
@@ -28,7 +28,7 @@ module Rubocop
28
28
  ' #do nothing',
29
29
  'end'])
30
30
  expect(re.offences.size).to eq(1)
31
- expect(re.offences.map(&:message))
31
+ expect(re.messages)
32
32
  .to eq([RescueException::MSG])
33
33
  end
34
34
 
@@ -40,7 +40,7 @@ module Rubocop
40
40
  ' #do nothing',
41
41
  'end'])
42
42
  expect(re.offences.size).to eq(1)
43
- expect(re.offences.map(&:message))
43
+ expect(re.messages)
44
44
  .to eq([RescueException::MSG])
45
45
  end
46
46
 
@@ -52,7 +52,7 @@ module Rubocop
52
52
  ' #do nothing',
53
53
  'end'])
54
54
  expect(re.offences.size).to eq(1)
55
- expect(re.offences.map(&:message))
55
+ expect(re.messages)
56
56
  .to eq([RescueException::MSG])
57
57
  end
58
58
 
@@ -6,7 +6,7 @@ module Rubocop
6
6
  module Cop
7
7
  module Lint
8
8
  describe UnreachableCode do
9
- let(:uc) { UnreachableCode.new }
9
+ subject(:uc) { UnreachableCode.new }
10
10
 
11
11
  UnreachableCode::NODE_TYPES.each do |t|
12
12
  it "registers an offence for #{t} before other statements" do
@@ -6,114 +6,1551 @@ module Rubocop
6
6
  module Cop
7
7
  module Lint
8
8
  describe UselessAssignment do
9
- let(:cop) { UselessAssignment.new }
10
-
11
- it 'registers an offence for def ending with lvar assignment' do
12
- inspect_source(cop,
13
- ['def test',
14
- ' something',
15
- ' top = 5',
16
- 'end'
17
- ])
18
- expect(cop.offences.size).to eq(1)
19
- end
20
-
21
- it 'registers an offence for defs ending with lvar assignment' do
22
- inspect_source(cop,
23
- ['def Top.test',
24
- ' something',
25
- ' top = 5',
26
- 'end'
27
- ])
28
- expect(cop.offences.size).to eq(1)
29
- end
30
-
31
- it 'registers an offence for def ending with lvar attr assignment' do
32
- inspect_source(cop,
33
- ['def test',
34
- ' top = Top.new',
35
- ' top.attr = 5',
36
- 'end'
37
- ])
38
- expect(cop.offences.size).to eq(1)
39
- end
40
-
41
- it 'registers an offence for defs ending with lvar attr assignment' do
42
- inspect_source(cop,
43
- ['def Top.test',
44
- ' top = Top.new',
45
- ' top.attr = 5',
46
- 'end'
47
- ])
48
- expect(cop.offences.size).to eq(1)
49
- end
50
-
51
- it 'accepts def ending with ivar assignment' do
52
- inspect_source(cop,
53
- ['def test',
54
- ' something',
55
- ' @top = 5',
56
- 'end'
57
- ])
58
- expect(cop.offences).to be_empty
59
- end
60
-
61
- it 'accepts def ending ivar attr assignment' do
62
- inspect_source(cop,
63
- ['def test',
64
- ' something',
65
- ' @top.attr = 5',
66
- 'end'
67
- ])
68
- expect(cop.offences).to be_empty
69
- end
70
-
71
- it 'accepts def ending with argument attr assignment' do
72
- inspect_source(cop,
73
- ['def test(some_arg)',
74
- ' unrelated_local_variable = Top.new',
75
- ' some_arg.attr = 5',
76
- 'end'
77
- ])
78
- expect(cop.offences).to be_empty
79
- end
80
-
81
- context 'when a lvar has an object passed as argument ' +
82
- 'at the end of the method' do
83
- it 'accepts the lvar attr assignment' do
84
- inspect_source(cop,
85
- ['def test(some_arg)',
86
- ' @some_ivar = some_arg',
87
- ' @some_ivar.do_something',
88
- ' some_lvar = @some_ivar',
89
- ' some_lvar.do_something',
90
- ' some_lvar.attr = 5',
91
- 'end'
92
- ])
93
- expect(cop.offences).to be_empty
94
- end
95
- end
96
-
97
- context 'when a lvar declared as an argument ' +
98
- 'is no longer the passed object at the end of the method' do
99
- it 'registers an offence for the lvar attr assignment' do
100
- inspect_source(cop,
101
- ['def test(some_arg)',
102
- ' some_arg = Top.new',
103
- ' some_arg.attr = 5',
104
- 'end'
105
- ])
106
- expect(cop.offences.size).to eq(1)
107
- end
108
- end
109
-
110
- it 'is not confused by operators ending with =' do
111
- inspect_source(cop,
112
- ['def test',
113
- ' top == 5',
114
- 'end'
115
- ])
116
- expect(cop.offences).to be_empty
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'
117
1554
  end
118
1555
  end
119
1556
  end