diff --git a/CHANGELOG.md b/CHANGELOG.md index 109fd81496c..343a905ddb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ * [#8654](https://github.com/rubocop-hq/rubocop/pull/8654): Fix a false positive for `Style/SafeNavigation` when checking `foo&.empty?` in a conditional. ([@koic][]) * [#8660](https://github.com/rubocop-hq/rubocop/pull/8660): Fix a false positive for `Style/ClassAndModuleChildren` when using cbase module name. ([@koic][]) * [#8664](https://github.com/rubocop-hq/rubocop/issues/8664): Fix a false positive for `Naming/BinaryOperatorParameterName` when naming multibyte character method name. ([@koic][]) +* [#8604](https://github.com/rubocop-hq/rubocop/issues/8604): Fix a false positive for `Bundler/DuplicatedGem` when gem is duplciated in condition. ([@tejasbubane][]) ### Changes diff --git a/lib/rubocop/cop/bundler/duplicated_gem.rb b/lib/rubocop/cop/bundler/duplicated_gem.rb index 67f69292c87..cb5c753eaf2 100644 --- a/lib/rubocop/cop/bundler/duplicated_gem.rb +++ b/lib/rubocop/cop/bundler/duplicated_gem.rb @@ -53,7 +53,11 @@ def duplicated_gem_nodes gem_declarations(processed_source.ast) .group_by(&:first_argument) .values - .select { |nodes| nodes.size > 1 } + .select { |nodes| nodes.size > 1 && !condition?(nodes) } + end + + def condition?(nodes) + nodes[0].parent&.if_type? && nodes[0].parent == nodes[1].parent end def register_offense(node, gem_name, line_of_first_occurrence) diff --git a/spec/rubocop/cop/bundler/duplicated_gem_spec.rb b/spec/rubocop/cop/bundler/duplicated_gem_spec.rb index a8f5785b449..b6383c523f5 100644 --- a/spec/rubocop/cop/bundler/duplicated_gem_spec.rb +++ b/spec/rubocop/cop/bundler/duplicated_gem_spec.rb @@ -51,5 +51,17 @@ GEM end end + + context 'and the gem is conditionally duplicated' do + it 'does not register an offense' do + expect_no_offenses(<<-GEM, 'Gemfile') + if Dir.exist? local + gem 'rubocop', path: local + else + gem 'rubocop', '~> 0.90.0' + end + GEM + end + end end end