rubocop 0.29.1 → 0.30.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 (341) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +73 -1
  3. data/README.md +11 -2
  4. data/config/default.yml +194 -6
  5. data/config/disabled.yml +25 -1
  6. data/config/enabled.yml +74 -12
  7. data/lib/rubocop.rb +19 -2
  8. data/lib/rubocop/config.rb +23 -3
  9. data/lib/rubocop/config_loader.rb +9 -1
  10. data/lib/rubocop/cop/autocorrect_logic.rb +26 -0
  11. data/lib/rubocop/cop/cop.rb +33 -17
  12. data/lib/rubocop/cop/lint/assignment_in_condition.rb +16 -4
  13. data/lib/rubocop/cop/lint/block_alignment.rb +5 -14
  14. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  15. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +61 -0
  16. data/lib/rubocop/cop/lint/void.rb +11 -3
  17. data/lib/rubocop/cop/mixin/on_normal_if_unless.rb +17 -0
  18. data/lib/rubocop/cop/performance/detect.rb +72 -0
  19. data/lib/rubocop/cop/performance/flat_map.rb +64 -0
  20. data/lib/rubocop/cop/performance/reverse_each.rb +43 -0
  21. data/lib/rubocop/cop/performance/sample.rb +58 -0
  22. data/lib/rubocop/cop/performance/size.rb +64 -0
  23. data/lib/rubocop/cop/rails/date.rb +121 -0
  24. data/lib/rubocop/cop/rails/find_by.rb +52 -0
  25. data/lib/rubocop/cop/rails/find_each.rb +41 -0
  26. data/lib/rubocop/cop/rails/time_zone.rb +93 -0
  27. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +45 -0
  28. data/lib/rubocop/cop/style/block_delimiters.rb +186 -0
  29. data/lib/rubocop/cop/style/closing_parenthesis_indentation.rb +76 -0
  30. data/lib/rubocop/cop/style/command_literal.rb +120 -0
  31. data/lib/rubocop/cop/style/copyright.rb +87 -0
  32. data/lib/rubocop/cop/style/documentation.rb +32 -0
  33. data/lib/rubocop/cop/style/empty_else.rb +85 -16
  34. data/lib/rubocop/cop/style/encoding.rb +14 -2
  35. data/lib/rubocop/cop/style/end_of_line.rb +5 -0
  36. data/lib/rubocop/cop/style/hash_syntax.rb +86 -23
  37. data/lib/rubocop/cop/style/if_unless_modifier.rb +15 -0
  38. data/lib/rubocop/cop/style/indentation_consistency.rb +13 -6
  39. data/lib/rubocop/cop/style/indentation_width.rb +34 -10
  40. data/lib/rubocop/cop/style/lambda.rb +32 -17
  41. data/lib/rubocop/cop/style/line_end_concatenation.rb +4 -0
  42. data/lib/rubocop/cop/style/missing_else.rb +72 -0
  43. data/lib/rubocop/cop/style/regexp_literal.rb +89 -58
  44. data/lib/rubocop/cop/style/space_around_operators.rb +13 -2
  45. data/lib/rubocop/cop/style/space_before_block_braces.rb +3 -3
  46. data/lib/rubocop/cop/style/space_inside_block_braces.rb +9 -5
  47. data/lib/rubocop/cop/style/symbol_literal.rb +36 -0
  48. data/lib/rubocop/cop/style/trailing_blank_lines.rb +7 -0
  49. data/lib/rubocop/cop/style/trailing_comma.rb +21 -7
  50. data/lib/rubocop/cop/style/trivial_accessors.rb +17 -3
  51. data/lib/rubocop/cop/style/unneeded_capital_w.rb +4 -6
  52. data/lib/rubocop/cop/style/while_until_modifier.rb +10 -0
  53. data/lib/rubocop/cop/util.rb +24 -19
  54. data/lib/rubocop/cop/variable_force.rb +14 -0
  55. data/lib/rubocop/cop/variable_force/reference.rb +10 -4
  56. data/lib/rubocop/formatter/html_formatter.rb +10 -4
  57. data/lib/rubocop/formatter/text_util.rb +5 -7
  58. data/lib/rubocop/options.rb +47 -45
  59. data/lib/rubocop/path_util.rb +12 -8
  60. data/lib/rubocop/processed_source.rb +12 -1
  61. data/lib/rubocop/rake_task.rb +2 -2
  62. data/lib/rubocop/version.rb +1 -1
  63. data/relnotes/v0.30.0.md +145 -0
  64. data/rubocop.gemspec +15 -3
  65. metadata +22 -545
  66. data/.gitignore +0 -54
  67. data/.rspec +0 -1
  68. data/.rubocop.yml +0 -11
  69. data/.rubocop_todo.yml +0 -28
  70. data/.travis.yml +0 -22
  71. data/Gemfile +0 -15
  72. data/Rakefile +0 -41
  73. data/lib/rubocop/cop/style/blocks.rb +0 -80
  74. data/lib/rubocop/cop/style/unneeded_percent_x.rb +0 -25
  75. data/logo/rubocop-final-logo.eps +0 -0
  76. data/spec/.rubocop.yml +0 -5
  77. data/spec/fixtures/html_formatter/expected.html +0 -495
  78. data/spec/fixtures/html_formatter/project/app/controllers/application_controller.rb +0 -5
  79. data/spec/fixtures/html_formatter/project/app/controllers/books_controller.rb +0 -74
  80. data/spec/fixtures/html_formatter/project/app/models/book.rb +0 -5
  81. data/spec/isolated_environment_spec.rb +0 -24
  82. data/spec/project_spec.rb +0 -119
  83. data/spec/rubocop/cli_spec.rb +0 -2740
  84. data/spec/rubocop/comment_config_spec.rb +0 -105
  85. data/spec/rubocop/config_loader_spec.rb +0 -377
  86. data/spec/rubocop/config_spec.rb +0 -235
  87. data/spec/rubocop/config_store_spec.rb +0 -53
  88. data/spec/rubocop/cop/commissioner_spec.rb +0 -85
  89. data/spec/rubocop/cop/cop_spec.rb +0 -135
  90. data/spec/rubocop/cop/corrector_spec.rb +0 -59
  91. data/spec/rubocop/cop/force_spec.rb +0 -29
  92. data/spec/rubocop/cop/lint/ambiguous_operator_spec.rb +0 -113
  93. data/spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb +0 -35
  94. data/spec/rubocop/cop/lint/assignment_in_condition_spec.rb +0 -130
  95. data/spec/rubocop/cop/lint/block_alignment_spec.rb +0 -533
  96. data/spec/rubocop/cop/lint/condition_position_spec.rb +0 -51
  97. data/spec/rubocop/cop/lint/debugger_spec.rb +0 -73
  98. data/spec/rubocop/cop/lint/def_end_alignment_spec.rb +0 -108
  99. data/spec/rubocop/cop/lint/deprecated_class_methods_spec.rb +0 -38
  100. data/spec/rubocop/cop/lint/duplicate_methods_spec.rb +0 -189
  101. data/spec/rubocop/cop/lint/else_layout_spec.rb +0 -65
  102. data/spec/rubocop/cop/lint/empty_ensure_spec.rb +0 -27
  103. data/spec/rubocop/cop/lint/empty_interpolation_spec.rb +0 -18
  104. data/spec/rubocop/cop/lint/end_alignment_spec.rb +0 -91
  105. data/spec/rubocop/cop/lint/end_in_method_spec.rb +0 -29
  106. data/spec/rubocop/cop/lint/ensure_return_spec.rb +0 -39
  107. data/spec/rubocop/cop/lint/eval_spec.rb +0 -33
  108. data/spec/rubocop/cop/lint/handle_exceptions_spec.rb +0 -30
  109. data/spec/rubocop/cop/lint/invalid_character_literal_spec.rb +0 -33
  110. data/spec/rubocop/cop/lint/literal_in_condition_spec.rb +0 -172
  111. data/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb +0 -31
  112. data/spec/rubocop/cop/lint/loop_spec.rb +0 -27
  113. data/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +0 -57
  114. data/spec/rubocop/cop/lint/require_parentheses_spec.rb +0 -82
  115. data/spec/rubocop/cop/lint/rescue_exception_spec.rb +0 -123
  116. data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +0 -237
  117. data/spec/rubocop/cop/lint/space_before_first_arg_spec.rb +0 -66
  118. data/spec/rubocop/cop/lint/string_conversion_in_interpolation_spec.rb +0 -51
  119. data/spec/rubocop/cop/lint/syntax_spec.rb +0 -34
  120. data/spec/rubocop/cop/lint/underscore_prefixed_variable_name_spec.rb +0 -179
  121. data/spec/rubocop/cop/lint/unreachable_code_spec.rb +0 -63
  122. data/spec/rubocop/cop/lint/unused_block_argument_spec.rb +0 -231
  123. data/spec/rubocop/cop/lint/unused_method_argument_spec.rb +0 -278
  124. data/spec/rubocop/cop/lint/useless_access_modifier_spec.rb +0 -192
  125. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +0 -1778
  126. data/spec/rubocop/cop/lint/useless_comparison_spec.rb +0 -30
  127. data/spec/rubocop/cop/lint/useless_else_without_rescue_spec.rb +0 -48
  128. data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +0 -211
  129. data/spec/rubocop/cop/lint/void_spec.rb +0 -64
  130. data/spec/rubocop/cop/metrics/abc_size_spec.rb +0 -109
  131. data/spec/rubocop/cop/metrics/block_nesting_spec.rb +0 -156
  132. data/spec/rubocop/cop/metrics/class_length_spec.rb +0 -131
  133. data/spec/rubocop/cop/metrics/cyclomatic_complexity_spec.rb +0 -204
  134. data/spec/rubocop/cop/metrics/line_length_spec.rb +0 -136
  135. data/spec/rubocop/cop/metrics/method_length_spec.rb +0 -147
  136. data/spec/rubocop/cop/metrics/parameter_lists_spec.rb +0 -44
  137. data/spec/rubocop/cop/metrics/perceived_complexity_spec.rb +0 -222
  138. data/spec/rubocop/cop/offense_spec.rb +0 -133
  139. data/spec/rubocop/cop/rails/action_filter_spec.rb +0 -64
  140. data/spec/rubocop/cop/rails/default_scope_spec.rb +0 -37
  141. data/spec/rubocop/cop/rails/delegate_spec.rb +0 -177
  142. data/spec/rubocop/cop/rails/has_and_belongs_to_many_spec.rb +0 -13
  143. data/spec/rubocop/cop/rails/output_spec.rb +0 -37
  144. data/spec/rubocop/cop/rails/read_write_attribute_spec.rb +0 -130
  145. data/spec/rubocop/cop/rails/scope_args_spec.rb +0 -25
  146. data/spec/rubocop/cop/rails/validation_spec.rb +0 -28
  147. data/spec/rubocop/cop/severity_spec.rb +0 -113
  148. data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +0 -421
  149. data/spec/rubocop/cop/style/accessor_method_name_spec.rb +0 -81
  150. data/spec/rubocop/cop/style/alias_spec.rb +0 -59
  151. data/spec/rubocop/cop/style/align_array_spec.rb +0 -123
  152. data/spec/rubocop/cop/style/align_hash_spec.rb +0 -411
  153. data/spec/rubocop/cop/style/align_parameters_spec.rb +0 -431
  154. data/spec/rubocop/cop/style/and_or_spec.rb +0 -235
  155. data/spec/rubocop/cop/style/array_join_spec.rb +0 -29
  156. data/spec/rubocop/cop/style/ascii_comments_spec.rb +0 -21
  157. data/spec/rubocop/cop/style/ascii_identifiers_spec.rb +0 -36
  158. data/spec/rubocop/cop/style/attr_spec.rb +0 -29
  159. data/spec/rubocop/cop/style/bare_percent_literals_spec.rb +0 -132
  160. data/spec/rubocop/cop/style/begin_block_spec.rb +0 -13
  161. data/spec/rubocop/cop/style/block_comments_spec.rb +0 -45
  162. data/spec/rubocop/cop/style/block_end_newline_spec.rb +0 -61
  163. data/spec/rubocop/cop/style/blocks_spec.rb +0 -121
  164. data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +0 -288
  165. data/spec/rubocop/cop/style/case_equality_spec.rb +0 -13
  166. data/spec/rubocop/cop/style/case_indentation_spec.rb +0 -293
  167. data/spec/rubocop/cop/style/character_literal_spec.rb +0 -37
  168. data/spec/rubocop/cop/style/class_and_module_camel_case_spec.rb +0 -40
  169. data/spec/rubocop/cop/style/class_and_module_children_spec.rb +0 -133
  170. data/spec/rubocop/cop/style/class_check_spec.rb +0 -41
  171. data/spec/rubocop/cop/style/class_methods_spec.rb +0 -80
  172. data/spec/rubocop/cop/style/class_vars_spec.rb +0 -19
  173. data/spec/rubocop/cop/style/collection_methods_spec.rb +0 -48
  174. data/spec/rubocop/cop/style/colon_method_call_spec.rb +0 -65
  175. data/spec/rubocop/cop/style/comment_annotation_spec.rb +0 -86
  176. data/spec/rubocop/cop/style/comment_indentation_spec.rb +0 -179
  177. data/spec/rubocop/cop/style/constant_name_spec.rb +0 -65
  178. data/spec/rubocop/cop/style/def_with_parentheses_spec.rb +0 -43
  179. data/spec/rubocop/cop/style/deprecated_hash_methods_spec.rb +0 -45
  180. data/spec/rubocop/cop/style/documentation_spec.rb +0 -146
  181. data/spec/rubocop/cop/style/dot_position_spec.rb +0 -132
  182. data/spec/rubocop/cop/style/double_negation_spec.rb +0 -22
  183. data/spec/rubocop/cop/style/each_with_object_spec.rb +0 -73
  184. data/spec/rubocop/cop/style/else_alignment_spec.rb +0 -506
  185. data/spec/rubocop/cop/style/empty_else_spec.rb +0 -100
  186. data/spec/rubocop/cop/style/empty_line_between_defs_spec.rb +0 -135
  187. data/spec/rubocop/cop/style/empty_lines_around_access_modifier_spec.rb +0 -178
  188. data/spec/rubocop/cop/style/empty_lines_around_block_body_spec.rb +0 -103
  189. data/spec/rubocop/cop/style/empty_lines_around_class_body_spec.rb +0 -135
  190. data/spec/rubocop/cop/style/empty_lines_around_method_body_spec.rb +0 -90
  191. data/spec/rubocop/cop/style/empty_lines_around_module_body_spec.rb +0 -79
  192. data/spec/rubocop/cop/style/empty_lines_spec.rb +0 -51
  193. data/spec/rubocop/cop/style/empty_literal_spec.rb +0 -117
  194. data/spec/rubocop/cop/style/encoding_spec.rb +0 -139
  195. data/spec/rubocop/cop/style/end_block_spec.rb +0 -13
  196. data/spec/rubocop/cop/style/end_of_line_spec.rb +0 -65
  197. data/spec/rubocop/cop/style/even_odd_spec.rb +0 -164
  198. data/spec/rubocop/cop/style/extra_spacing_spec.rb +0 -68
  199. data/spec/rubocop/cop/style/file_name_spec.rb +0 -84
  200. data/spec/rubocop/cop/style/first_parameter_indentation_spec.rb +0 -304
  201. data/spec/rubocop/cop/style/flip_flop_spec.rb +0 -23
  202. data/spec/rubocop/cop/style/for_spec.rb +0 -105
  203. data/spec/rubocop/cop/style/format_string_spec.rb +0 -184
  204. data/spec/rubocop/cop/style/global_vars_spec.rb +0 -34
  205. data/spec/rubocop/cop/style/guard_clause_spec.rb +0 -172
  206. data/spec/rubocop/cop/style/hash_syntax_spec.rb +0 -137
  207. data/spec/rubocop/cop/style/if_unless_modifier_spec.rb +0 -146
  208. data/spec/rubocop/cop/style/if_with_semicolon_spec.rb +0 -24
  209. data/spec/rubocop/cop/style/indent_array_spec.rb +0 -173
  210. data/spec/rubocop/cop/style/indent_hash_spec.rb +0 -367
  211. data/spec/rubocop/cop/style/indentation_consistency_spec.rb +0 -509
  212. data/spec/rubocop/cop/style/indentation_width_spec.rb +0 -885
  213. data/spec/rubocop/cop/style/infinite_loop_spec.rb +0 -48
  214. data/spec/rubocop/cop/style/inline_comment_spec.rb +0 -13
  215. data/spec/rubocop/cop/style/lambda_call_spec.rb +0 -65
  216. data/spec/rubocop/cop/style/lambda_spec.rb +0 -76
  217. data/spec/rubocop/cop/style/leading_comment_space_spec.rb +0 -64
  218. data/spec/rubocop/cop/style/line_end_concatenation_spec.rb +0 -184
  219. data/spec/rubocop/cop/style/method_call_parentheses_spec.rb +0 -59
  220. data/spec/rubocop/cop/style/method_called_on_do_end_block_spec.rb +0 -60
  221. data/spec/rubocop/cop/style/method_def_parentheses_spec.rb +0 -108
  222. data/spec/rubocop/cop/style/method_name_spec.rb +0 -125
  223. data/spec/rubocop/cop/style/module_function_spec.rb +0 -24
  224. data/spec/rubocop/cop/style/multiline_block_chain_spec.rb +0 -78
  225. data/spec/rubocop/cop/style/multiline_block_layout_spec.rb +0 -199
  226. data/spec/rubocop/cop/style/multiline_if_then_spec.rb +0 -134
  227. data/spec/rubocop/cop/style/multiline_operation_indentation_spec.rb +0 -464
  228. data/spec/rubocop/cop/style/multiline_ternary_operator_spec.rb +0 -18
  229. data/spec/rubocop/cop/style/negated_if_spec.rb +0 -102
  230. data/spec/rubocop/cop/style/negated_while_spec.rb +0 -64
  231. data/spec/rubocop/cop/style/nested_ternary_operator_spec.rb +0 -21
  232. data/spec/rubocop/cop/style/next_spec.rb +0 -301
  233. data/spec/rubocop/cop/style/nil_comparison_spec.rb +0 -29
  234. data/spec/rubocop/cop/style/non_nil_check_spec.rb +0 -132
  235. data/spec/rubocop/cop/style/not_spec.rb +0 -28
  236. data/spec/rubocop/cop/style/numeric_literals_spec.rb +0 -80
  237. data/spec/rubocop/cop/style/one_line_conditional_spec.rb +0 -13
  238. data/spec/rubocop/cop/style/op_method_spec.rb +0 -85
  239. data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +0 -161
  240. data/spec/rubocop/cop/style/percent_literal_delimiters_spec.rb +0 -316
  241. data/spec/rubocop/cop/style/percent_q_literals_spec.rb +0 -122
  242. data/spec/rubocop/cop/style/perl_backrefs_spec.rb +0 -22
  243. data/spec/rubocop/cop/style/predicate_name_spec.rb +0 -57
  244. data/spec/rubocop/cop/style/proc_spec.rb +0 -27
  245. data/spec/rubocop/cop/style/raise_args_spec.rb +0 -87
  246. data/spec/rubocop/cop/style/redundant_begin_spec.rb +0 -121
  247. data/spec/rubocop/cop/style/redundant_exception_spec.rb +0 -59
  248. data/spec/rubocop/cop/style/redundant_return_spec.rb +0 -171
  249. data/spec/rubocop/cop/style/redundant_self_spec.rb +0 -186
  250. data/spec/rubocop/cop/style/regexp_literal_spec.rb +0 -188
  251. data/spec/rubocop/cop/style/rescue_modifier_spec.rb +0 -116
  252. data/spec/rubocop/cop/style/self_assignment_spec.rb +0 -49
  253. data/spec/rubocop/cop/style/semicolon_spec.rb +0 -114
  254. data/spec/rubocop/cop/style/signal_exception_spec.rb +0 -289
  255. data/spec/rubocop/cop/style/single_line_block_params_spec.rb +0 -82
  256. data/spec/rubocop/cop/style/single_line_methods_spec.rb +0 -94
  257. data/spec/rubocop/cop/style/single_space_before_first_arg_spec.rb +0 -63
  258. data/spec/rubocop/cop/style/space_after_colon_spec.rb +0 -55
  259. data/spec/rubocop/cop/style/space_after_comma_spec.rb +0 -30
  260. data/spec/rubocop/cop/style/space_after_control_keyword_spec.rb +0 -84
  261. data/spec/rubocop/cop/style/space_after_method_name_spec.rb +0 -70
  262. data/spec/rubocop/cop/style/space_after_not_spec.rb +0 -22
  263. data/spec/rubocop/cop/style/space_after_semicolon_spec.rb +0 -23
  264. data/spec/rubocop/cop/style/space_around_block_parameters_spec.rb +0 -155
  265. data/spec/rubocop/cop/style/space_around_equals_in_parameter_default_spec.rb +0 -95
  266. data/spec/rubocop/cop/style/space_around_operators_spec.rb +0 -330
  267. data/spec/rubocop/cop/style/space_before_block_braces_spec.rb +0 -73
  268. data/spec/rubocop/cop/style/space_before_comma_spec.rb +0 -42
  269. data/spec/rubocop/cop/style/space_before_comment_spec.rb +0 -34
  270. data/spec/rubocop/cop/style/space_before_modifier_keyword_spec.rb +0 -70
  271. data/spec/rubocop/cop/style/space_before_semicolon_spec.rb +0 -28
  272. data/spec/rubocop/cop/style/space_inside_block_braces_spec.rb +0 -317
  273. data/spec/rubocop/cop/style/space_inside_brackets_spec.rb +0 -83
  274. data/spec/rubocop/cop/style/space_inside_hash_literal_braces_spec.rb +0 -147
  275. data/spec/rubocop/cop/style/space_inside_parens_spec.rb +0 -44
  276. data/spec/rubocop/cop/style/space_inside_range_literal_spec.rb +0 -64
  277. data/spec/rubocop/cop/style/special_global_vars_spec.rb +0 -62
  278. data/spec/rubocop/cop/style/string_literals_in_interpolation_spec.rb +0 -99
  279. data/spec/rubocop/cop/style/string_literals_spec.rb +0 -228
  280. data/spec/rubocop/cop/style/struct_inheritance_spec.rb +0 -44
  281. data/spec/rubocop/cop/style/symbol_array_spec.rb +0 -37
  282. data/spec/rubocop/cop/style/symbol_proc_spec.rb +0 -100
  283. data/spec/rubocop/cop/style/tab_spec.rb +0 -47
  284. data/spec/rubocop/cop/style/trailing_blank_lines_spec.rb +0 -83
  285. data/spec/rubocop/cop/style/trailing_comma_spec.rb +0 -390
  286. data/spec/rubocop/cop/style/trailing_whitespace_spec.rb +0 -31
  287. data/spec/rubocop/cop/style/trivial_accessors_spec.rb +0 -558
  288. data/spec/rubocop/cop/style/unless_else_spec.rb +0 -23
  289. data/spec/rubocop/cop/style/unneeded_capital_w_spec.rb +0 -90
  290. data/spec/rubocop/cop/style/unneeded_percent_q_spec.rb +0 -127
  291. data/spec/rubocop/cop/style/unneeded_percent_x_spec.rb +0 -34
  292. data/spec/rubocop/cop/style/variable_interpolation_spec.rb +0 -76
  293. data/spec/rubocop/cop/style/variable_name_spec.rb +0 -107
  294. data/spec/rubocop/cop/style/when_then_spec.rb +0 -42
  295. data/spec/rubocop/cop/style/while_until_do_spec.rb +0 -55
  296. data/spec/rubocop/cop/style/while_until_modifier_spec.rb +0 -93
  297. data/spec/rubocop/cop/style/word_array_spec.rb +0 -117
  298. data/spec/rubocop/cop/team_spec.rb +0 -190
  299. data/spec/rubocop/cop/util_spec.rb +0 -102
  300. data/spec/rubocop/cop/variable_force/assignment_spec.rb +0 -200
  301. data/spec/rubocop/cop/variable_force/locatable_spec.rb +0 -699
  302. data/spec/rubocop/cop/variable_force/reference_spec.rb +0 -19
  303. data/spec/rubocop/cop/variable_force/scope_spec.rb +0 -319
  304. data/spec/rubocop/cop/variable_force/variable_spec.rb +0 -59
  305. data/spec/rubocop/cop/variable_force/variable_table_spec.rb +0 -276
  306. data/spec/rubocop/cop/variable_force_spec.rb +0 -26
  307. data/spec/rubocop/formatter/base_formatter_spec.rb +0 -191
  308. data/spec/rubocop/formatter/clang_style_formatter_spec.rb +0 -114
  309. data/spec/rubocop/formatter/colorizable_spec.rb +0 -107
  310. data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +0 -50
  311. data/spec/rubocop/formatter/disabled_lines_formatter_spec.rb +0 -67
  312. data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +0 -63
  313. data/spec/rubocop/formatter/file_list_formatter_spec.rb +0 -33
  314. data/spec/rubocop/formatter/formatter_set_spec.rb +0 -141
  315. data/spec/rubocop/formatter/fuubar_style_formatter_spec.rb +0 -129
  316. data/spec/rubocop/formatter/html_formatter_spec.rb +0 -48
  317. data/spec/rubocop/formatter/json_formatter_spec.rb +0 -152
  318. data/spec/rubocop/formatter/offense_count_formatter_spec.rb +0 -75
  319. data/spec/rubocop/formatter/progress_formatter_spec.rb +0 -182
  320. data/spec/rubocop/formatter/simple_text_formatter_spec.rb +0 -135
  321. data/spec/rubocop/formatter/text_util_spec.rb +0 -55
  322. data/spec/rubocop/options_spec.rb +0 -181
  323. data/spec/rubocop/path_util_spec.rb +0 -77
  324. data/spec/rubocop/processed_source_spec.rb +0 -184
  325. data/spec/rubocop/rake_task_spec.rb +0 -122
  326. data/spec/rubocop/runner_spec.rb +0 -99
  327. data/spec/rubocop/string_util_spec.rb +0 -46
  328. data/spec/rubocop/target_finder_spec.rb +0 -245
  329. data/spec/rubocop/token_spec.rb +0 -29
  330. data/spec/spec_helper.rb +0 -39
  331. data/spec/support/cop_helper.rb +0 -75
  332. data/spec/support/cops/class_must_be_a_module_cop.rb +0 -19
  333. data/spec/support/cops/module_must_be_a_class_cop.rb +0 -19
  334. data/spec/support/coverage.rb +0 -15
  335. data/spec/support/custom_matchers.rb +0 -58
  336. data/spec/support/file_helper.rb +0 -23
  337. data/spec/support/jruby_workaround.rb +0 -15
  338. data/spec/support/mri_syntax_checker.rb +0 -73
  339. data/spec/support/shared_contexts.rb +0 -53
  340. data/spec/support/shared_examples.rb +0 -55
  341. data/spec/support/statement_modifier_helper.rb +0 -41
@@ -1,1778 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
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 offense' do
24
- inspect_source(cop, source)
25
- expect(cop.offenses.size).to eq(1)
26
- expect(cop.offenses.first.message)
27
- .to eq('Useless assignment to variable - `foo`.')
28
- expect(cop.offenses.first.line).to eq(5)
29
- expect(cop.highlights).to eq(['foo'])
30
- end
31
-
32
- include_examples 'mimics MRI 2.1'
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 offense' do
52
- inspect_source(cop, source)
53
- expect(cop.offenses.size).to eq(1)
54
- expect(cop.offenses.first.message)
55
- .to eq('Useless assignment to variable - `foo`.')
56
- expect(cop.offenses.first.line).to eq(5)
57
- expect(cop.highlights).to eq(['foo'])
58
- end
59
-
60
- include_examples 'mimics MRI 2.1'
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 offense' do
81
- inspect_source(cop, source)
82
- expect(cop.offenses.size).to eq(1)
83
- expect(cop.offenses.first.message)
84
- .to eq('Useless assignment to variable - `foo`.')
85
- expect(cop.offenses.first.line).to eq(6)
86
- expect(cop.highlights).to eq(['foo'])
87
- end
88
-
89
- include_examples 'mimics MRI 2.1'
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 offense' do
108
- inspect_source(cop, source)
109
- expect(cop.offenses.size).to eq(1)
110
- expect(cop.offenses.first.message)
111
- .to eq('Useless assignment to variable - `foo`.')
112
- expect(cop.offenses.first.line).to eq(5)
113
- expect(cop.highlights).to eq(['foo'])
114
- end
115
-
116
- include_examples 'mimics MRI 2.1'
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 offense' do
137
- inspect_source(cop, source)
138
- expect(cop.offenses.size).to eq(1)
139
- expect(cop.offenses.first.message)
140
- .to eq('Useless assignment to variable - `foo`.')
141
- expect(cop.offenses.first.line).to eq(6)
142
- expect(cop.highlights).to eq(['foo'])
143
- end
144
-
145
- include_examples 'mimics MRI 2.1'
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 offense' do
166
- inspect_source(cop, source)
167
- expect(cop.offenses.size).to eq(1)
168
- expect(cop.offenses.first.message)
169
- .to eq('Useless assignment to variable - `foo`.')
170
- expect(cop.offenses.first.line).to eq(6)
171
- expect(cop.highlights).to eq(['foo'])
172
- end
173
-
174
- include_examples 'mimics MRI 2.1'
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 offense' do
193
- inspect_source(cop, source)
194
- expect(cop.offenses.size).to eq(1)
195
- expect(cop.offenses.first.message)
196
- .to eq('Useless assignment to variable - `foo`.')
197
- expect(cop.offenses.first.line).to eq(5)
198
- expect(cop.highlights).to eq(['foo'])
199
- end
200
-
201
- include_examples 'mimics MRI 2.1'
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 offense' do
214
- inspect_source(cop, source)
215
- expect(cop.offenses.size).to eq(1)
216
- expect(cop.offenses.first.message)
217
- .to eq('Useless assignment to variable - `foo`.')
218
- expect(cop.offenses.first.line).to eq(1)
219
- expect(cop.highlights).to eq(['foo'])
220
- end
221
-
222
- include_examples 'mimics MRI 2.1'
223
- end
224
-
225
- context 'when a variable is assigned with operator assignment ' \
226
- 'in top level' do
227
- let(:source) do
228
- 'foo ||= 1'
229
- end
230
-
231
- it 'registers an offense' do
232
- inspect_source(cop, source)
233
- expect(cop.offenses.size).to eq(1)
234
- expect(cop.offenses.first.message).to eq(
235
- 'Useless assignment to variable - `foo`. Use just operator `||`.'
236
- )
237
- expect(cop.offenses.first.line).to eq(1)
238
- expect(cop.highlights).to eq(['foo'])
239
- end
240
- end
241
-
242
- context 'when a variable is assigned multiple times ' \
243
- 'but unreferenced' do
244
- let(:source) do
245
- [
246
- 'def some_method',
247
- ' foo = 1',
248
- ' bar = 2',
249
- ' foo = 3',
250
- ' puts bar',
251
- 'end'
252
- ]
253
- end
254
-
255
- it 'registers offenses for each assignment' do
256
- inspect_source(cop, source)
257
- expect(cop.offenses.size).to eq(2)
258
-
259
- expect(cop.offenses[0].message)
260
- .to eq('Useless assignment to variable - `foo`.')
261
- expect(cop.offenses[0].line).to eq(2)
262
-
263
- expect(cop.offenses[1].message)
264
- .to eq('Useless assignment to variable - `foo`.')
265
- expect(cop.offenses[1].line).to eq(4)
266
-
267
- expect(cop.highlights).to eq(%w(foo foo))
268
- end
269
- end
270
-
271
- context 'when a referenced variable is reassigned ' \
272
- 'but not re-referenced' do
273
- let(:source) do
274
- [
275
- 'def some_method',
276
- ' foo = 1',
277
- ' puts foo',
278
- ' foo = 3',
279
- 'end'
280
- ]
281
- end
282
-
283
- it 'registers an offense for the non-re-referenced assignment' do
284
- inspect_source(cop, source)
285
- expect(cop.offenses.size).to eq(1)
286
- expect(cop.offenses.first.message)
287
- .to eq('Useless assignment to variable - `foo`.')
288
- expect(cop.offenses.first.line).to eq(4)
289
- expect(cop.highlights).to eq(['foo'])
290
- end
291
- end
292
-
293
- context 'when an unreferenced variable is reassigned ' \
294
- 'and re-referenced' do
295
- let(:source) do
296
- [
297
- 'def some_method',
298
- ' foo = 1',
299
- ' foo = 3',
300
- ' puts foo',
301
- 'end'
302
- ]
303
- end
304
-
305
- it 'registers an offense for the unreferenced assignment' do
306
- inspect_source(cop, source)
307
- expect(cop.offenses.size).to eq(1)
308
- expect(cop.offenses.first.message)
309
- .to eq('Useless assignment to variable - `foo`.')
310
- expect(cop.offenses.first.line).to eq(2)
311
- expect(cop.highlights).to eq(['foo'])
312
- end
313
- end
314
-
315
- context 'when an unreferenced variable is reassigned in a block' do
316
- let(:source) do
317
- [
318
- 'def const_name(node)',
319
- ' const_names = []',
320
- ' const_node = node',
321
- '',
322
- ' loop do',
323
- ' namespace_node, name = *const_node',
324
- ' const_names << name',
325
- ' break unless namespace_node',
326
- ' break if namespace_node.type == :cbase',
327
- ' const_node = namespace_node',
328
- ' end',
329
- '',
330
- " const_names.reverse.join('::')",
331
- 'end'
332
- ]
333
- end
334
-
335
- include_examples 'accepts'
336
- include_examples 'mimics MRI 2.1'
337
- end
338
-
339
- context 'when a referenced variable is reassigned in a block' do
340
- let(:source) do
341
- [
342
- 'def some_method',
343
- ' foo = 1',
344
- ' puts foo',
345
- ' 1.times do',
346
- ' foo = 2',
347
- ' end',
348
- 'end'
349
- ]
350
- end
351
-
352
- include_examples 'accepts'
353
- include_examples 'mimics MRI 2.1'
354
- end
355
-
356
- context 'when a block local variable is declared but not assigned' do
357
- let(:source) do
358
- [
359
- '1.times do |i; foo|',
360
- 'end'
361
- ]
362
- end
363
-
364
- include_examples 'accepts'
365
- end
366
-
367
- context 'when a block local variable is assigned and unreferenced' do
368
- let(:source) do
369
- [
370
- '1.times do |i; foo|',
371
- ' foo = 2',
372
- 'end'
373
- ]
374
- end
375
-
376
- it 'registers offenses for the assignment' do
377
- inspect_source(cop, source)
378
- expect(cop.offenses.size).to eq(1)
379
- expect(cop.offenses.first.message)
380
- .to eq('Useless assignment to variable - `foo`.')
381
- expect(cop.offenses.first.line).to eq(2)
382
- expect(cop.highlights).to eq(['foo'])
383
- end
384
- end
385
-
386
- context 'when a variable is assigned in loop body and unreferenced' do
387
- let(:source) do
388
- [
389
- 'def some_method',
390
- ' while true',
391
- ' foo = 1',
392
- ' end',
393
- 'end'
394
- ]
395
- end
396
-
397
- it 'registers an offense' do
398
- inspect_source(cop, source)
399
- expect(cop.offenses.size).to eq(1)
400
- expect(cop.offenses.first.message)
401
- .to eq('Useless assignment to variable - `foo`.')
402
- expect(cop.offenses.first.line).to eq(3)
403
- expect(cop.highlights).to eq(['foo'])
404
- end
405
-
406
- include_examples 'mimics MRI 2.1'
407
- end
408
-
409
- context 'when a variable is reassigned at the end of loop body ' \
410
- 'and would be referenced in next iteration' do
411
- let(:source) do
412
- [
413
- 'def some_method',
414
- ' total = 0',
415
- ' foo = 0',
416
- '',
417
- ' while total < 100',
418
- ' total += foo',
419
- ' foo += 1',
420
- ' end',
421
- '',
422
- ' total',
423
- 'end'
424
- ]
425
- end
426
-
427
- include_examples 'accepts'
428
- include_examples 'mimics MRI 2.1'
429
- end
430
-
431
- context 'when a variable is reassigned at the end of loop body ' \
432
- 'and would be referenced in loop condition' do
433
- let(:source) do
434
- [
435
- 'def some_method',
436
- ' total = 0',
437
- ' foo = 0',
438
- '',
439
- ' while foo < 100',
440
- ' total += 1',
441
- ' foo += 1',
442
- ' end',
443
- '',
444
- ' total',
445
- 'end'
446
- ]
447
- end
448
-
449
- include_examples 'accepts'
450
- include_examples 'mimics MRI 2.1'
451
- end
452
-
453
- context 'when a setter is invoked with operator assignment in loop body' do
454
- let(:source) do
455
- [
456
- 'def some_method',
457
- ' obj = {}',
458
- '',
459
- ' while obj[:count] < 100',
460
- ' obj[:count] += 1',
461
- ' end',
462
- 'end'
463
- ]
464
- end
465
-
466
- include_examples 'accepts'
467
- include_examples 'mimics MRI 2.1'
468
- end
469
-
470
- context "when a variable is reassigned in loop body but won't " \
471
- 'be referenced either next iteration or loop condition' do
472
- let(:source) do
473
- [
474
- 'def some_method',
475
- ' total = 0',
476
- ' foo = 0',
477
- '',
478
- ' while total < 100',
479
- ' total += 1',
480
- ' foo += 1',
481
- ' end',
482
- '',
483
- ' total',
484
- 'end'
485
- ]
486
- end
487
-
488
- it 'registers an offense' do
489
- pending 'Requires an advanced logic that checks whether the return ' \
490
- 'value of an operator assignment is used or not.'
491
- inspect_source(cop, source)
492
- expect(cop.offenses.size).to eq(1)
493
- expect(cop.offenses.first.message)
494
- .to eq('Useless assignment to variable - `foo`.')
495
- expect(cop.offenses.first.line).to eq(7)
496
- expect(cop.highlights).to eq(['foo'])
497
- end
498
- end
499
-
500
- context 'when a referenced variable is reassigned ' \
501
- 'but not re-referenced in a method defined in loop' do
502
- let(:source) do
503
- [
504
- 'while true',
505
- ' def some_method',
506
- ' foo = 1',
507
- ' puts foo',
508
- ' foo = 3',
509
- ' end',
510
- 'end'
511
- ]
512
- end
513
-
514
- it 'registers an offense' do
515
- inspect_source(cop, source)
516
- expect(cop.offenses.size).to eq(1)
517
- expect(cop.offenses.first.message)
518
- .to eq('Useless assignment to variable - `foo`.')
519
- expect(cop.offenses.first.line).to eq(5)
520
- expect(cop.highlights).to eq(['foo'])
521
- end
522
- end
523
-
524
- context 'when a variable that has same name as outer scope variable ' \
525
- 'is not referenced in a method defined in loop' do
526
- let(:source) do
527
- [
528
- 'foo = 1',
529
- '',
530
- 'while foo < 100',
531
- ' foo += 1',
532
- ' def some_method',
533
- ' foo = 1',
534
- ' end',
535
- 'end'
536
- ]
537
- end
538
-
539
- it 'registers an offense' do
540
- inspect_source(cop, source)
541
- expect(cop.offenses.size).to eq(1)
542
- expect(cop.offenses.first.message)
543
- .to eq('Useless assignment to variable - `foo`.')
544
- expect(cop.offenses.first.line).to eq(6)
545
- expect(cop.highlights).to eq(['foo'])
546
- end
547
- end
548
-
549
- context 'when a variable is assigned in single branch if ' \
550
- 'and unreferenced' do
551
- let(:source) do
552
- [
553
- 'def some_method(flag)',
554
- ' if flag',
555
- ' foo = 1',
556
- ' end',
557
- 'end'
558
- ]
559
- end
560
-
561
- it 'registers an offense' do
562
- inspect_source(cop, source)
563
- expect(cop.offenses.size).to eq(1)
564
- expect(cop.offenses.first.message)
565
- .to eq('Useless assignment to variable - `foo`.')
566
- expect(cop.offenses.first.line).to eq(3)
567
- expect(cop.highlights).to eq(['foo'])
568
- end
569
-
570
- include_examples 'mimics MRI 2.1'
571
- end
572
-
573
- context 'when a unreferenced variable is reassigned in same branch ' \
574
- 'and referenced after the branching' do
575
- let(:source) do
576
- [
577
- 'def some_method(flag)',
578
- ' if flag',
579
- ' foo = 1',
580
- ' foo = 2',
581
- ' end',
582
- '',
583
- ' foo',
584
- 'end'
585
- ]
586
- end
587
-
588
- it 'registers an offense for the unreferenced assignment' do
589
- inspect_source(cop, source)
590
- expect(cop.offenses.size).to eq(1)
591
- expect(cop.offenses.first.message)
592
- .to eq('Useless assignment to variable - `foo`.')
593
- expect(cop.offenses.first.line).to eq(3)
594
- expect(cop.highlights).to eq(['foo'])
595
- end
596
- end
597
-
598
- context 'when a variable is reassigned in single branch if ' \
599
- 'and referenced after the branching' do
600
- let(:source) do
601
- [
602
- 'def some_method(flag)',
603
- ' foo = 1',
604
- '',
605
- ' if flag',
606
- ' foo = 2',
607
- ' end',
608
- '',
609
- ' foo',
610
- 'end'
611
- ]
612
- end
613
-
614
- include_examples 'accepts'
615
- include_examples 'mimics MRI 2.1'
616
- end
617
-
618
- context 'when a variable is assigned in each branch of if ' \
619
- 'and referenced after the branching' do
620
- let(:source) do
621
- [
622
- 'def some_method(flag)',
623
- ' if flag',
624
- ' foo = 2',
625
- ' else',
626
- ' foo = 3',
627
- ' end',
628
- '',
629
- ' foo',
630
- 'end'
631
- ]
632
- end
633
-
634
- include_examples 'accepts'
635
- include_examples 'mimics MRI 2.1'
636
- end
637
-
638
- context 'when a variable is reassigned in single branch if ' \
639
- 'and referenced in the branch' do
640
- let(:source) do
641
- [
642
- 'def some_method(flag)',
643
- ' foo = 1',
644
- '',
645
- ' if flag',
646
- ' foo = 2',
647
- ' puts foo',
648
- ' end',
649
- 'end'
650
- ]
651
- end
652
-
653
- it 'registers an offense for the unreferenced assignment' do
654
- inspect_source(cop, source)
655
- expect(cop.offenses.size).to eq(1)
656
- expect(cop.offenses.first.message)
657
- .to eq('Useless assignment to variable - `foo`.')
658
- expect(cop.offenses.first.line).to eq(2)
659
- expect(cop.highlights).to eq(['foo'])
660
- end
661
- end
662
-
663
- context 'when a variable is assigned in each branch of if ' \
664
- 'and referenced in the else branch' do
665
- let(:source) do
666
- [
667
- 'def some_method(flag)',
668
- ' if flag',
669
- ' foo = 2',
670
- ' else',
671
- ' foo = 3',
672
- ' puts foo',
673
- ' end',
674
- 'end'
675
- ]
676
- end
677
-
678
- it 'registers an offense for the assignment in the if branch' do
679
- inspect_source(cop, source)
680
- expect(cop.offenses.size).to eq(1)
681
- expect(cop.offenses.first.message)
682
- .to eq('Useless assignment to variable - `foo`.')
683
- expect(cop.offenses.first.line).to eq(3)
684
- expect(cop.highlights).to eq(['foo'])
685
- end
686
- end
687
-
688
- context 'when a variable is reassigned and unreferenced in a if branch ' \
689
- 'while the variable is referenced in the paired else branch ' do
690
- let(:source) do
691
- [
692
- 'def some_method(flag)',
693
- ' foo = 1',
694
- '',
695
- ' if flag',
696
- ' puts foo',
697
- ' foo = 2',
698
- ' else',
699
- ' puts foo',
700
- ' end',
701
- 'end'
702
- ]
703
- end
704
-
705
- it 'registers an offense for the reassignment in the if branch' do
706
- inspect_source(cop, source)
707
- expect(cop.offenses.size).to eq(1)
708
- expect(cop.offenses.first.message)
709
- .to eq('Useless assignment to variable - `foo`.')
710
- expect(cop.offenses.first.line).to eq(6)
711
- expect(cop.highlights).to eq(['foo'])
712
- end
713
- end
714
-
715
- context 'when a variable is assigned in branch of modifier if ' \
716
- 'that references the variable in its conditional clause' \
717
- 'and referenced after the branching' do
718
- let(:source) do
719
- [
720
- 'def some_method(flag)',
721
- ' foo = 1 unless foo',
722
- ' puts foo',
723
- 'end'
724
- ]
725
- end
726
-
727
- include_examples 'accepts'
728
- include_examples 'mimics MRI 2.1'
729
- end
730
-
731
- context 'when a variable is assigned in branch of modifier if ' \
732
- 'that references the variable in its conditional clause' \
733
- 'and unreferenced' do
734
- let(:source) do
735
- [
736
- 'def some_method(flag)',
737
- ' foo = 1 unless foo',
738
- 'end'
739
- ]
740
- end
741
-
742
- it 'registers an offense' do
743
- inspect_source(cop, source)
744
- expect(cop.offenses.size).to eq(1)
745
- expect(cop.offenses.first.message)
746
- .to eq('Useless assignment to variable - `foo`.')
747
- expect(cop.offenses.first.line).to eq(2)
748
- expect(cop.highlights).to eq(['foo'])
749
- end
750
- end
751
-
752
- context 'when a variable is assigned on each side of && ' \
753
- 'and referenced after the &&' do
754
- let(:source) do
755
- [
756
- 'def some_method',
757
- ' (foo = do_something_returns_object_or_nil) && (foo = 1)',
758
- ' foo',
759
- 'end'
760
- ]
761
- end
762
-
763
- include_examples 'accepts'
764
- include_examples 'mimics MRI 2.1'
765
- end
766
-
767
- context 'when a unreferenced variable is reassigned ' \
768
- 'on the left side of && and referenced after the &&' do
769
- let(:source) do
770
- [
771
- 'def some_method',
772
- ' foo = 1',
773
- ' (foo = do_something_returns_object_or_nil) && do_something',
774
- ' foo',
775
- 'end'
776
- ]
777
- end
778
-
779
- it 'registers an offense for the unreferenced assignment' do
780
- inspect_source(cop, source)
781
- expect(cop.offenses.size).to eq(1)
782
- expect(cop.offenses.first.message)
783
- .to eq('Useless assignment to variable - `foo`.')
784
- expect(cop.offenses.first.line).to eq(2)
785
- expect(cop.highlights).to eq(['foo'])
786
- end
787
- end
788
-
789
- context 'when a unreferenced variable is reassigned ' \
790
- 'on the right side of && and referenced after the &&' do
791
- let(:source) do
792
- [
793
- 'def some_method',
794
- ' foo = 1',
795
- ' do_something_returns_object_or_nil && foo = 2',
796
- ' foo',
797
- 'end'
798
- ]
799
- end
800
-
801
- include_examples 'accepts'
802
- include_examples 'mimics MRI 2.1'
803
- end
804
-
805
- context 'when a variable is reassigned ' \
806
- 'while referencing itself in rhs and referenced' do
807
- let(:source) do
808
- [
809
- 'def some_method',
810
- ' foo = [1, 2]',
811
- ' foo = foo.map { |i| i + 1 }',
812
- ' puts foo',
813
- 'end'
814
- ]
815
- end
816
-
817
- include_examples 'accepts'
818
- include_examples 'mimics MRI 2.1'
819
- end
820
-
821
- context 'when a variable is reassigned ' \
822
- 'with binary operator assignment and referenced' do
823
- let(:source) do
824
- [
825
- 'def some_method',
826
- ' foo = 1',
827
- ' foo += 1',
828
- ' foo',
829
- 'end'
830
- ]
831
- end
832
-
833
- include_examples 'accepts'
834
- include_examples 'mimics MRI 2.1'
835
- end
836
-
837
- context 'when a variable is reassigned ' \
838
- 'with logical operator assignment and referenced' do
839
- let(:source) do
840
- [
841
- 'def some_method',
842
- ' foo = do_something_returns_object_or_nil',
843
- ' foo ||= 1',
844
- ' foo',
845
- 'end'
846
- ]
847
- end
848
-
849
- include_examples 'accepts'
850
- include_examples 'mimics MRI 2.1'
851
- end
852
-
853
- context 'when a variable is reassigned with binary operator ' \
854
- 'assignment while assigning to itself in rhs ' \
855
- 'then referenced' do
856
- let(:source) do
857
- [
858
- 'def some_method',
859
- ' foo = 1',
860
- ' foo += foo = 2',
861
- ' foo',
862
- 'end'
863
- ]
864
- end
865
-
866
- it 'registers an offense for the assignment in rhs' do
867
- inspect_source(cop, source)
868
- expect(cop.offenses.size).to eq(1)
869
- expect(cop.offenses.first.message)
870
- .to eq('Useless assignment to variable - `foo`.')
871
- expect(cop.offenses.first.line).to eq(3)
872
- expect(cop.highlights).to eq(['foo'])
873
- end
874
- end
875
-
876
- context 'when a variable is assigned first with ||= and referenced' do
877
- let(:source) do
878
- [
879
- 'def some_method',
880
- ' foo ||= 1',
881
- ' foo',
882
- 'end'
883
- ]
884
- end
885
-
886
- include_examples 'accepts'
887
- include_examples 'mimics MRI 2.1'
888
- end
889
-
890
- context 'when a variable is assigned with ||= ' \
891
- 'at the last expression of the scope' do
892
- let(:source) do
893
- [
894
- 'def some_method',
895
- ' foo = do_something_returns_object_or_nil',
896
- ' foo ||= 1',
897
- 'end'
898
- ]
899
- end
900
-
901
- it 'registers an offense' do
902
- inspect_source(cop, source)
903
- expect(cop.offenses.size).to eq(1)
904
- expect(cop.offenses.first.message).to eq(
905
- 'Useless assignment to variable - `foo`. Use just operator `||`.'
906
- )
907
- expect(cop.offenses.first.line).to eq(3)
908
- expect(cop.highlights).to eq(['foo'])
909
- end
910
- end
911
-
912
- context 'when a variable is assigned with ||= ' \
913
- 'before the last expression of the scope' do
914
- let(:source) do
915
- [
916
- 'def some_method',
917
- ' foo = do_something_returns_object_or_nil',
918
- ' foo ||= 1',
919
- ' some_return_value',
920
- 'end'
921
- ]
922
- end
923
-
924
- it 'registers an offense' do
925
- inspect_source(cop, source)
926
- expect(cop.offenses.size).to eq(1)
927
- expect(cop.offenses.first.message)
928
- .to eq('Useless assignment to variable - `foo`.')
929
- expect(cop.offenses.first.line).to eq(3)
930
- expect(cop.highlights).to eq(['foo'])
931
- end
932
- end
933
-
934
- context 'when a variable is assigned with multiple assignment ' \
935
- 'and unreferenced' do
936
- let(:source) do
937
- [
938
- 'def some_method',
939
- ' foo, bar = do_something',
940
- ' puts foo',
941
- 'end'
942
- ]
943
- end
944
-
945
- it 'registers an offense' do
946
- inspect_source(cop, source)
947
- expect(cop.offenses.size).to eq(1)
948
- expect(cop.offenses.first.message).to eq(
949
- 'Useless assignment to variable - `bar`. ' \
950
- 'Use `_` or `_bar` as a variable name ' \
951
- "to indicate that it won't be used."
952
- )
953
- expect(cop.offenses.first.line).to eq(2)
954
- expect(cop.highlights).to eq(['bar'])
955
- end
956
-
957
- include_examples 'mimics MRI 2.1'
958
- end
959
-
960
- context 'when a variable is reassigned with multiple assignment ' \
961
- 'while referencing itself in rhs and referenced' do
962
- let(:source) do
963
- [
964
- 'def some_method',
965
- ' foo = 1',
966
- ' foo, bar = do_something(foo)',
967
- ' puts foo, bar',
968
- 'end'
969
- ]
970
- end
971
-
972
- include_examples 'accepts'
973
- include_examples 'mimics MRI 2.1'
974
- end
975
-
976
- context 'when a variable is assigned in loop body ' \
977
- 'and referenced in post while condition' do
978
- let(:source) do
979
- [
980
- 'begin',
981
- ' a = (a || 0) + 1',
982
- ' puts a',
983
- 'end while a <= 2'
984
- ]
985
- end
986
-
987
- include_examples 'accepts'
988
- include_examples 'mimics MRI 2.1'
989
- end
990
-
991
- context 'when a variable is assigned in loop body ' \
992
- 'and referenced in post until condition' do
993
- let(:source) do
994
- [
995
- 'begin',
996
- ' a = (a || 0) + 1',
997
- ' puts a',
998
- 'end until a > 2'
999
- ]
1000
- end
1001
-
1002
- include_examples 'accepts'
1003
- include_examples 'mimics MRI 2.1'
1004
- end
1005
-
1006
- context 'when a variable is assigned ' \
1007
- 'in main body of begin with rescue but unreferenced' do
1008
- let(:source) do
1009
- [
1010
- 'begin',
1011
- ' do_something',
1012
- ' foo = true',
1013
- 'rescue',
1014
- ' do_anything',
1015
- 'end'
1016
- ]
1017
- end
1018
-
1019
- it 'registers an offense' do
1020
- inspect_source(cop, source)
1021
- expect(cop.offenses.size).to eq(1)
1022
- expect(cop.offenses.first.message)
1023
- .to eq('Useless assignment to variable - `foo`.')
1024
- expect(cop.offenses.first.line).to eq(3)
1025
- expect(cop.highlights).to eq(['foo'])
1026
- end
1027
-
1028
- include_examples 'mimics MRI 2.1'
1029
- end
1030
-
1031
- context 'when a variable is assigned in main body of begin, rescue ' \
1032
- 'and else then referenced after the begin' do
1033
- let(:source) do
1034
- [
1035
- 'begin',
1036
- ' do_something',
1037
- ' foo = :in_begin',
1038
- 'rescue FirstError',
1039
- ' foo = :in_first_rescue',
1040
- 'rescue SecondError',
1041
- ' foo = :in_second_rescue',
1042
- 'else',
1043
- ' foo = :in_else',
1044
- 'end',
1045
- '',
1046
- 'puts foo'
1047
- ]
1048
- end
1049
-
1050
- include_examples 'accepts'
1051
- include_examples 'mimics MRI 2.1'
1052
- end
1053
-
1054
- context 'when a variable is reassigned multiple times ' \
1055
- 'in main body of begin then referenced after the begin' do
1056
- let(:source) do
1057
- [
1058
- 'begin',
1059
- ' status = :initial',
1060
- ' connect_sometimes_fails!',
1061
- ' status = :connected',
1062
- ' fetch_sometimes_fails!',
1063
- ' status = :fetched',
1064
- 'rescue',
1065
- ' do_something',
1066
- 'end',
1067
- '',
1068
- 'puts status'
1069
- ]
1070
- end
1071
-
1072
- include_examples 'accepts'
1073
- include_examples 'mimics MRI 2.1'
1074
- end
1075
-
1076
- context 'when a variable is reassigned multiple times ' \
1077
- 'in main body of begin then referenced in rescue' do
1078
- let(:source) do
1079
- [
1080
- 'begin',
1081
- ' status = :initial',
1082
- ' connect_sometimes_fails!',
1083
- ' status = :connected',
1084
- ' fetch_sometimes_fails!',
1085
- ' status = :fetched',
1086
- 'rescue',
1087
- ' puts status',
1088
- 'end'
1089
- ]
1090
- end
1091
-
1092
- include_examples 'accepts'
1093
- include_examples 'mimics MRI 2.1'
1094
- end
1095
-
1096
- context 'when a variable is reassigned multiple times ' \
1097
- 'in main body of begin then referenced in ensure' do
1098
- let(:source) do
1099
- [
1100
- 'begin',
1101
- ' status = :initial',
1102
- ' connect_sometimes_fails!',
1103
- ' status = :connected',
1104
- ' fetch_sometimes_fails!',
1105
- ' status = :fetched',
1106
- 'ensure',
1107
- ' puts status',
1108
- 'end'
1109
- ]
1110
- end
1111
-
1112
- include_examples 'accepts'
1113
- include_examples 'mimics MRI 2.1'
1114
- end
1115
-
1116
- context 'when a variable is reassigned multiple times in rescue ' \
1117
- 'and referenced after the begin' do
1118
- let(:source) do
1119
- [
1120
- 'foo = false',
1121
- '',
1122
- 'begin',
1123
- ' do_something',
1124
- 'rescue',
1125
- ' foo = true',
1126
- ' foo = true',
1127
- 'end',
1128
- '',
1129
- 'puts foo'
1130
- ]
1131
- end
1132
-
1133
- it 'registers an offense' do
1134
- inspect_source(cop, source)
1135
- expect(cop.offenses.size).to eq(1)
1136
- expect(cop.offenses.first.message)
1137
- .to eq('Useless assignment to variable - `foo`.')
1138
- expect(cop.offenses.first.line).to eq(6)
1139
- expect(cop.highlights).to eq(['foo'])
1140
- end
1141
- end
1142
-
1143
- context 'when a variable is reassigned multiple times ' \
1144
- 'in rescue with ensure then referenced after the begin' do
1145
- let(:source) do
1146
- [
1147
- 'foo = false',
1148
- '',
1149
- 'begin',
1150
- ' do_something',
1151
- 'rescue',
1152
- ' foo = true',
1153
- ' foo = true',
1154
- 'ensure',
1155
- ' do_anything',
1156
- 'end',
1157
- '',
1158
- 'puts foo'
1159
- ]
1160
- end
1161
-
1162
- it 'registers an offense' do
1163
- inspect_source(cop, source)
1164
- expect(cop.offenses.size).to eq(1)
1165
- expect(cop.offenses.first.message)
1166
- .to eq('Useless assignment to variable - `foo`.')
1167
- expect(cop.offenses.first.line).to eq(6)
1168
- expect(cop.highlights).to eq(['foo'])
1169
- end
1170
- end
1171
-
1172
- context 'when a variable is reassigned multiple times ' \
1173
- 'in ensure with rescue then referenced after the begin' do
1174
- let(:source) do
1175
- [
1176
- 'begin',
1177
- ' do_something',
1178
- 'rescue',
1179
- ' do_anything',
1180
- 'ensure',
1181
- ' foo = true',
1182
- ' foo = true',
1183
- 'end',
1184
- '',
1185
- 'puts foo'
1186
- ]
1187
- end
1188
-
1189
- it 'registers an offense' do
1190
- inspect_source(cop, source)
1191
- expect(cop.offenses.size).to eq(1)
1192
- expect(cop.offenses.first.message)
1193
- .to eq('Useless assignment to variable - `foo`.')
1194
- expect(cop.offenses.first.line).to eq(6)
1195
- expect(cop.highlights).to eq(['foo'])
1196
- end
1197
- end
1198
-
1199
- context 'when a variable is assigned at the end of rescue ' \
1200
- 'and would be referenced with retry' do
1201
- let(:source) do
1202
- [
1203
- 'retried = false',
1204
- '',
1205
- 'begin',
1206
- ' do_something',
1207
- 'rescue',
1208
- ' fail if retried',
1209
- ' retried = true',
1210
- ' retry',
1211
- 'end'
1212
- ]
1213
- end
1214
-
1215
- include_examples 'accepts'
1216
- include_examples 'mimics MRI 2.1'
1217
- end
1218
-
1219
- context 'when a variable is assigned with operator assignment ' \
1220
- 'in rescue and would be referenced with retry' do
1221
- let(:source) do
1222
- [
1223
- 'retry_count = 0',
1224
- '',
1225
- 'begin',
1226
- ' do_something',
1227
- 'rescue',
1228
- ' fail if (retry_count += 1) > 3',
1229
- ' retry',
1230
- 'end'
1231
- ]
1232
- end
1233
-
1234
- include_examples 'accepts'
1235
- include_examples 'mimics MRI 2.1'
1236
- end
1237
-
1238
- context 'when a variable is assigned ' \
1239
- 'in main body of begin, rescue and else ' \
1240
- 'and reassigned in ensure then referenced after the begin' do
1241
- let(:source) do
1242
- [
1243
- 'begin',
1244
- ' do_something',
1245
- ' foo = :in_begin',
1246
- 'rescue FirstError',
1247
- ' foo = :in_first_rescue',
1248
- 'rescue SecondError',
1249
- ' foo = :in_second_rescue',
1250
- 'else',
1251
- ' foo = :in_else',
1252
- 'ensure',
1253
- ' foo = :in_ensure',
1254
- 'end',
1255
- '',
1256
- 'puts foo'
1257
- ]
1258
- end
1259
-
1260
- it 'registers offenses for each assignment before ensure' do
1261
- inspect_source(cop, source)
1262
- expect(cop.offenses.size).to eq(4)
1263
-
1264
- expect(cop.offenses[0].line).to eq(3)
1265
- expect(cop.offenses[1].line).to eq(5)
1266
- expect(cop.offenses[2].line).to eq(7)
1267
- expect(cop.offenses[3].line).to eq(9)
1268
- end
1269
- end
1270
-
1271
- context 'when a rescued error variable is wrongly tried to be referenced ' \
1272
- 'in another rescue body' do
1273
- let(:source) do
1274
- [
1275
- 'begin',
1276
- ' do_something',
1277
- 'rescue FirstError => error',
1278
- 'rescue SecondError',
1279
- ' p error # => nil',
1280
- 'end'
1281
- ]
1282
- end
1283
-
1284
- it 'registers an offense' do
1285
- inspect_source(cop, source)
1286
- expect(cop.offenses.size).to eq(1)
1287
- expect(cop.offenses.first.message)
1288
- .to eq('Useless assignment to variable - `error`.')
1289
- expect(cop.offenses.first.line).to eq(3)
1290
- expect(cop.highlights).to eq(['error'])
1291
- end
1292
- end
1293
-
1294
- context 'when a method argument is reassigned ' \
1295
- 'and zero arity super is called' do
1296
- let(:source) do
1297
- [
1298
- 'def some_method(foo)',
1299
- ' foo = 1',
1300
- ' super',
1301
- 'end'
1302
- ]
1303
- end
1304
-
1305
- include_examples 'accepts'
1306
- include_examples 'mimics MRI 2.1'
1307
- end
1308
-
1309
- context 'when a local variable is unreferenced ' \
1310
- 'and zero arity super is called' do
1311
- let(:source) do
1312
- [
1313
- 'def some_method(bar)',
1314
- ' foo = 1',
1315
- ' super',
1316
- 'end'
1317
- ]
1318
- end
1319
-
1320
- it 'registers an offense' do
1321
- inspect_source(cop, source)
1322
- expect(cop.offenses.size).to eq(1)
1323
- expect(cop.offenses.first.message)
1324
- .to eq('Useless assignment to variable - `foo`.')
1325
- expect(cop.offenses.first.line).to eq(2)
1326
- expect(cop.highlights).to eq(['foo'])
1327
- end
1328
-
1329
- include_examples 'mimics MRI 2.1'
1330
- end
1331
-
1332
- context 'when a method argument is reassigned ' \
1333
- 'but not passed to super' do
1334
- let(:source) do
1335
- [
1336
- 'def some_method(foo, bar)',
1337
- ' foo = 1',
1338
- ' super(bar)',
1339
- 'end'
1340
- ]
1341
- end
1342
-
1343
- it 'registers an offense' do
1344
- inspect_source(cop, source)
1345
- expect(cop.offenses.size).to eq(1)
1346
- expect(cop.offenses.first.message)
1347
- .to eq('Useless assignment to variable - `foo`.')
1348
- expect(cop.offenses.first.line).to eq(2)
1349
- expect(cop.highlights).to eq(['foo'])
1350
- end
1351
- end
1352
-
1353
- context 'when a named capture is unreferenced in top level' do
1354
- let(:source) do
1355
- "/(?<foo>\w+)/ =~ 'FOO'"
1356
- end
1357
-
1358
- it 'registers an offense' do
1359
- inspect_source(cop, source)
1360
- expect(cop.offenses.size).to eq(1)
1361
- expect(cop.offenses.first.message)
1362
- .to eq('Useless assignment to variable - `foo`.')
1363
- expect(cop.offenses.first.line).to eq(1)
1364
- end
1365
-
1366
- include_examples 'mimics MRI 2.1'
1367
- end
1368
-
1369
- context 'when a named capture is unreferenced ' \
1370
- 'in other than top level' do
1371
- let(:source) do
1372
- [
1373
- 'def some_method',
1374
- " /(?<foo>\w+)/ =~ 'FOO'",
1375
- 'end'
1376
- ]
1377
- end
1378
-
1379
- it 'registers an offense' do
1380
- inspect_source(cop, source)
1381
- expect(cop.offenses.size).to eq(1)
1382
- expect(cop.offenses.first.message)
1383
- .to eq('Useless assignment to variable - `foo`.')
1384
- expect(cop.offenses.first.line).to eq(2)
1385
- expect(cop.highlights).to eq(["/(?<foo>\w+)/"])
1386
- end
1387
-
1388
- # MRI 2.0 accepts this case, but I have no idea why it does so
1389
- # and there's no convincing reason to conform to this behavior,
1390
- # so RuboCop does not mimic MRI in this case.
1391
- end
1392
-
1393
- context 'when a named capture is referenced' do
1394
- let(:source) do
1395
- [
1396
- 'def some_method',
1397
- " /(?<foo>\w+)(?<bar>\s+)/ =~ 'FOO'",
1398
- ' puts foo',
1399
- ' puts bar',
1400
- 'end'
1401
- ]
1402
- end
1403
-
1404
- include_examples 'accepts'
1405
- include_examples 'mimics MRI 2.1'
1406
- end
1407
-
1408
- context 'when a variable is referenced ' \
1409
- 'in rhs of named capture expression' do
1410
- let(:source) do
1411
- [
1412
- 'def some_method',
1413
- " foo = 'some string'",
1414
- " /(?<foo>\w+)/ =~ foo",
1415
- ' puts foo',
1416
- 'end'
1417
- ]
1418
- end
1419
-
1420
- include_examples 'accepts'
1421
- end
1422
-
1423
- context 'when a variable is assigned in begin ' \
1424
- 'and referenced outside' do
1425
- let(:source) do
1426
- [
1427
- 'def some_method',
1428
- ' begin',
1429
- ' foo = 1',
1430
- ' end',
1431
- ' puts foo',
1432
- 'end'
1433
- ]
1434
- end
1435
-
1436
- include_examples 'accepts'
1437
- include_examples 'mimics MRI 2.1'
1438
- end
1439
-
1440
- context 'when a variable is shadowed by a block argument ' \
1441
- 'and unreferenced' do
1442
- let(:source) do
1443
- [
1444
- 'def some_method',
1445
- ' foo = 1',
1446
- ' 1.times do |foo|',
1447
- ' puts foo',
1448
- ' end',
1449
- 'end'
1450
- ]
1451
- end
1452
-
1453
- it 'registers an offense' do
1454
- inspect_source(cop, source)
1455
- expect(cop.offenses.size).to eq(1)
1456
- expect(cop.offenses.first.message)
1457
- .to eq('Useless assignment to variable - `foo`.')
1458
- expect(cop.offenses.first.line).to eq(2)
1459
- expect(cop.highlights).to eq(['foo'])
1460
- end
1461
-
1462
- include_examples 'mimics MRI 2.1', 'unused variable'
1463
- end
1464
-
1465
- context 'when a variable is not used and the name starts with _' do
1466
- let(:source) do
1467
- [
1468
- 'def some_method',
1469
- ' _foo = 1',
1470
- ' bar = 2',
1471
- ' puts bar',
1472
- 'end'
1473
- ]
1474
- end
1475
-
1476
- include_examples 'accepts'
1477
- include_examples 'mimics MRI 2.1'
1478
- end
1479
-
1480
- context 'when a method argument is not used' do
1481
- let(:source) do
1482
- [
1483
- 'def some_method(arg)',
1484
- 'end'
1485
- ]
1486
- end
1487
-
1488
- include_examples 'accepts'
1489
- include_examples 'mimics MRI 2.1'
1490
- end
1491
-
1492
- context 'when an optional method argument is not used' do
1493
- let(:source) do
1494
- [
1495
- 'def some_method(arg = nil)',
1496
- 'end'
1497
- ]
1498
- end
1499
-
1500
- include_examples 'accepts'
1501
- include_examples 'mimics MRI 2.1'
1502
- end
1503
-
1504
- context 'when a block method argument is not used' do
1505
- let(:source) do
1506
- [
1507
- 'def some_method(&block)',
1508
- 'end'
1509
- ]
1510
- end
1511
-
1512
- include_examples 'accepts'
1513
- include_examples 'mimics MRI 2.1'
1514
- end
1515
-
1516
- context 'when a splat method argument is not used' do
1517
- let(:source) do
1518
- [
1519
- 'def some_method(*args)',
1520
- 'end'
1521
- ]
1522
- end
1523
-
1524
- include_examples 'accepts'
1525
- include_examples 'mimics MRI 2.1'
1526
- end
1527
-
1528
- context 'when a optional keyword method argument is not used' do
1529
- let(:source) do
1530
- [
1531
- 'def some_method(name: value)',
1532
- 'end'
1533
- ]
1534
- end
1535
-
1536
- include_examples 'accepts' unless RUBY_VERSION < '2.0'
1537
- include_examples 'mimics MRI 2.1'
1538
- end
1539
-
1540
- context 'when a keyword splat method argument is used' do
1541
- let(:source) do
1542
- [
1543
- 'def some_method(name: value, **rest_keywords)',
1544
- ' p rest_keywords',
1545
- 'end'
1546
- ]
1547
- end
1548
-
1549
- include_examples 'accepts' unless RUBY_VERSION < '2.0'
1550
- include_examples 'mimics MRI 2.1'
1551
- end
1552
-
1553
- context 'when a keyword splat method argument is not used' do
1554
- let(:source) do
1555
- [
1556
- 'def some_method(name: value, **rest_keywords)',
1557
- 'end'
1558
- ]
1559
- end
1560
-
1561
- include_examples 'accepts' unless RUBY_VERSION < '2.0'
1562
- include_examples 'mimics MRI 2.1'
1563
- end
1564
-
1565
- context 'when an anonymous keyword splat method argument is defined' do
1566
- let(:source) do
1567
- [
1568
- 'def some_method(name: value, **)',
1569
- 'end'
1570
- ]
1571
- end
1572
-
1573
- include_examples 'accepts' unless RUBY_VERSION < '2.0'
1574
- include_examples 'mimics MRI 2.1'
1575
- end
1576
-
1577
- context 'when a block argument is not used' do
1578
- let(:source) do
1579
- [
1580
- '1.times do |i|',
1581
- 'end'
1582
- ]
1583
- end
1584
-
1585
- include_examples 'accepts'
1586
- include_examples 'mimics MRI 2.1'
1587
- end
1588
-
1589
- context 'when there is only one AST node and it is unused variable' do
1590
- let(:source) do
1591
- 'foo = 1'
1592
- end
1593
-
1594
- it 'registers an offense' do
1595
- inspect_source(cop, source)
1596
- expect(cop.offenses.size).to eq(1)
1597
- expect(cop.offenses.first.message)
1598
- .to eq('Useless assignment to variable - `foo`.')
1599
- expect(cop.offenses.first.line).to eq(1)
1600
- expect(cop.highlights).to eq(['foo'])
1601
- end
1602
-
1603
- include_examples 'mimics MRI 2.1'
1604
- end
1605
-
1606
- context 'when a variable is assigned ' \
1607
- 'while being passed to a method taking block' do
1608
- context 'and the variable is used' do
1609
- let(:source) do
1610
- [
1611
- 'some_method(foo = 1) do',
1612
- 'end',
1613
- 'puts foo'
1614
- ]
1615
- end
1616
-
1617
- include_examples 'accepts'
1618
- include_examples 'mimics MRI 2.1'
1619
- end
1620
-
1621
- context 'and the variable is not used' do
1622
- let(:source) do
1623
- [
1624
- 'some_method(foo = 1) do',
1625
- 'end'
1626
- ]
1627
- end
1628
-
1629
- it 'registers an offense' do
1630
- inspect_source(cop, source)
1631
- expect(cop.offenses.size).to eq(1)
1632
- expect(cop.offenses.first.message)
1633
- .to eq('Useless assignment to variable - `foo`.')
1634
- expect(cop.offenses.first.line).to eq(1)
1635
- expect(cop.highlights).to eq(['foo'])
1636
- end
1637
-
1638
- include_examples 'mimics MRI 2.1'
1639
- end
1640
- end
1641
-
1642
- context 'when a variable is assigned ' \
1643
- 'and passed to a method followed by method taking block' do
1644
- let(:source) do
1645
- [
1646
- "pattern = '*.rb'",
1647
- 'Dir.glob(pattern).map do |path|',
1648
- 'end'
1649
- ]
1650
- end
1651
-
1652
- include_examples 'accepts'
1653
- include_examples 'mimics MRI 2.1'
1654
- end
1655
-
1656
- describe 'similar name suggestion' do
1657
- context "when there's a similar variable-like method invocation" do
1658
- let(:source) do
1659
- [
1660
- 'def some_method',
1661
- ' enviromnent = {}',
1662
- ' another_symbol',
1663
- ' puts environment',
1664
- 'end'
1665
- ]
1666
- end
1667
-
1668
- it 'suggests the method name' do
1669
- inspect_source(cop, source)
1670
- expect(cop.offenses.size).to eq(1)
1671
- expect(cop.offenses.first.message).to eq(
1672
- 'Useless assignment to variable - `enviromnent`. ' \
1673
- 'Did you mean `environment`?'
1674
- )
1675
- end
1676
- end
1677
-
1678
- context "when there's a similar variable" do
1679
- let(:source) do
1680
- [
1681
- 'def some_method',
1682
- ' environment = nil',
1683
- ' another_symbol',
1684
- ' enviromnent = {}',
1685
- ' puts environment',
1686
- 'end'
1687
- ]
1688
- end
1689
-
1690
- it 'suggests the variable name' do
1691
- inspect_source(cop, source)
1692
- expect(cop.offenses.size).to eq(1)
1693
- expect(cop.offenses.first.message).to eq(
1694
- 'Useless assignment to variable - `enviromnent`. ' \
1695
- 'Did you mean `environment`?'
1696
- )
1697
- end
1698
- end
1699
-
1700
- context 'when there are only less similar names' do
1701
- let(:source) do
1702
- [
1703
- 'def some_method',
1704
- ' enviromnent = {}',
1705
- ' another_symbol',
1706
- ' puts envelope',
1707
- 'end'
1708
- ]
1709
- end
1710
-
1711
- it 'does not suggest any name' do
1712
- inspect_source(cop, source)
1713
- expect(cop.offenses.size).to eq(1)
1714
- expect(cop.offenses.first.message)
1715
- .to eq('Useless assignment to variable - `enviromnent`.')
1716
- end
1717
- end
1718
-
1719
- context "when there's a similar method invocation with explicit receiver" do
1720
- let(:source) do
1721
- [
1722
- 'def some_method',
1723
- ' enviromnent = {}',
1724
- ' another_symbol',
1725
- ' puts self.environment',
1726
- 'end'
1727
- ]
1728
- end
1729
-
1730
- it 'does not suggest any name' do
1731
- inspect_source(cop, source)
1732
- expect(cop.offenses.size).to eq(1)
1733
- expect(cop.offenses.first.message)
1734
- .to eq('Useless assignment to variable - `enviromnent`.')
1735
- end
1736
- end
1737
-
1738
- context "when there's a similar method invocation with arguments" do
1739
- let(:source) do
1740
- [
1741
- 'def some_method',
1742
- ' enviromnent = {}',
1743
- ' another_symbol',
1744
- ' puts environment(1)',
1745
- 'end'
1746
- ]
1747
- end
1748
-
1749
- it 'does not suggest any name' do
1750
- inspect_source(cop, source)
1751
- expect(cop.offenses.size).to eq(1)
1752
- expect(cop.offenses.first.message)
1753
- .to eq('Useless assignment to variable - `enviromnent`.')
1754
- end
1755
- end
1756
-
1757
- context "when there's a similar name but it's in inner scope" do
1758
- let(:source) do
1759
- [
1760
- 'class SomeClass',
1761
- ' enviromnent = {}',
1762
- '',
1763
- ' def some_method(environment)',
1764
- ' puts environment',
1765
- ' end',
1766
- 'end'
1767
- ]
1768
- end
1769
-
1770
- it 'does not suggest any name' do
1771
- inspect_source(cop, source)
1772
- expect(cop.offenses.size).to eq(1)
1773
- expect(cop.offenses.first.message)
1774
- .to eq('Useless assignment to variable - `enviromnent`.')
1775
- end
1776
- end
1777
- end
1778
- end