Skip to content

Commit

Permalink
[Fixes rubocop#8276] Metrics/CyclomaticComplexity now discounts repea…
Browse files Browse the repository at this point in the history
…ted uses of &. on same lvar receiver
  • Loading branch information
marcandre committed Jul 29, 2020
1 parent e438819 commit d942645
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -32,6 +32,7 @@
* [#8350](https://github.com/rubocop-hq/rubocop/pull/8350): Set default max line length to 120 for `Style/MultilineMethodSignature`. ([@koic][])
* [#8338](https://github.com/rubocop-hq/rubocop/pull/8338): **potentially breaking**. Config#for_department now returns only the config specified for that department; the 'Enabled' attribute is no longer calculated. ([@marcandre][])
* [#8037](https://github.com/rubocop-hq/rubocop/pull/8037): **(Breaking)** Cop `Metrics/AbcSize` now counts ||=, &&=, multiple assignments, for, yield, iterating blocks. `&.` now count as conditions too (unless repeated on the same variable). Default bumped from 15 to 16.5. ([@marcandre][])
* [#8276](https://github.com/rubocop-hq/rubocop/issues/8276): Cop `Metrics/CyclomaticComplexity` not longer counts `&.` when repeated on the same variable. ([@marcandre][])

## 0.88.0 (2020-07-13)

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/metrics/cyclomatic_complexity.rb
Expand Up @@ -42,6 +42,7 @@ class CyclomaticComplexity < Base

def complexity_score_for(node)
return 0 if iterating_block?(node) == false
return 0 if node.csend_type? && discount_for_repeated_csend?(node)

1
end
Expand Down
7 changes: 5 additions & 2 deletions lib/rubocop/cop/mixin/method_complexity.rb
Expand Up @@ -57,8 +57,11 @@ def check_complexity(node, method_name)
end

def complexity(body)
body.each_node(*self.class::COUNTED_NODES).reduce(1) do |score, node|
reset_on_lvasgn(node) if node.lvasgn_type?
body.each_node(:lvasgn, *self.class::COUNTED_NODES).reduce(1) do |score, node|
if node.lvasgn_type?
reset_on_lvasgn(node)
next score
end
score + complexity_score_for(node)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/conditional_assignment.rb
Expand Up @@ -67,7 +67,7 @@ def end_with_eq?(sym)

private

def expand_elsif(node, elsif_branches = []) # rubocop:todo Metrics/CyclomaticComplexity
def expand_elsif(node, elsif_branches = [])
return [] if node.nil? || !node.if_type? || !node.elsif?

elsif_branches << node.if_branch
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/if_inside_else.rb
Expand Up @@ -61,7 +61,7 @@ module Style
class IfInsideElse < Cop
MSG = 'Convert `if` nested inside `else` to `elsif`.'

def on_if(node) # rubocop:todo Metrics/CyclomaticComplexity
def on_if(node)
return if node.ternary? || node.unless?

else_branch = node.else_branch
Expand Down
18 changes: 16 additions & 2 deletions spec/rubocop/cop/metrics/cyclomatic_complexity_spec.rb
Expand Up @@ -212,9 +212,23 @@ def method_name
it 'registers an offense for &.' do
expect_offense(<<~RUBY)
def method_name
^^^^^^^^^^^^^^^ Cyclomatic complexity for method_name is too high. [2/1]
foo = nil
^^^^^^^^^^^^^^^ Cyclomatic complexity for method_name is too high. [3/1]
foo&.bar
foo&.bar
end
RUBY
end

it 'counts repeated &. on same untouched local variable as 1' do
expect_offense(<<~RUBY)
def method_name
^^^^^^^^^^^^^^^ Cyclomatic complexity for method_name is too high. [3/1]
var = 1
var&.foo
var&.dont_count_me
var = 2
var&.bar
var&.dont_count_me_eother
end
RUBY
end
Expand Down

0 comments on commit d942645

Please sign in to comment.