rubocop 1.65.0 → 1.65.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rubocop/cop/cop.rb +2 -2
- data/lib/rubocop/cop/layout/assignment_indentation.rb +3 -2
- data/lib/rubocop/cop/layout/condition_position.rb +0 -4
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +0 -2
- data/lib/rubocop/cop/lint/ambiguous_operator.rb +0 -2
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +0 -2
- data/lib/rubocop/cop/lint/boolean_symbol.rb +0 -2
- data/lib/rubocop/cop/lint/circular_argument_reference.rb +0 -13
- data/lib/rubocop/cop/lint/debugger.rb +0 -4
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +0 -10
- data/lib/rubocop/cop/lint/duplicate_case_condition.rb +0 -4
- data/lib/rubocop/cop/lint/duplicate_hash_key.rb +0 -4
- data/lib/rubocop/cop/lint/duplicate_methods.rb +0 -10
- data/lib/rubocop/cop/lint/each_with_object_argument.rb +0 -4
- data/lib/rubocop/cop/lint/else_layout.rb +0 -2
- data/lib/rubocop/cop/lint/empty_ensure.rb +1 -11
- data/lib/rubocop/cop/lint/empty_interpolation.rb +0 -4
- data/lib/rubocop/cop/lint/empty_when.rb +0 -2
- data/lib/rubocop/cop/lint/ensure_return.rb +1 -6
- data/lib/rubocop/cop/lint/float_comparison.rb +3 -1
- data/lib/rubocop/cop/lint/float_out_of_range.rb +0 -4
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +0 -10
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -5
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +0 -7
- data/lib/rubocop/cop/lint/interpolation_check.rb +0 -4
- data/lib/rubocop/cop/lint/literal_assignment_in_condition.rb +1 -1
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +0 -4
- data/lib/rubocop/cop/lint/loop.rb +6 -12
- data/lib/rubocop/cop/lint/nested_method_definition.rb +0 -6
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +0 -4
- data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +0 -5
- data/lib/rubocop/cop/lint/percent_string_array.rb +0 -4
- data/lib/rubocop/cop/lint/percent_symbol_array.rb +0 -4
- data/lib/rubocop/cop/lint/rand_one.rb +0 -4
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +3 -1
- data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +0 -4
- data/lib/rubocop/cop/lint/require_parentheses.rb +0 -4
- data/lib/rubocop/cop/lint/rescue_exception.rb +0 -4
- data/lib/rubocop/cop/lint/return_in_void_context.rb +0 -2
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +0 -4
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +6 -10
- data/lib/rubocop/cop/lint/unified_integer.rb +0 -4
- data/lib/rubocop/cop/lint/unreachable_code.rb +0 -5
- data/lib/rubocop/cop/lint/useless_assignment.rb +1 -5
- data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +0 -4
- data/lib/rubocop/cop/lint/useless_setter_call.rb +0 -4
- data/lib/rubocop/cop/naming/predicate_name.rb +52 -26
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +10 -1
- data/lib/rubocop/cop/style/def_with_parentheses.rb +0 -2
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +0 -1
- data/lib/rubocop/cop/style/eval_with_location.rb +12 -11
- data/lib/rubocop/cop/style/file_read.rb +2 -5
- data/lib/rubocop/cop/style/file_write.rb +2 -5
- data/lib/rubocop/cop/style/global_std_stream.rb +7 -1
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +0 -1
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +2 -2
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/missing_else.rb +0 -4
- data/lib/rubocop/cop/style/multiline_when_then.rb +0 -4
- data/lib/rubocop/cop/style/redundant_condition.rb +0 -1
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +8 -24
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +21 -2
- data/lib/rubocop/cop/style/while_until_do.rb +0 -2
- data/lib/rubocop/cop/style/while_until_modifier.rb +0 -1
- data/lib/rubocop/cop/style/zero_length_predicate.rb +5 -1
- data/lib/rubocop/cop/variable_force.rb +13 -1
- data/lib/rubocop/core_ext/string.rb +2 -6
- data/lib/rubocop/ext/regexp_node.rb +9 -31
- data/lib/rubocop/options.rb +3 -1
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +5 -0
- metadata +7 -7
@@ -9,16 +9,12 @@ module RuboCop
|
|
9
9
|
# @example
|
10
10
|
#
|
11
11
|
# # bad
|
12
|
-
#
|
13
12
|
# rand 1
|
14
13
|
# Kernel.rand(-1)
|
15
14
|
# rand 1.0
|
16
15
|
# rand(-1.0)
|
17
16
|
#
|
18
|
-
# @example
|
19
|
-
#
|
20
17
|
# # good
|
21
|
-
#
|
22
18
|
# 0 # just use 0 instead
|
23
19
|
class RandOne < Base
|
24
20
|
MSG = '`%<method>s` always returns `0`. Perhaps you meant `rand(2)` or `rand`?'
|
@@ -14,14 +14,16 @@ module RuboCop
|
|
14
14
|
#
|
15
15
|
# When comment enables all cops at once `rubocop:enable all`
|
16
16
|
# that cop checks whether any cop was actually enabled.
|
17
|
+
#
|
17
18
|
# @example
|
19
|
+
#
|
18
20
|
# # bad
|
19
21
|
# foo = 1
|
20
22
|
# # rubocop:enable Layout/LineLength
|
21
23
|
#
|
22
24
|
# # good
|
23
25
|
# foo = 1
|
24
|
-
#
|
26
|
+
#
|
25
27
|
# # bad
|
26
28
|
# # rubocop:disable Style/StringLiterals
|
27
29
|
# foo = "1"
|
@@ -9,16 +9,12 @@ module RuboCop
|
|
9
9
|
# @example
|
10
10
|
#
|
11
11
|
# # bad
|
12
|
-
#
|
13
12
|
# "result is #{something.to_s}"
|
14
13
|
# print something.to_s
|
15
14
|
# puts something.to_s
|
16
15
|
# warn something.to_s
|
17
16
|
#
|
18
|
-
# @example
|
19
|
-
#
|
20
17
|
# # good
|
21
|
-
#
|
22
18
|
# "result is #{something}"
|
23
19
|
# print something
|
24
20
|
# puts something
|
@@ -12,17 +12,16 @@ module RuboCop
|
|
12
12
|
# because `Ractor` should not access outer variables.
|
13
13
|
# eg. following style is encouraged:
|
14
14
|
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
15
|
+
# [source,ruby]
|
16
|
+
# ----
|
17
|
+
# worker_id, pipe = env
|
18
|
+
# Ractor.new(worker_id, pipe) do |worker_id, pipe|
|
19
|
+
# end
|
20
|
+
# ----
|
21
21
|
#
|
22
22
|
# @example
|
23
23
|
#
|
24
24
|
# # bad
|
25
|
-
#
|
26
25
|
# def some_method
|
27
26
|
# foo = 1
|
28
27
|
#
|
@@ -31,10 +30,7 @@ module RuboCop
|
|
31
30
|
# end
|
32
31
|
# end
|
33
32
|
#
|
34
|
-
# @example
|
35
|
-
#
|
36
33
|
# # good
|
37
|
-
#
|
38
34
|
# def some_method
|
39
35
|
# foo = 1
|
40
36
|
#
|
@@ -10,14 +10,12 @@ module RuboCop
|
|
10
10
|
# @example
|
11
11
|
#
|
12
12
|
# # bad
|
13
|
-
#
|
14
13
|
# def some_method
|
15
14
|
# return
|
16
15
|
# do_something
|
17
16
|
# end
|
18
17
|
#
|
19
18
|
# # bad
|
20
|
-
#
|
21
19
|
# def some_method
|
22
20
|
# if cond
|
23
21
|
# return
|
@@ -27,10 +25,7 @@ module RuboCop
|
|
27
25
|
# do_something
|
28
26
|
# end
|
29
27
|
#
|
30
|
-
# @example
|
31
|
-
#
|
32
28
|
# # good
|
33
|
-
#
|
34
29
|
# def some_method
|
35
30
|
# do_something
|
36
31
|
# end
|
@@ -28,16 +28,12 @@ module RuboCop
|
|
28
28
|
# @example
|
29
29
|
#
|
30
30
|
# # bad
|
31
|
-
#
|
32
31
|
# def some_method
|
33
32
|
# some_var = 1
|
34
33
|
# do_something
|
35
34
|
# end
|
36
35
|
#
|
37
|
-
# @example
|
38
|
-
#
|
39
36
|
# # good
|
40
|
-
#
|
41
37
|
# def some_method
|
42
38
|
# some_var = 1
|
43
39
|
# do_something(some_var)
|
@@ -61,7 +57,7 @@ module RuboCop
|
|
61
57
|
def check_for_unused_assignments(variable)
|
62
58
|
return if variable.should_be_unused?
|
63
59
|
|
64
|
-
variable.assignments.
|
60
|
+
variable.assignments.reverse_each do |assignment|
|
65
61
|
next if assignment.used? || part_of_ignored_node?(assignment.node)
|
66
62
|
|
67
63
|
message = message_for_useless_assignment(assignment)
|
@@ -3,59 +3,75 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Naming
|
6
|
-
# Checks that predicate
|
6
|
+
# Checks that predicate method names end with a question mark and
|
7
7
|
# do not start with a forbidden prefix.
|
8
8
|
#
|
9
|
-
# A method is determined to be a predicate method if its name starts
|
10
|
-
#
|
11
|
-
#
|
12
|
-
# Any method name that starts with one of these prefixes is required by
|
13
|
-
# the cop to end with a `?`. Other methods can be allowed by adding to
|
14
|
-
# the `AllowedMethods` configuration.
|
9
|
+
# A method is determined to be a predicate method if its name starts with
|
10
|
+
# one of the prefixes listed in the `NamePrefix` configuration. The list
|
11
|
+
# defaults to `is_`, `has_`, and `have_` but may be overridden.
|
15
12
|
#
|
16
|
-
#
|
13
|
+
# Predicate methods must end with a question mark.
|
17
14
|
#
|
18
|
-
#
|
19
|
-
#
|
15
|
+
# When `ForbiddenPrefixes` is also set (as it is by default), predicate
|
16
|
+
# methods which begin with a forbidden prefix are not allowed, even if
|
17
|
+
# they end with a `?`. These methods should be changed to remove the
|
18
|
+
# prefix.
|
20
19
|
#
|
21
|
-
#
|
22
|
-
# will register an offense only due to the lack of question mark (and will be
|
23
|
-
# autocorrected to `is_foo?`). If `ForbiddenPrefixes` contains `is_`,
|
24
|
-
# `is_foo` will register an offense both because the ? is missing and because of
|
25
|
-
# the `is_` prefix, and will be corrected to `foo?`.
|
26
|
-
#
|
27
|
-
# NOTE: `ForbiddenPrefixes` is only applied to prefixes in `NamePrefix`;
|
28
|
-
# a prefix in the former but not the latter will not be considered by
|
29
|
-
# this cop.
|
30
|
-
#
|
31
|
-
# @example
|
20
|
+
# @example NamePrefix: ['is_', 'has_', 'have_'] (default)
|
32
21
|
# # bad
|
33
22
|
# def is_even(value)
|
34
23
|
# end
|
35
24
|
#
|
36
|
-
#
|
25
|
+
# # When ForbiddenPrefixes: ['is_', 'has_', 'have_'] (default)
|
26
|
+
# # good
|
27
|
+
# def even?(value)
|
37
28
|
# end
|
38
29
|
#
|
30
|
+
# # When ForbiddenPrefixes: []
|
39
31
|
# # good
|
40
|
-
# def
|
32
|
+
# def is_even?(value)
|
41
33
|
# end
|
42
34
|
#
|
35
|
+
# @example NamePrefix: ['seems_to_be_']
|
43
36
|
# # bad
|
44
|
-
# def
|
37
|
+
# def seems_to_be_even(value)
|
45
38
|
# end
|
46
39
|
#
|
47
|
-
#
|
40
|
+
# # When ForbiddenPrefixes: ['seems_to_be_']
|
41
|
+
# # good
|
42
|
+
# def even?(value)
|
48
43
|
# end
|
49
44
|
#
|
45
|
+
# # When ForbiddenPrefixes: []
|
50
46
|
# # good
|
51
|
-
# def value
|
47
|
+
# def seems_to_be_even?(value)
|
52
48
|
# end
|
53
49
|
#
|
54
50
|
# @example AllowedMethods: ['is_a?'] (default)
|
51
|
+
# # Despite starting with the `is_` prefix, this method is allowed
|
55
52
|
# # good
|
56
53
|
# def is_a?(value)
|
57
54
|
# end
|
58
55
|
#
|
56
|
+
# @example AllowedMethods: ['is_even?']
|
57
|
+
# # good
|
58
|
+
# def is_even?(value)
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# @example MethodDefinitionMacros: ['define_method', 'define_singleton_method'] (default)
|
62
|
+
# # bad
|
63
|
+
# define_method(:is_even) { |value| }
|
64
|
+
#
|
65
|
+
# # good
|
66
|
+
# define_method(:even?) { |value| }
|
67
|
+
#
|
68
|
+
# @example MethodDefinitionMacros: ['def_node_matcher']
|
69
|
+
# # bad
|
70
|
+
# def_node_matcher(:is_even) { |value| }
|
71
|
+
#
|
72
|
+
# # good
|
73
|
+
# # def_node_matcher(:even?) { |value| }
|
74
|
+
#
|
59
75
|
class PredicateName < Base
|
60
76
|
include AllowedMethods
|
61
77
|
|
@@ -93,6 +109,16 @@ module RuboCop
|
|
93
109
|
end
|
94
110
|
alias on_defs on_def
|
95
111
|
|
112
|
+
def validate_config
|
113
|
+
forbidden_prefixes.each do |forbidden_prefix|
|
114
|
+
next if predicate_prefixes.include?(forbidden_prefix)
|
115
|
+
|
116
|
+
raise ValidationError, <<~MSG.chomp
|
117
|
+
The `Naming/PredicateName` cop is misconfigured. Prefix #{forbidden_prefix} must be included in NamePrefix because it is included in ForbiddenPrefixes.
|
118
|
+
MSG
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
96
122
|
private
|
97
123
|
|
98
124
|
def allowed_method_name?(method_name, prefix)
|
@@ -113,13 +113,21 @@ module RuboCop
|
|
113
113
|
end
|
114
114
|
end
|
115
115
|
|
116
|
+
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
116
117
|
def correct_node(corrector, node, offending_name, preferred_name)
|
117
118
|
return unless node
|
118
119
|
|
119
120
|
node.each_node(:lvar, :lvasgn, :masgn) do |child_node|
|
120
121
|
next unless variable_name_matches?(child_node, offending_name)
|
121
122
|
|
122
|
-
|
123
|
+
if child_node.lvar_type?
|
124
|
+
parent_node = child_node.parent
|
125
|
+
if parent_node.respond_to?(:value_omission?) && parent_node.value_omission?
|
126
|
+
corrector.insert_after(parent_node.loc.operator, " #{preferred_name}")
|
127
|
+
else
|
128
|
+
corrector.replace(child_node, preferred_name)
|
129
|
+
end
|
130
|
+
end
|
123
131
|
|
124
132
|
if child_node.masgn_type? || child_node.lvasgn_type?
|
125
133
|
correct_reassignment(corrector, child_node, offending_name, preferred_name)
|
@@ -127,6 +135,7 @@ module RuboCop
|
|
127
135
|
end
|
128
136
|
end
|
129
137
|
end
|
138
|
+
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
130
139
|
|
131
140
|
# If the exception variable is reassigned, that assignment needs to be corrected.
|
132
141
|
# Further `lvar` nodes will not be corrected though since they now refer to a
|
@@ -17,6 +17,18 @@ module RuboCop
|
|
17
17
|
# will not attempt to automatically add a binding, or add filename and
|
18
18
|
# line values.
|
19
19
|
#
|
20
|
+
# NOTE: This cop works only when a string literal is given as a code string.
|
21
|
+
# No offense is reported if a string variable is given as below:
|
22
|
+
#
|
23
|
+
# [source,ruby]
|
24
|
+
# ----
|
25
|
+
# code = <<-RUBY
|
26
|
+
# def do_something
|
27
|
+
# end
|
28
|
+
# RUBY
|
29
|
+
# eval code # not checked.
|
30
|
+
# ----
|
31
|
+
#
|
20
32
|
# @example
|
21
33
|
# # bad
|
22
34
|
# eval <<-RUBY
|
@@ -42,17 +54,6 @@ module RuboCop
|
|
42
54
|
# end
|
43
55
|
# RUBY
|
44
56
|
#
|
45
|
-
# This cop works only when a string literal is given as a code string.
|
46
|
-
# No offense is reported if a string variable is given as below:
|
47
|
-
#
|
48
|
-
# @example
|
49
|
-
# # not checked
|
50
|
-
# code = <<-RUBY
|
51
|
-
# def do_something
|
52
|
-
# end
|
53
|
-
# RUBY
|
54
|
-
# eval code
|
55
|
-
#
|
56
57
|
class EvalWithLocation < Base
|
57
58
|
extend AutoCorrector
|
58
59
|
|
@@ -6,8 +6,7 @@ module RuboCop
|
|
6
6
|
# Favor `File.(bin)read` convenience methods.
|
7
7
|
#
|
8
8
|
# @example
|
9
|
-
#
|
10
|
-
# # bad
|
9
|
+
# # bad - text mode
|
11
10
|
# File.open(filename).read
|
12
11
|
# File.open(filename, &:read)
|
13
12
|
# File.open(filename) { |f| f.read }
|
@@ -23,9 +22,7 @@ module RuboCop
|
|
23
22
|
# # good
|
24
23
|
# File.read(filename)
|
25
24
|
#
|
26
|
-
#
|
27
|
-
# ## binary mode
|
28
|
-
# # bad
|
25
|
+
# # bad - binary mode
|
29
26
|
# File.open(filename, 'rb').read
|
30
27
|
# File.open(filename, 'rb', &:read)
|
31
28
|
# File.open(filename, 'rb') do |f|
|
@@ -17,8 +17,7 @@ module RuboCop
|
|
17
17
|
# ----
|
18
18
|
#
|
19
19
|
# @example
|
20
|
-
#
|
21
|
-
# # bad
|
20
|
+
# # bad - text mode
|
22
21
|
# File.open(filename, 'w').write(content)
|
23
22
|
# File.open(filename, 'w') do |f|
|
24
23
|
# f.write(content)
|
@@ -27,9 +26,7 @@ module RuboCop
|
|
27
26
|
# # good
|
28
27
|
# File.write(filename, content)
|
29
28
|
#
|
30
|
-
#
|
31
|
-
# ## binary mode
|
32
|
-
# # bad
|
29
|
+
# # bad - binary mode
|
33
30
|
# File.open(filename, 'wb').write(content)
|
34
31
|
# File.open(filename, 'wb') do |f|
|
35
32
|
# f.write(content)
|
@@ -44,7 +44,9 @@ module RuboCop
|
|
44
44
|
PATTERN
|
45
45
|
|
46
46
|
def on_const(node)
|
47
|
-
|
47
|
+
return if namespaced?(node)
|
48
|
+
|
49
|
+
const_name = node.short_name
|
48
50
|
return unless STD_STREAMS.include?(const_name)
|
49
51
|
|
50
52
|
gvar_name = gvar_name(const_name).to_sym
|
@@ -61,6 +63,10 @@ module RuboCop
|
|
61
63
|
format(MSG, gvar_name: gvar_name(const_name), const_name: const_name)
|
62
64
|
end
|
63
65
|
|
66
|
+
def namespaced?(node)
|
67
|
+
!node.namespace.nil? && (node.relative? || !node.namespace.cbase_type?)
|
68
|
+
end
|
69
|
+
|
64
70
|
def gvar_name(const_name)
|
65
71
|
"$#{const_name.to_s.downcase}"
|
66
72
|
end
|
@@ -4,7 +4,7 @@ module RuboCop
|
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
6
|
# Enforces the presence (default) or absence of parentheses in
|
7
|
-
# method calls containing
|
7
|
+
# method calls containing arguments.
|
8
8
|
#
|
9
9
|
# In the default style (require_parentheses), macro methods are allowed.
|
10
10
|
# Additional methods can be added to the `AllowedMethods` or
|
@@ -95,30 +95,14 @@ module RuboCop
|
|
95
95
|
delimiters.include?(char)
|
96
96
|
end
|
97
97
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
char_class_depth
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
# Please remove this `else` branch when support for regexp_parser 1.8 will be dropped.
|
111
|
-
# It's for compatibility with regexp_parser 1.8 and will never be maintained.
|
112
|
-
else
|
113
|
-
def each_escape(node)
|
114
|
-
node.parsed_tree&.traverse&.reduce(0) do |char_class_depth, (event, expr)|
|
115
|
-
yield(expr.text[1], expr.start_index, !char_class_depth.zero?) if expr.type == :escape
|
116
|
-
|
117
|
-
if expr.type == :set
|
118
|
-
char_class_depth + (event == :enter ? 1 : -1)
|
119
|
-
else
|
120
|
-
char_class_depth
|
121
|
-
end
|
98
|
+
def each_escape(node)
|
99
|
+
node.parsed_tree&.traverse&.reduce(0) do |char_class_depth, (event, expr)|
|
100
|
+
yield(expr.text[1], expr.ts, !char_class_depth.zero?) if expr.type == :escape
|
101
|
+
|
102
|
+
if expr.type == :set
|
103
|
+
char_class_depth + (event == :enter ? 1 : -1)
|
104
|
+
else
|
105
|
+
char_class_depth
|
122
106
|
end
|
123
107
|
end
|
124
108
|
end
|