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