rubocop 0.15.0 → 0.16.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 (221) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -10
  3. data/.travis.yml +2 -1
  4. data/CHANGELOG.md +55 -0
  5. data/Gemfile +3 -0
  6. data/README.md +37 -0
  7. data/config/default.yml +99 -16
  8. data/config/enabled.yml +28 -16
  9. data/lib/rubocop.rb +16 -2
  10. data/lib/rubocop/cli.rb +10 -91
  11. data/lib/rubocop/config.rb +4 -1
  12. data/lib/rubocop/config_loader.rb +18 -10
  13. data/lib/rubocop/config_store.rb +3 -2
  14. data/lib/rubocop/cop/check_assignment.rb +43 -0
  15. data/lib/rubocop/cop/check_methods.rb +18 -0
  16. data/lib/rubocop/cop/commissioner.rb +15 -3
  17. data/lib/rubocop/cop/cop.rb +51 -43
  18. data/lib/rubocop/cop/lint/assignment_in_condition.rb +1 -1
  19. data/lib/rubocop/cop/lint/block_alignment.rb +7 -7
  20. data/lib/rubocop/cop/lint/debugger.rb +1 -1
  21. data/lib/rubocop/cop/lint/empty_ensure.rb +1 -1
  22. data/lib/rubocop/cop/lint/end_alignment.rb +56 -19
  23. data/lib/rubocop/cop/lint/end_in_method.rb +4 -10
  24. data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
  25. data/lib/rubocop/cop/lint/eval.rb +1 -1
  26. data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -1
  27. data/lib/rubocop/cop/lint/literal_in_condition.rb +2 -2
  28. data/lib/rubocop/cop/lint/loop.rb +1 -1
  29. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
  30. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  31. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
  32. data/lib/rubocop/cop/lint/syntax.rb +1 -1
  33. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
  34. data/lib/rubocop/cop/lint/useless_assignment.rb +2 -2
  35. data/lib/rubocop/cop/lint/useless_comparison.rb +1 -1
  36. data/lib/rubocop/cop/lint/useless_setter_call.rb +6 -16
  37. data/lib/rubocop/cop/lint/void.rb +5 -5
  38. data/lib/rubocop/cop/rails/default_scope.rb +33 -0
  39. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +2 -4
  40. data/lib/rubocop/cop/rails/output.rb +1 -1
  41. data/lib/rubocop/cop/rails/read_attribute.rb +1 -1
  42. data/lib/rubocop/cop/rails/validation.rb +1 -1
  43. data/lib/rubocop/cop/style/access_modifier_indentation.rb +31 -16
  44. data/lib/rubocop/cop/style/accessor_method_name.rb +53 -0
  45. data/lib/rubocop/cop/style/alias.rb +14 -2
  46. data/lib/rubocop/cop/style/align_hash.rb +15 -16
  47. data/lib/rubocop/cop/style/and_or.rb +3 -3
  48. data/lib/rubocop/cop/style/array_syntax.rb +22 -0
  49. data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
  50. data/lib/rubocop/cop/style/ascii_identifiers.rb +1 -3
  51. data/lib/rubocop/cop/style/attr.rb +1 -1
  52. data/lib/rubocop/cop/style/autocorrect_alignment.rb +13 -12
  53. data/lib/rubocop/cop/style/begin_block.rb +1 -1
  54. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  55. data/lib/rubocop/cop/style/block_nesting.rb +5 -1
  56. data/lib/rubocop/cop/style/blocks.rb +4 -4
  57. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +48 -22
  58. data/lib/rubocop/cop/style/case_equality.rb +1 -1
  59. data/lib/rubocop/cop/style/case_indentation.rb +25 -6
  60. data/lib/rubocop/cop/style/class_and_module_camel_case.rb +1 -1
  61. data/lib/rubocop/cop/style/class_length.rb +7 -19
  62. data/lib/rubocop/cop/style/class_methods.rb +1 -1
  63. data/lib/rubocop/cop/style/class_vars.rb +1 -1
  64. data/lib/rubocop/cop/style/code_length.rb +35 -0
  65. data/lib/rubocop/cop/style/collection_methods.rb +1 -1
  66. data/lib/rubocop/cop/style/colon_method_call.rb +1 -1
  67. data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
  68. data/lib/rubocop/cop/style/configurable_enforced_style.rb +51 -0
  69. data/lib/rubocop/cop/style/configurable_max.rb +17 -0
  70. data/lib/rubocop/cop/style/configurable_naming.rb +4 -10
  71. data/lib/rubocop/cop/style/constant_name.rb +5 -3
  72. data/lib/rubocop/cop/style/cyclomatic_complexity.rb +8 -13
  73. data/lib/rubocop/cop/style/def_parentheses.rb +4 -43
  74. data/lib/rubocop/cop/style/documentation.rb +1 -1
  75. data/lib/rubocop/cop/style/dot_position.rb +6 -2
  76. data/lib/rubocop/cop/style/empty_line_between_defs.rb +1 -1
  77. data/lib/rubocop/cop/style/empty_lines.rb +5 -1
  78. data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +1 -1
  79. data/lib/rubocop/cop/style/empty_lines_around_body.rb +28 -13
  80. data/lib/rubocop/cop/style/empty_literal.rb +3 -3
  81. data/lib/rubocop/cop/style/encoding.rb +5 -5
  82. data/lib/rubocop/cop/style/end_block.rb +1 -1
  83. data/lib/rubocop/cop/style/end_of_line.rb +8 -6
  84. data/lib/rubocop/cop/style/even_odd.rb +6 -6
  85. data/lib/rubocop/cop/style/favor_join.rb +1 -1
  86. data/lib/rubocop/cop/style/favor_modifier.rb +20 -34
  87. data/lib/rubocop/cop/style/favor_sprintf.rb +1 -1
  88. data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +1 -1
  89. data/lib/rubocop/cop/style/final_newline.rb +1 -1
  90. data/lib/rubocop/cop/style/flip_flop.rb +20 -0
  91. data/lib/rubocop/cop/style/for.rb +34 -3
  92. data/lib/rubocop/cop/style/global_vars.rb +1 -1
  93. data/lib/rubocop/cop/style/hash_methods.rb +3 -3
  94. data/lib/rubocop/cop/style/hash_syntax.rb +8 -11
  95. data/lib/rubocop/cop/style/if_node.rb +25 -0
  96. data/lib/rubocop/cop/style/if_then_else.rb +1 -1
  97. data/lib/rubocop/cop/style/indentation_width.rb +77 -43
  98. data/lib/rubocop/cop/style/lambda.rb +2 -2
  99. data/lib/rubocop/cop/style/lambda_call.rb +4 -12
  100. data/lib/rubocop/cop/style/leading_comment_space.rb +1 -1
  101. data/lib/rubocop/cop/style/line_length.rb +9 -5
  102. data/lib/rubocop/cop/style/method_call_parentheses.rb +1 -1
  103. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +1 -1
  104. data/lib/rubocop/cop/style/method_def_parentheses.rb +52 -0
  105. data/lib/rubocop/cop/style/method_length.rb +9 -22
  106. data/lib/rubocop/cop/style/module_function.rb +1 -1
  107. data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
  108. data/lib/rubocop/cop/style/multiline_if_then.rb +11 -1
  109. data/lib/rubocop/cop/style/nil_comparison.rb +1 -1
  110. data/lib/rubocop/cop/style/not.rb +1 -1
  111. data/lib/rubocop/cop/style/numeric_literals.rb +2 -2
  112. data/lib/rubocop/cop/style/op_method.rb +2 -2
  113. data/lib/rubocop/cop/style/parameter_lists.rb +2 -2
  114. data/lib/rubocop/cop/style/parentheses_around_condition.rb +8 -1
  115. data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
  116. data/lib/rubocop/cop/style/predicate_name.rb +54 -0
  117. data/lib/rubocop/cop/style/proc.rb +1 -1
  118. data/lib/rubocop/cop/style/raise_args.rb +4 -10
  119. data/lib/rubocop/cop/style/redundant_begin.rb +5 -15
  120. data/lib/rubocop/cop/style/redundant_exception.rb +1 -1
  121. data/lib/rubocop/cop/style/redundant_return.rb +9 -19
  122. data/lib/rubocop/cop/style/redundant_self.rb +8 -3
  123. data/lib/rubocop/cop/style/regexp_literal.rb +4 -3
  124. data/lib/rubocop/cop/style/rescue_modifier.rb +9 -17
  125. data/lib/rubocop/cop/style/semicolon.rb +20 -22
  126. data/lib/rubocop/cop/style/signal_exception.rb +10 -17
  127. data/lib/rubocop/cop/style/single_line_block_params.rb +62 -0
  128. data/lib/rubocop/cop/style/single_line_methods.rb +5 -15
  129. data/lib/rubocop/cop/style/space_after_comma_etc.rb +1 -3
  130. data/lib/rubocop/cop/style/space_after_control_keyword.rb +1 -1
  131. data/lib/rubocop/cop/style/space_after_method_name.rb +4 -12
  132. data/lib/rubocop/cop/style/space_after_not.rb +1 -1
  133. data/lib/rubocop/cop/style/space_around_block_braces.rb +105 -94
  134. data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +1 -1
  135. data/lib/rubocop/cop/style/space_around_operators.rb +38 -123
  136. data/lib/rubocop/cop/style/space_before_modifier_keyword.rb +1 -1
  137. data/lib/rubocop/cop/style/space_inside.rb +1 -3
  138. data/lib/rubocop/cop/style/space_inside_brackets.rb +0 -2
  139. data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +5 -5
  140. data/lib/rubocop/cop/style/space_inside_parens.rb +0 -2
  141. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  142. data/lib/rubocop/cop/style/string_help.rb +1 -1
  143. data/lib/rubocop/cop/style/string_literals.rb +4 -11
  144. data/lib/rubocop/cop/style/surrounding_space.rb +0 -2
  145. data/lib/rubocop/cop/style/symbol_array.rb +3 -10
  146. data/lib/rubocop/cop/style/tab.rb +5 -5
  147. data/lib/rubocop/cop/style/ternary_operator.rb +2 -2
  148. data/lib/rubocop/cop/style/trailing_blank_lines.rb +1 -1
  149. data/lib/rubocop/cop/style/trailing_whitespace.rb +1 -1
  150. data/lib/rubocop/cop/style/trivial_accessors.rb +4 -14
  151. data/lib/rubocop/cop/style/unless_else.rb +1 -1
  152. data/lib/rubocop/cop/style/variable_interpolation.rb +1 -1
  153. data/lib/rubocop/cop/style/when_then.rb +3 -1
  154. data/lib/rubocop/cop/style/while_until_do.rb +2 -2
  155. data/lib/rubocop/cop/style/word_array.rb +4 -10
  156. data/lib/rubocop/cop/util.rb +85 -12
  157. data/lib/rubocop/cop/variable_inspector.rb +4 -2
  158. data/lib/rubocop/file_inspector.rb +98 -0
  159. data/lib/rubocop/formatter/base_formatter.rb +2 -2
  160. data/lib/rubocop/formatter/disabled_config_formatter.rb +10 -2
  161. data/lib/rubocop/rake_task.rb +5 -1
  162. data/lib/rubocop/target_finder.rb +3 -3
  163. data/lib/rubocop/version.rb +1 -1
  164. data/rubocop-todo.yml +12 -0
  165. data/rubocop.gemspec +1 -1
  166. data/spec/isolated_environment_spec.rb +24 -0
  167. data/spec/rubocop/cli_spec.rb +120 -27
  168. data/spec/rubocop/config_loader_spec.rb +25 -11
  169. data/spec/rubocop/config_spec.rb +25 -9
  170. data/spec/rubocop/config_store_spec.rb +1 -1
  171. data/spec/rubocop/cop/cop_spec.rb +22 -3
  172. data/spec/rubocop/cop/lint/block_alignment_spec.rb +4 -4
  173. data/spec/rubocop/cop/lint/end_alignment_spec.rb +79 -47
  174. data/spec/rubocop/cop/lint/syntax_spec.rb +4 -3
  175. data/spec/rubocop/cop/rails/default_scope_spec.rb +31 -0
  176. data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +37 -2
  177. data/spec/rubocop/cop/style/accessor_method_name_spec.rb +81 -0
  178. data/spec/rubocop/cop/style/alias_spec.rb +10 -0
  179. data/spec/rubocop/cop/style/align_parameters_spec.rb +3 -3
  180. data/spec/rubocop/cop/style/block_nesting_spec.rb +4 -1
  181. data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +83 -0
  182. data/spec/rubocop/cop/style/case_indentation_spec.rb +227 -71
  183. data/spec/rubocop/cop/style/class_length_spec.rb +2 -0
  184. data/spec/rubocop/cop/style/constant_name_spec.rb +6 -0
  185. data/spec/rubocop/cop/style/cyclomatic_complexity_spec.rb +1 -0
  186. data/spec/rubocop/cop/style/documentation_spec.rb +12 -0
  187. data/spec/rubocop/cop/style/dot_position_spec.rb +10 -0
  188. data/spec/rubocop/cop/style/empty_lines_around_body_spec.rb +22 -0
  189. data/spec/rubocop/cop/style/empty_lines_spec.rb +6 -0
  190. data/spec/rubocop/cop/style/end_of_line_spec.rb +17 -0
  191. data/spec/rubocop/cop/style/favor_modifier_spec.rb +15 -0
  192. data/spec/rubocop/cop/style/flip_flop_spec.rb +23 -0
  193. data/spec/rubocop/cop/style/for_spec.rb +96 -18
  194. data/spec/rubocop/cop/style/hash_syntax_spec.rb +8 -2
  195. data/spec/rubocop/cop/style/indentation_width_spec.rb +185 -20
  196. data/spec/rubocop/cop/style/line_length_spec.rb +1 -0
  197. data/spec/rubocop/cop/style/method_def_parentheses_spec.rb +72 -0
  198. data/spec/rubocop/cop/style/multiline_if_then_spec.rb +16 -0
  199. data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +34 -3
  200. data/spec/rubocop/cop/style/predicate_name_spec.rb +25 -0
  201. data/spec/rubocop/cop/style/redundant_self_spec.rb +15 -0
  202. data/spec/rubocop/cop/style/regexp_literal_spec.rb +5 -0
  203. data/spec/rubocop/cop/style/semicolon_spec.rb +16 -16
  204. data/spec/rubocop/cop/style/{reduce_arguments_spec.rb → single_line_block_params_spec.rb} +12 -4
  205. data/spec/rubocop/cop/style/space_around_block_braces_spec.rb +58 -8
  206. data/spec/rubocop/cop/style/space_around_operators_spec.rb +71 -0
  207. data/spec/rubocop/cop/style/string_literals_spec.rb +18 -0
  208. data/spec/rubocop/file_inspector_spec.rb +78 -0
  209. data/spec/rubocop/formatter/clang_style_formatter_spec.rb +7 -7
  210. data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +3 -3
  211. data/spec/rubocop/formatter/file_list_formatter_spec.rb +2 -2
  212. data/spec/rubocop/processed_source_spec.rb +7 -3
  213. data/spec/rubocop/token_spec.rb +1 -1
  214. data/spec/spec_helper.rb +3 -2
  215. data/spec/support/isolated_environment.rb +7 -0
  216. data/spec/support/shared_context.rb +5 -1
  217. metadata +35 -12
  218. data/lib/rubocop/cop/style/reduce_arguments.rb +0 -34
  219. data/lib/rubocop/cop/style/symbol_name.rb +0 -45
  220. data/spec/rubocop/cop/style/def_without_parentheses_spec.rb +0 -33
  221. data/spec/rubocop/cop/style/symbol_name_spec.rb +0 -138
@@ -35,7 +35,7 @@ module Rubocop
35
35
 
36
36
  # assignment nodes from shorthand ops like ||= don't have operator
37
37
  if asgn_node.type != :begin && asgn_node.loc.operator
38
- warning(asgn_node, :operator)
38
+ add_offence(asgn_node, :operator)
39
39
  end
40
40
  end
41
41
  end
@@ -129,13 +129,13 @@ module Rubocop
129
129
  match = /\S.*/.match(do_loc.source_line)
130
130
  indentation_of_do_line = match.begin(0)
131
131
  if end_loc.column != indentation_of_do_line
132
- warning(nil,
133
- end_loc,
134
- sprintf(MSG, end_loc.line, end_loc.column,
135
- start_loc.source.lines.to_a.first.chomp,
136
- start_loc.line, start_loc.column,
137
- alt_start_msg(match, start_loc, do_loc,
138
- indentation_of_do_line)))
132
+ add_offence(nil,
133
+ end_loc,
134
+ sprintf(MSG, end_loc.line, end_loc.column,
135
+ start_loc.source.lines.to_a.first.chomp,
136
+ start_loc.line, start_loc.column,
137
+ alt_start_msg(match, start_loc, do_loc,
138
+ indentation_of_do_line)))
139
139
  end
140
140
  end
141
141
  end
@@ -27,7 +27,7 @@ module Rubocop
27
27
  DEBUGGER_NODES = [DEBUGGER_NODE, PRY_NODE, REMOTE_PRY_NODE]
28
28
 
29
29
  def on_send(node)
30
- warning(node, :selector) if DEBUGGER_NODES.include?(node)
30
+ add_offence(node, :selector) if DEBUGGER_NODES.include?(node)
31
31
  end
32
32
  end
33
33
  end
@@ -10,7 +10,7 @@ module Rubocop
10
10
  def on_ensure(node)
11
11
  _body, ensure_body = *node
12
12
 
13
- warning(node, :keyword) unless ensure_body
13
+ add_offence(node, :keyword) unless ensure_body
14
14
  end
15
15
  end
16
16
  end
@@ -5,23 +5,21 @@ module Rubocop
5
5
  module Lint
6
6
  # This cop checks whether the end keywords are aligned properly.
7
7
  #
8
- # For keywords (if, def, etc.) the end is aligned with the start
9
- # of the keyword.
8
+ # Two modes are supported through the AlignWith configuration
9
+ # parameter. If it's set to `keyword` (which is the default), the `end`
10
+ # shall be aligned with the start of the keyword (if, def, etc.). If it's
11
+ # set to `variable` the `end` shall be aligned with the left-hand-side of
12
+ # the variable assignment, if there is one.
10
13
  #
11
14
  # @example
12
15
  #
13
16
  # variable = if true
14
17
  # end
15
18
  class EndAlignment < Cop
16
- MSG = 'end at %d, %d is not aligned with %s at %d, %d'
17
-
18
- def on_def(node)
19
- check(node)
20
- end
19
+ include CheckMethods
20
+ include CheckAssignment
21
21
 
22
- def on_defs(node)
23
- check(node)
24
- end
22
+ MSG = 'end at %d, %d is not aligned with %s at %d, %d'
25
23
 
26
24
  def on_class(node)
27
25
  check(node)
@@ -45,18 +43,57 @@ module Rubocop
45
43
 
46
44
  private
47
45
 
48
- def check(node)
49
- # discard modifier forms of if/while/until
50
- return unless node.loc.end
46
+ def check_assignment(node, rhs)
47
+ # If there are method calls chained to the right hand side of the
48
+ # assignment, we let rhs be the receiver of those method calls before
49
+ # we check if it's an if/unless/while/until.
50
+ rhs = first_part_of_call_chain(rhs)
51
+
52
+ return unless rhs
53
+
54
+ case rhs.type
55
+ when :if, :while, :until
56
+ return if rhs.loc.respond_to?(:question) # ternary
57
+
58
+ offset = if alignment == :variable
59
+ rhs.loc.keyword.column - node.loc.expression.column
60
+ else
61
+ 0
62
+ end
63
+ expr = node.loc.expression
64
+ range = Parser::Source::Range.new(expr.source_buffer,
65
+ expr.begin_pos,
66
+ rhs.loc.keyword.end_pos)
67
+ check_offset(rhs, range.source, offset)
68
+ ignore_node(rhs) # Don't check again.
69
+ end
70
+ end
71
+
72
+ def check(node, *_)
73
+ check_offset(node, node.loc.keyword.source, 0)
74
+ end
75
+
76
+ def check_offset(node, alignment_base, offset)
77
+ return if ignored_node?(node)
51
78
 
52
- kw_loc = node.loc.keyword
53
79
  end_loc = node.loc.end
80
+ return unless end_loc # Discard modifier forms of if/while/until.
81
+
82
+ kw_loc = node.loc.keyword
83
+
84
+ if kw_loc.line != end_loc.line &&
85
+ kw_loc.column != end_loc.column + offset
86
+ add_offence(nil, end_loc,
87
+ sprintf(MSG, end_loc.line, end_loc.column,
88
+ alignment_base, kw_loc.line, kw_loc.column))
89
+ end
90
+ end
54
91
 
55
- if kw_loc.line != end_loc.line && kw_loc.column != end_loc.column
56
- warning(nil,
57
- end_loc,
58
- sprintf(MSG, end_loc.line, end_loc.column,
59
- kw_loc.source, kw_loc.line, kw_loc.column))
92
+ def alignment
93
+ a = cop_config['AlignWith']
94
+ case a
95
+ when 'keyword', 'variable' then a.to_sym
96
+ else fail "Unknown AlignWith: #{a}"
60
97
  end
61
98
  end
62
99
  end
@@ -5,21 +5,15 @@ module Rubocop
5
5
  module Lint
6
6
  # This cop checks for END blocks in method definitions.
7
7
  class EndInMethod < Cop
8
- MSG = 'END found in method definition. Use `at_exit` instead.'
9
-
10
- def on_def(node)
11
- check(node)
12
- end
8
+ include CheckMethods
13
9
 
14
- def on_defs(node)
15
- check(node)
16
- end
10
+ MSG = 'END found in method definition. Use `at_exit` instead.'
17
11
 
18
12
  private
19
13
 
20
- def check(node)
14
+ def check(node, *_)
21
15
  on_node(:postexe, node) do |end_node|
22
- warning(end_node, :keyword)
16
+ add_offence(end_node, :keyword)
23
17
  end
24
18
  end
25
19
  end
@@ -13,7 +13,7 @@ module Rubocop
13
13
  return unless ensure_body
14
14
 
15
15
  on_node(:return, ensure_body) do |e|
16
- warning(e, :expression)
16
+ add_offence(e, :expression)
17
17
  end
18
18
  end
19
19
  end
@@ -10,7 +10,7 @@ module Rubocop
10
10
  def on_send(node)
11
11
  receiver, method_name, = *node
12
12
 
13
- warning(node, :selector) if receiver.nil? && method_name == :eval
13
+ add_offence(node, :selector) if receiver.nil? && method_name == :eval
14
14
  end
15
15
  end
16
16
  end
@@ -10,7 +10,7 @@ module Rubocop
10
10
  def on_resbody(node)
11
11
  _exc_list_node, _exc_var_node, body_node = *node
12
12
 
13
- warning(node, :expression) unless body_node
13
+ add_offence(node, :expression) unless body_node
14
14
  end
15
15
  end
16
16
  end
@@ -54,7 +54,7 @@ module Rubocop
54
54
 
55
55
  # if the cond node is literal we obviously have a problem
56
56
  if literal?(cond)
57
- warning(cond, :expression)
57
+ add_offence(cond, :expression)
58
58
  else
59
59
  # alternatively we have to consider a logical node with a
60
60
  # literal argument
@@ -94,7 +94,7 @@ module Rubocop
94
94
 
95
95
  def handle_node(node)
96
96
  if literal?(node)
97
- warning(node, :expression)
97
+ add_offence(node, :expression)
98
98
  elsif [:send, :and, :or, :begin].include?(node.type)
99
99
  check_node(node)
100
100
  end
@@ -19,7 +19,7 @@ module Rubocop
19
19
  private
20
20
 
21
21
  def register_offence(node)
22
- warning(node, :keyword)
22
+ add_offence(node, :keyword)
23
23
  end
24
24
  end
25
25
  end
@@ -26,7 +26,7 @@ module Rubocop
26
26
  Parser::Source::Range.new(expr.source_buffer,
27
27
  expr.begin_pos - space_length,
28
28
  expr.begin_pos)
29
- warning(nil, space_range)
29
+ add_offence(nil, space_range)
30
30
  end
31
31
  end
32
32
  end
@@ -11,7 +11,7 @@ module Rubocop
11
11
  return unless node.children.first
12
12
  rescue_args = node.children.first.children
13
13
  if rescue_args.any? { |a| targets_exception?(a) }
14
- warning(node, :expression)
14
+ add_offence(node, :expression)
15
15
  end
16
16
  end
17
17
 
@@ -23,7 +23,7 @@ module Rubocop
23
23
  return unless outer_local_variable
24
24
 
25
25
  message = sprintf(MSG, variable.name)
26
- warning(variable.declaration_node, :expression, message)
26
+ add_offence(variable.declaration_node, :expression, message)
27
27
  end
28
28
  end
29
29
  end
@@ -25,7 +25,7 @@ module Rubocop
25
25
 
26
26
  def investigate(processed_source)
27
27
  processed_source.diagnostics.each do |d|
28
- add_offence(d.level, nil, d.location, d.message)
28
+ add_offence(nil, d.location, d.message, d.level)
29
29
  end
30
30
  end
31
31
  end
@@ -17,7 +17,7 @@ module Rubocop
17
17
 
18
18
  expressions.each_cons(2) do |e1, e2|
19
19
  if NODE_TYPES.include?(e1.type) || flow_command?(e1)
20
- warning(e2, :expression)
20
+ add_offence(e2, :expression)
21
21
  end
22
22
  end
23
23
  end
@@ -42,7 +42,7 @@ module Rubocop
42
42
  assignment.node.loc.name
43
43
  end
44
44
 
45
- warning(nil, location, message)
45
+ add_offence(nil, location, message)
46
46
  end
47
47
  end
48
48
 
@@ -80,7 +80,7 @@ module Rubocop
80
80
  return unless variable.block_local_variable?
81
81
  return unless variable.assignments.empty?
82
82
  message = sprintf(MSG, variable.name)
83
- warning(variable.declaration_node, :expression, message)
83
+ add_offence(variable.declaration_node, :expression, message)
84
84
  end
85
85
  end
86
86
  end
@@ -21,7 +21,7 @@ module Rubocop
21
21
  if OPS.include?(op)
22
22
  receiver, _method, args = *node
23
23
 
24
- warning(node, :selector) if receiver == args
24
+ add_offence(node, :selector) if receiver == args
25
25
  end
26
26
  end
27
27
  end
@@ -13,24 +13,14 @@ module Rubocop
13
13
  # x.attr = 5
14
14
  # end
15
15
  class UselessSetterCall < Cop
16
+ include CheckMethods
17
+
16
18
  MSG = 'Useless setter call to local variable %s.'
17
19
  ASSIGNMENT_TYPES = [:lvasgn, :ivasgn, :cvasgn, :gvasgn].freeze
18
20
 
19
- def on_def(node)
20
- _name, args, body = *node
21
-
22
- check_for_useless_assignment(body, args)
23
- end
24
-
25
- def on_defs(node)
26
- _target, _name, args, body = *node
27
-
28
- check_for_useless_assignment(body, args)
29
- end
30
-
31
21
  private
32
22
 
33
- def check_for_useless_assignment(body, args)
23
+ def check(_node, _method_name, args, body)
34
24
  return unless body
35
25
 
36
26
  if body.type == :begin
@@ -48,9 +38,9 @@ module Rubocop
48
38
  var_name, = *receiver
49
39
  return if tracker.contain_object_passed_as_argument?(var_name)
50
40
 
51
- warning(receiver,
52
- :name,
53
- MSG.format(receiver.loc.name.source))
41
+ add_offence(receiver,
42
+ :name,
43
+ MSG.format(receiver.loc.name.source))
54
44
  end
55
45
 
56
46
  def setter_call_to_local_variable?(node)
@@ -32,20 +32,20 @@ module Rubocop
32
32
 
33
33
  op = node.loc.selector.source
34
34
 
35
- warning(node, :selector, sprintf(OP_MSG, op)) if OPS.include?(op)
35
+ add_offence(node, :selector, sprintf(OP_MSG, op)) if OPS.include?(op)
36
36
  end
37
37
 
38
38
  def check_for_var(node)
39
39
  if VARS.include?(node.type)
40
- warning(node, :name,
41
- sprintf(VAR_MSG, node.loc.name.source))
40
+ add_offence(node, :name,
41
+ sprintf(VAR_MSG, node.loc.name.source))
42
42
  end
43
43
  end
44
44
 
45
45
  def check_for_literal(node)
46
46
  if LITERALS.include?(node.type)
47
- warning(node, :expression,
48
- sprintf(LIT_MSG, node.loc.expression.source))
47
+ add_offence(node, :expression,
48
+ sprintf(LIT_MSG, node.loc.expression.source))
49
49
  end
50
50
  end
51
51
  end
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ module Rails
6
+ # This cop checks for default_scope calls when it was passed
7
+ # a lambda or a proc instead of a block.
8
+ #
9
+ # @example
10
+ #
11
+ # # incorrect
12
+ # default_scope -> { something }
13
+ #
14
+ # # correct
15
+ # default_scope { something }
16
+ class DefaultScope < Cop
17
+ MSG = 'default_scope expects a block as its sole argument.'
18
+
19
+ def on_send(node)
20
+ return unless command?(:default_scope, node)
21
+
22
+ _receiver, _method_name, *args = *node
23
+
24
+ return unless args.size == 1
25
+
26
+ first_arg = args[0]
27
+
28
+ add_offence(first_arg, :expression) if lambda_or_proc?(first_arg)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -8,10 +8,8 @@ module Rubocop
8
8
  MSG = 'Prefer has_many :through to has_and_belongs_to_many.'
9
9
 
10
10
  def on_send(node)
11
- receiver, method_name, *_args = *node
12
-
13
- if receiver.nil? && method_name == :has_and_belongs_to_many
14
- convention(node, :selector)
11
+ if command?(:has_and_belongs_to_many, node)
12
+ add_offence(node, :selector)
15
13
  end
16
14
  end
17
15
  end
@@ -18,7 +18,7 @@ module Rubocop
18
18
  receiver, method_name, *_args = *node
19
19
 
20
20
  if receiver.nil? && BLACKLIST.include?(method_name)
21
- convention(node, :selector)
21
+ add_offence(node, :selector)
22
22
  end
23
23
  end
24
24
 
@@ -19,7 +19,7 @@ module Rubocop
19
19
  receiver, method_name, *_args = *node
20
20
 
21
21
  if receiver.nil? && method_name == :read_attribute
22
- convention(node, :selector)
22
+ add_offence(node, :selector)
23
23
  end
24
24
  end
25
25
  end