From 7dedfb3e63782111fd796a7a8bb1943d06766ebf Mon Sep 17 00:00:00 2001 From: Mateus Pereira Date: Mon, 23 Oct 2023 15:57:38 -0300 Subject: [PATCH] Add `Tempfile.create` to ignored iterators list --- docs/defaults.reek.yml | 1 + lib/reek/smell_detectors/nested_iterators.rb | 7 ++++--- spec/reek/smell_detectors/nested_iterators_spec.rb | 12 ++++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/docs/defaults.reek.yml b/docs/defaults.reek.yml index 7cb7d6168..49ec150f2 100644 --- a/docs/defaults.reek.yml +++ b/docs/defaults.reek.yml @@ -57,6 +57,7 @@ detectors: max_allowed_nesting: 1 ignore_iterators: - tap + - Tempfile.create NilCheck: enabled: true exclude: [] diff --git a/lib/reek/smell_detectors/nested_iterators.rb b/lib/reek/smell_detectors/nested_iterators.rb index 717253248..35bcfd7ad 100644 --- a/lib/reek/smell_detectors/nested_iterators.rb +++ b/lib/reek/smell_detectors/nested_iterators.rb @@ -26,7 +26,7 @@ def line # The name of the config field that sets the names of any # methods for which nesting should not be considered IGNORE_ITERATORS_KEY = 'ignore_iterators' - DEFAULT_IGNORE_ITERATORS = ['tap'].freeze + DEFAULT_IGNORE_ITERATORS = ['tap', 'Tempfile.create'].freeze def self.default_config super.merge( @@ -128,8 +128,9 @@ def max_allowed_nesting # @quality :reek:FeatureEnvy def ignored_iterator?(exp) - ignore_iterators.any? { |pattern| /#{pattern}/ =~ exp.call.name } || - exp.without_block_arguments? + ignore_iterators.any? do |pattern| + /#{pattern}/ =~ exp.call.format_to_ruby + end || exp.without_block_arguments? end end end diff --git a/spec/reek/smell_detectors/nested_iterators_spec.rb b/spec/reek/smell_detectors/nested_iterators_spec.rb index 453b29f4d..44ab7356c 100644 --- a/spec/reek/smell_detectors/nested_iterators_spec.rb +++ b/spec/reek/smell_detectors/nested_iterators_spec.rb @@ -66,6 +66,18 @@ def alfa(bravo) expect(src).not_to reek_of(:NestedIterators) end + it 'does not report nested iterators for Tempfile#create' do + src = <<-RUBY + def alfa(bravo) + Tempfile.create('bravo', '.') do |charlie| + charlie.each { |delta| delta } + end + end + RUBY + + expect(src).not_to reek_of(:NestedIterators) + end + it 'does not report method with successive iterators' do src = <<-RUBY def alfa