rubocop 0.39.0 → 0.40.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 (89) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +11 -1
  3. data/config/default.yml +65 -2
  4. data/config/disabled.yml +0 -28
  5. data/config/enabled.yml +40 -0
  6. data/lib/rubocop.rb +3 -0
  7. data/lib/rubocop/ast_node.rb +28 -13
  8. data/lib/rubocop/cached_data.rb +15 -2
  9. data/lib/rubocop/cli.rb +24 -8
  10. data/lib/rubocop/config.rb +3 -3
  11. data/lib/rubocop/config_loader.rb +0 -7
  12. data/lib/rubocop/cop/cop.rb +2 -2
  13. data/lib/rubocop/cop/lint/condition_position.rb +3 -1
  14. data/lib/rubocop/cop/lint/else_layout.rb +3 -2
  15. data/lib/rubocop/cop/lint/end_alignment.rb +2 -2
  16. data/lib/rubocop/cop/lint/nested_method_definition.rb +15 -9
  17. data/lib/rubocop/cop/lint/require_parentheses.rb +1 -1
  18. data/lib/rubocop/cop/lint/unused_block_argument.rb +2 -0
  19. data/lib/rubocop/cop/lint/useless_access_modifier.rb +86 -20
  20. data/lib/rubocop/cop/lint/useless_array_splat.rb +56 -0
  21. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  22. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +11 -25
  23. data/lib/rubocop/cop/mixin/if_node.rb +4 -0
  24. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +14 -12
  25. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +75 -9
  26. data/lib/rubocop/cop/mixin/on_normal_if_unless.rb +1 -1
  27. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -2
  28. data/lib/rubocop/cop/mixin/trailing_comma.rb +15 -5
  29. data/lib/rubocop/cop/performance/case_when_splat.rb +71 -44
  30. data/lib/rubocop/cop/performance/detect.rb +28 -18
  31. data/lib/rubocop/cop/performance/end_with.rb +1 -1
  32. data/lib/rubocop/cop/performance/redundant_merge.rb +29 -11
  33. data/lib/rubocop/cop/performance/start_with.rb +1 -1
  34. data/lib/rubocop/cop/performance/string_replacement.rb +39 -20
  35. data/lib/rubocop/cop/rails/action_filter.rb +1 -2
  36. data/lib/rubocop/cop/rails/date.rb +2 -5
  37. data/lib/rubocop/cop/rails/time_zone.rb +3 -6
  38. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +57 -0
  39. data/lib/rubocop/cop/style/alias.rb +10 -3
  40. data/lib/rubocop/cop/style/align_parameters.rb +8 -2
  41. data/lib/rubocop/cop/style/and_or.rb +29 -21
  42. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  43. data/lib/rubocop/cop/style/collection_methods.rb +1 -2
  44. data/lib/rubocop/cop/style/comment_indentation.rb +1 -1
  45. data/lib/rubocop/cop/style/conditional_assignment.rb +13 -7
  46. data/lib/rubocop/cop/style/empty_case_condition.rb +96 -0
  47. data/lib/rubocop/cop/style/encoding.rb +9 -5
  48. data/lib/rubocop/cop/style/extra_spacing.rb +22 -7
  49. data/lib/rubocop/cop/style/file_name.rb +7 -2
  50. data/lib/rubocop/cop/style/guard_clause.rb +18 -10
  51. data/lib/rubocop/cop/style/if_inside_else.rb +1 -1
  52. data/lib/rubocop/cop/style/if_unless_modifier.rb +2 -1
  53. data/lib/rubocop/cop/style/indentation_width.rb +7 -4
  54. data/lib/rubocop/cop/style/lambda.rb +98 -30
  55. data/lib/rubocop/cop/style/line_end_concatenation.rb +5 -0
  56. data/lib/rubocop/cop/style/multiline_array_brace_layout.rb +34 -9
  57. data/lib/rubocop/cop/style/multiline_assignment_layout.rb +2 -1
  58. data/lib/rubocop/cop/style/multiline_hash_brace_layout.rb +42 -17
  59. data/lib/rubocop/cop/style/multiline_method_call_brace_layout.rb +39 -14
  60. data/lib/rubocop/cop/style/multiline_method_definition_brace_layout.rb +36 -15
  61. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +8 -6
  62. data/lib/rubocop/cop/style/negated_while.rb +2 -1
  63. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +15 -0
  64. data/lib/rubocop/cop/style/nested_ternary_operator.rb +5 -8
  65. data/lib/rubocop/cop/style/next.rb +1 -1
  66. data/lib/rubocop/cop/style/not.rb +5 -2
  67. data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -2
  68. data/lib/rubocop/cop/style/raise_args.rb +70 -7
  69. data/lib/rubocop/cop/style/redundant_exception.rb +34 -20
  70. data/lib/rubocop/cop/style/redundant_parentheses.rb +27 -1
  71. data/lib/rubocop/cop/style/space_after_colon.rb +14 -10
  72. data/lib/rubocop/cop/style/space_after_comma.rb +5 -0
  73. data/lib/rubocop/cop/style/space_after_not.rb +3 -4
  74. data/lib/rubocop/cop/style/space_after_semicolon.rb +5 -0
  75. data/lib/rubocop/cop/style/space_around_operators.rb +2 -1
  76. data/lib/rubocop/cop/style/special_global_vars.rb +4 -2
  77. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -1
  78. data/lib/rubocop/cop/style/string_methods.rb +1 -2
  79. data/lib/rubocop/cop/style/symbol_proc.rb +7 -1
  80. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +25 -0
  81. data/lib/rubocop/cop/style/word_array.rb +50 -22
  82. data/lib/rubocop/cop/util.rb +0 -4
  83. data/lib/rubocop/formatter/clang_style_formatter.rb +38 -22
  84. data/lib/rubocop/options.rb +45 -10
  85. data/lib/rubocop/path_util.rb +2 -34
  86. data/lib/rubocop/result_cache.rb +10 -4
  87. data/lib/rubocop/runner.rb +5 -3
  88. data/lib/rubocop/version.rb +1 -1
  89. metadata +7 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dfcbbab3c5c192b9ec4f2af0d65ef6af2218196a
4
- data.tar.gz: 13951f737deacbbca91730cafca92ed861e5b4b9
3
+ metadata.gz: c54ef854713e3c180b88e86b627ef3694c44b080
4
+ data.tar.gz: be923c307bd8873232a0955f2ebc917219967994
5
5
  SHA512:
6
- metadata.gz: 938228431a9934dc8916618e35256c75bf5a95ad3387933019599ecc242a3281ce2a234f3a174793634b44bcf5043b924ee18f1949f3abfd97e67c8db1aea088
7
- data.tar.gz: 20459e73a5a9fbe29eeac14413a6f2e54253692d474e01cde4ec1155dfff8010a1202297b1d35a4d422cddc27d7d15a2f633725d1ebe6e63ffff41bf672f8114
6
+ metadata.gz: 007d8566d96c61467cfa6c700642c0cc1023781b575c6a438f5e64638d378a507886c59259db8e51e1b7d7b30be1e9e8bf5d454eeff75a735165e686ced8b85f
7
+ data.tar.gz: 908bceb1c0eb19cf3e290d9f620dc6e4a75fbc22c4bc5a749e164203a70166db544659c5cb8bfc4355207d2ae2f043358e566700032e0f887318d1bdfcf81be7
data/README.md CHANGED
@@ -42,6 +42,7 @@ specific RuboCop release.**
42
42
  - [Style](#style)
43
43
  - [Lint](#lint)
44
44
  - [Metrics](#metrics)
45
+ - [Performance](#performance)
45
46
  - [Rails](#rails)
46
47
  - [Configuration](#configuration)
47
48
  - [Inheritance](#inheritance)
@@ -60,6 +61,7 @@ specific RuboCop release.**
60
61
  - [File List Formatter](#file-list-formatter)
61
62
  - [JSON Formatter](#json-formatter)
62
63
  - [Offense Count Formatter](#offense-count-formatter)
64
+ - [Worst Offenders Formatter](#worst-offenders-formatter)
63
65
  - [HTML Formatter](#html-formatter)
64
66
  - [Compatibility](#compatibility)
65
67
  - [Editor integration](#editor-integration)
@@ -92,6 +94,7 @@ specific RuboCop release.**
92
94
  - [Logo](#logo)
93
95
  - [Contributors](#contributors)
94
96
  - [Mailing List](#mailing-list)
97
+ - [Freenode](#freenode)
95
98
  - [Changelog](#changelog)
96
99
  - [Copyright](#copyright)
97
100
 
@@ -115,7 +118,7 @@ haven't reached version 1.0 yet). To prevent an unwanted RuboCop update you
115
118
  might want to use a conservative version locking in your `Gemfile`:
116
119
 
117
120
  ```rb
118
- gem 'rubocop', '~> 0.39.0', require: false
121
+ gem 'rubocop', '~> 0.40.0', require: false
119
122
  ```
120
123
 
121
124
  ## Basic Usage
@@ -200,6 +203,13 @@ Command flag | Description
200
203
  `-s/--stdin` | Pipe source from STDIN. This is useful for editor integration.
201
204
  `--[no-]color` | Force color output on or off.
202
205
 
206
+ Default command-line options are loaded from `.rubocop` and `RUBOCOP_OPTS` and are combined with command-line options that are explicitly passed to `rubocop`.
207
+ Thus, the options have the following order of precedence (from highest to lowest):
208
+
209
+ 1. Explicit command-line options
210
+ 2. Options from `RUBOCOP_OPTS` environment variable
211
+ 3. Options from `.rubocop` file.
212
+
203
213
  ### Cops
204
214
 
205
215
  In RuboCop lingo the various checks performed on the code are called cops. There are several cop departments.
@@ -425,10 +425,11 @@ Style/EmptyLinesAroundModuleBody:
425
425
  # AutoCorrectEncodingComment must match the regex
426
426
  # /#.*coding\s?[:=]\s?(?:UTF|utf)-8/
427
427
  Style/Encoding:
428
- EnforcedStyle: always
428
+ EnforcedStyle: never
429
429
  SupportedStyles:
430
430
  - when_needed
431
431
  - always
432
+ - never
432
433
  AutoCorrectEncodingComment: '# encoding: utf-8'
433
434
 
434
435
  Style/ExtraSpacing:
@@ -590,6 +591,13 @@ Style/IndentHash:
590
591
  # But it can be overridden by setting this parameter
591
592
  IndentationWidth: ~
592
593
 
594
+ Style/Lambda:
595
+ EnforcedStyle: line_count_dependent
596
+ SupportedStyles:
597
+ - line_count_dependent
598
+ - lambda
599
+ - literal
600
+
593
601
  Style/LambdaCall:
594
602
  EnforcedStyle: call
595
603
  SupportedStyles:
@@ -630,6 +638,16 @@ Style/MethodName:
630
638
  - snake_case
631
639
  - camelCase
632
640
 
641
+ Style/MultilineArrayBraceLayout:
642
+ EnforcedStyle: symmetrical
643
+ SupportedStyles:
644
+ # symmetrical: closing brace is positioned in same way as opening brace
645
+ # new_line: closing brace is always on a new line
646
+ # same_line: closing brace is always on the same line as last element
647
+ - symmetrical
648
+ - new_line
649
+ - same_line
650
+
633
651
  Style/MultilineAssignmentLayout:
634
652
  # The types of assignments which are subject to this rule.
635
653
  SupportedTypes:
@@ -648,6 +666,26 @@ Style/MultilineAssignmentLayout:
648
666
  # for the set of supported types.
649
667
  - new_line
650
668
 
669
+ Style/MultilineHashBraceLayout:
670
+ EnforcedStyle: symmetrical
671
+ SupportedStyles:
672
+ # symmetrical: closing brace is positioned in same way as opening brace
673
+ # new_line: closing brace is always on a new line
674
+ # same_line: closing brace is always on same line as last element
675
+ - symmetrical
676
+ - new_line
677
+ - same_line
678
+
679
+ Style/MultilineMethodCallBraceLayout:
680
+ EnforcedStyle: symmetrical
681
+ SupportedStyles:
682
+ # symmetrical: closing brace is positioned in same way as opening brace
683
+ # new_line: closing brace is always on a new line
684
+ # same_line: closing brace is always on the same line as last argument
685
+ - symmetrical
686
+ - new_line
687
+ - same_line
688
+
651
689
  Style/MultilineMethodCallIndentation:
652
690
  EnforcedStyle: aligned
653
691
  SupportedStyles:
@@ -657,6 +695,16 @@ Style/MultilineMethodCallIndentation:
657
695
  # But it can be overridden by setting this parameter
658
696
  IndentationWidth: ~
659
697
 
698
+ Style/MultilineMethodDefinitionBraceLayout:
699
+ EnforcedStyle: symmetrical
700
+ SupportedStyles:
701
+ # symmetrical: closing brace is positioned in same way as opening brace
702
+ # new_line: closing brace is always on a new line
703
+ # same_line: closing brace is always on the same line as last parameter
704
+ - symmetrical
705
+ - new_line
706
+ - same_line
707
+
660
708
  Style/MultilineOperationIndentation:
661
709
  EnforcedStyle: aligned
662
710
  SupportedStyles:
@@ -715,6 +763,10 @@ Style/PredicateName:
715
763
  # should still be accepted
716
764
  NameWhitelist:
717
765
  - is_a?
766
+ # Exclude Rspec specs because there is a strong convetion to write spec
767
+ # helpers in the form of `have_something` or `be_something`.
768
+ Exclude:
769
+ - 'spec/**/*'
718
770
 
719
771
  Style/RaiseArgs:
720
772
  EnforcedStyle: exploded
@@ -864,6 +916,7 @@ Style/SymbolProc:
864
916
  # The names should be fairly unique, otherwise you'll end up ignoring lots of code.
865
917
  IgnoredMethods:
866
918
  - respond_to
919
+ - define_method
867
920
 
868
921
  Style/TrailingBlankLines:
869
922
  EnforcedStyle: final_newline
@@ -944,11 +997,17 @@ Style/VariableName:
944
997
  Style/WhileUntilModifier:
945
998
  MaxLineLength: 80
946
999
 
1000
+ # WordArray enforces how array literals of word-like strings should be expressed.
947
1001
  Style/WordArray:
948
1002
  EnforcedStyle: percent
949
1003
  SupportedStyles:
1004
+ # percent style: %w(word1 word2)
950
1005
  - percent
1006
+ # bracket style: ['word1', 'word2']
951
1007
  - brackets
1008
+ # The MinSize option causes the WordArray rule to be ignored for arrays
1009
+ # smaller than a certain size. The rule is only applied to arrays
1010
+ # whose element count is greater than or equal to MinSize.
952
1011
  MinSize: 0
953
1012
  # The regular expression WordRegex decides what is considered a word.
954
1013
  WordRegex: !ruby/regexp '/\A[\p{Word}\n\t]+\z/'
@@ -1006,7 +1065,7 @@ Lint/AssignmentInCondition:
1006
1065
  Lint/BlockAlignment:
1007
1066
  # The value `start_of_block` means that the `end` should be aligned with line
1008
1067
  # where the `do` keyword appears.
1009
- # The value `start_of_line` means it should be aligned with the whole
1068
+ # The value `start_of_line` means it should be aligned with the whole
1010
1069
  # expression's starting line.
1011
1070
  # The value `either` means both are allowed.
1012
1071
  AlignWith: either
@@ -1051,6 +1110,10 @@ Lint/UnusedMethodArgument:
1051
1110
  AllowUnusedKeywordArguments: false
1052
1111
  IgnoreEmptyMethods: true
1053
1112
 
1113
+ # Checks for unused block arguments.
1114
+ Lint/UnusedBlockArgument:
1115
+ AllowUnusedKeywordArguments: false
1116
+
1054
1117
  ##################### Performance ############################
1055
1118
 
1056
1119
  Performance/RedundantMerge:
@@ -73,39 +73,11 @@ Style/MissingElse:
73
73
  - case
74
74
  - both
75
75
 
76
- Style/MultilineArrayBraceLayout:
77
- Description: >-
78
- Checks that the closing brace in an array literal is
79
- symmetrical with respect to the opening brace and the
80
- array elements.
81
- Enabled: false
82
-
83
76
  Style/MultilineAssignmentLayout:
84
77
  Description: 'Check for a newline after the assignment operator in multi-line assignments.'
85
78
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#indent-conditional-assignment'
86
79
  Enabled: false
87
80
 
88
- Style/MultilineHashBraceLayout:
89
- Description: >-
90
- Checks that the closing brace in a hash literal is
91
- symmetrical with respect to the opening brace and the
92
- hash elements.
93
- Enabled: false
94
-
95
- Style/MultilineMethodCallBraceLayout:
96
- Description: >-
97
- Checks that the closing brace in a method call is
98
- symmetrical with respect to the opening brace and the
99
- method arguments.
100
- Enabled: false
101
-
102
- Style/MultilineMethodDefinitionBraceLayout:
103
- Description: >-
104
- Checks that the closing brace in a method definition is
105
- symmetrical with respect to the opening brace and the
106
- method parameters.
107
- Enabled: false
108
-
109
81
  Style/OptionHash:
110
82
  Description: "Don't use option hashes when you can use keyword arguments."
111
83
  Enabled: false
@@ -204,6 +204,10 @@ Style/EmptyElse:
204
204
  Description: 'Avoid empty else-clauses.'
205
205
  Enabled: true
206
206
 
207
+ Style/EmptyCaseCondition:
208
+ Description: 'Avoid empty condition in case statements.'
209
+ Enabled: true
210
+
207
211
  Style/EmptyLineBetweenDefs:
208
212
  Description: 'Use empty lines between defs.'
209
213
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#empty-lines-between-methods'
@@ -411,6 +415,13 @@ Style/ModuleFunction:
411
415
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#module-function'
412
416
  Enabled: true
413
417
 
418
+ Style/MultilineArrayBraceLayout:
419
+ Description: >-
420
+ Checks that the closing brace in an array literal is
421
+ either on the same line as the last array element, or
422
+ a new line.
423
+ Enabled: true
424
+
414
425
  Style/MultilineBlockChain:
415
426
  Description: 'Avoid multi-line chains of blocks.'
416
427
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#single-line-blocks'
@@ -420,17 +431,38 @@ Style/MultilineBlockLayout:
420
431
  Description: 'Ensures newlines after multiline block do statements.'
421
432
  Enabled: true
422
433
 
434
+ Style/MultilineHashBraceLayout:
435
+ Description: >-
436
+ Checks that the closing brace in a hash literal is
437
+ either on the same line as the last hash element, or
438
+ a new line.
439
+ Enabled: true
440
+
423
441
  Style/MultilineIfThen:
424
442
  Description: 'Do not use then for multi-line if/unless.'
425
443
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-then'
426
444
  Enabled: true
427
445
 
446
+ Style/MultilineMethodCallBraceLayout:
447
+ Description: >-
448
+ Checks that the closing brace in a method call is
449
+ either on the same line as the last method argument, or
450
+ a new line.
451
+ Enabled: true
452
+
428
453
  Style/MultilineMethodCallIndentation:
429
454
  Description: >-
430
455
  Checks indentation of method calls with the dot operator
431
456
  that span more than one line.
432
457
  Enabled: true
433
458
 
459
+ Style/MultilineMethodDefinitionBraceLayout:
460
+ Description: >-
461
+ Checks that the closing brace in a method definition is
462
+ either on the same line as the last method parameter, or
463
+ a new line.
464
+ Enabled: true
465
+
434
466
  Style/MultilineOperationIndentation:
435
467
  Description: >-
436
468
  Checks indentation of binary operations that span more than
@@ -1135,6 +1167,10 @@ Lint/UselessAccessModifier:
1135
1167
  Description: 'Checks for useless access modifiers.'
1136
1168
  Enabled: true
1137
1169
 
1170
+ Lint/UselessArraySplat:
1171
+ Description: 'Checks for useless array splats.'
1172
+ Enabled: true
1173
+
1138
1174
  Lint/UselessAssignment:
1139
1175
  Description: 'Checks for useless assignment to a local variable.'
1140
1176
  StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#underscore-unused-vars'
@@ -1347,6 +1383,10 @@ Rails/TimeZone:
1347
1383
  Reference: 'http://danilenko.org/2012/7/6/rails_timezones'
1348
1384
  Enabled: true
1349
1385
 
1386
+ Rails/UniqBeforePluck:
1387
+ Description: 'Prefer the use of uniq before pluck.'
1388
+ Enabled: true
1389
+
1350
1390
  Rails/Validation:
1351
1391
  Description: 'Use validates :attribute, hash of validations.'
1352
1392
  Enabled: true
@@ -128,6 +128,7 @@ require 'rubocop/cop/lint/unreachable_code'
128
128
  require 'rubocop/cop/lint/unused_block_argument'
129
129
  require 'rubocop/cop/lint/unused_method_argument'
130
130
  require 'rubocop/cop/lint/useless_access_modifier'
131
+ require 'rubocop/cop/lint/useless_array_splat'
131
132
  require 'rubocop/cop/lint/useless_assignment'
132
133
  require 'rubocop/cop/lint/useless_comparison'
133
134
  require 'rubocop/cop/lint/useless_else_without_rescue'
@@ -208,6 +209,7 @@ require 'rubocop/cop/style/dot_position'
208
209
  require 'rubocop/cop/style/double_negation'
209
210
  require 'rubocop/cop/style/each_with_object'
210
211
  require 'rubocop/cop/style/else_alignment'
212
+ require 'rubocop/cop/style/empty_case_condition'
211
213
  require 'rubocop/cop/style/empty_else'
212
214
  require 'rubocop/cop/style/empty_line_between_defs'
213
215
  require 'rubocop/cop/style/empty_lines'
@@ -366,6 +368,7 @@ require 'rubocop/cop/rails/pluralization_grammar'
366
368
  require 'rubocop/cop/rails/read_write_attribute'
367
369
  require 'rubocop/cop/rails/scope_args'
368
370
  require 'rubocop/cop/rails/time_zone'
371
+ require 'rubocop/cop/rails/uniq_before_pluck'
369
372
  require 'rubocop/cop/rails/validation'
370
373
 
371
374
  require 'rubocop/cop/team'
@@ -419,13 +419,13 @@ module RuboCop
419
419
  kind_filter = :"#{kind}?"
420
420
  define_method(recursive_kind) do
421
421
  case type
422
- when :begin, :pair, *OPERATOR_KEYWORDS, *COMPOSITE_LITERALS
423
- children.all?(&recursive_kind)
424
422
  when :send
425
423
  receiver, method_name, *args = *self
426
424
  COMPARISON_OPERATORS.include?(method_name) &&
427
425
  receiver.send(recursive_kind) &&
428
426
  args.all?(&recursive_kind)
427
+ when :begin, :pair, *OPERATOR_KEYWORDS, *COMPOSITE_LITERALS
428
+ children.all?(&recursive_kind)
429
429
  else
430
430
  send(kind_filter)
431
431
  end
@@ -503,7 +503,6 @@ module RuboCop
503
503
  def value_used?
504
504
  # Be conservative and return true if we're not sure
505
505
  return false if parent.nil?
506
- index = parent.children.index { |child| child.equal?(self) }
507
506
 
508
507
  case parent.type
509
508
  when :array, :defined?, :dstr, :dsym, :eflipflop, :erange, :float,
@@ -511,19 +510,13 @@ module RuboCop
511
510
  :xstr
512
511
  parent.value_used?
513
512
  when :begin, :kwbegin
514
- # the last child node determines the value of the parent
515
- index == parent.children.size - 1 ? parent.value_used? : false
513
+ begin_value_used?
516
514
  when :for
517
- # `for var in enum; body; end`
518
- # (for <var> <enum> <body>)
519
- index == 2 ? parent.value_used? : true
515
+ for_value_used?
520
516
  when :case, :if
521
- # (case <condition> <when...>)
522
- # (if <condition> <truebranch> <falsebranch>)
523
- index == 0 ? true : parent.value_used?
517
+ case_if_value_used?
524
518
  when :while, :until, :while_post, :until_post
525
- # (while <condition> <body>) -> always evaluates to `nil`
526
- index == 0
519
+ while_until_value_used?
527
520
  else
528
521
  true
529
522
  end
@@ -589,5 +582,27 @@ module RuboCop
589
582
  last_node = current_node
590
583
  end
591
584
  end
585
+
586
+ def begin_value_used?
587
+ # the last child node determines the value of the parent
588
+ sibling_index == parent.children.size - 1 ? parent.value_used? : false
589
+ end
590
+
591
+ def for_value_used?
592
+ # `for var in enum; body; end`
593
+ # (for <var> <enum> <body>)
594
+ sibling_index == 2 ? parent.value_used? : true
595
+ end
596
+
597
+ def case_if_value_used?
598
+ # (case <condition> <when...>)
599
+ # (if <condition> <truebranch> <falsebranch>)
600
+ sibling_index == 0 ? true : parent.value_used?
601
+ end
602
+
603
+ def while_until_value_used?
604
+ # (while <condition> <body>) -> always evaluates to `nil`
605
+ sibling_index == 0
606
+ end
592
607
  end
593
608
  end
@@ -47,8 +47,7 @@ module RuboCop
47
47
 
48
48
  # Restore an offense object loaded from a JSON file.
49
49
  def deserialize_offenses(offenses)
50
- source_buffer = Parser::Source::Buffer.new(@filename)
51
- source_buffer.read
50
+ source_buffer = utf8_source_buffer
52
51
  offenses.map! do |o|
53
52
  location = Parser::Source::Range.new(source_buffer,
54
53
  o['location']['begin_pos'],
@@ -61,5 +60,19 @@ module RuboCop
61
60
  o['cop_name'], o['status'].to_sym)
62
61
  end
63
62
  end
63
+
64
+ # Return a source buffer that has a UTF-8 encoded source.
65
+ def utf8_source_buffer
66
+ # We are given a source with an encoding of ASCII-8BIT
67
+ ascii_source = Parser::Source::Buffer.new(@filename).read.source
68
+
69
+ # Correct the encoding of the source as we expect UTF-8
70
+ source = ascii_source.dup.force_encoding('UTF-8')
71
+
72
+ # The existing source is immutable so we need a new object
73
+ source_buffer = Parser::Source::Buffer.new(@filename)
74
+ source_buffer.source = source
75
+ source_buffer
76
+ end
64
77
  end
65
78
  end