From 134068f54a1ad1af85105860ff8672efb6feabfa Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 12 Sep 2020 15:59:57 +0900 Subject: [PATCH] [Fix #8704] Fix an error for `Lint/AmbiguousOperator` Fixes #8704. This PR fixes an error for `Lint/AmbiguousOperator` when using safe navigation operator with a unary operator. --- CHANGELOG.md | 1 + lib/rubocop/cop/lint/ambiguous_operator.rb | 2 ++ spec/rubocop/cop/lint/ambiguous_operator_spec.rb | 8 ++++++++ 3 files changed, 11 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe501e276fe..ae400d85be2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ * [#8683](https://github.com/rubocop-hq/rubocop/issues/8683): Make naming cops work with non-ascii characters. ([@tejasbubane][]) * [#8626](https://github.com/rubocop-hq/rubocop/issues/8626): Fix false negatives for `Lint/UselessMethodDefinition`. ([@marcandre][]) * [#8698](https://github.com/rubocop-hq/rubocop/pull/8698): Fix cache to avoid encoding exception. ([@marcandre][]) +* [#8704](https://github.com/rubocop-hq/rubocop/issues/8704): Fix an error for `Lint/AmbiguousOperator` when using safe navigation operator with a unary operator. ([@koic][]) ### Changes diff --git a/lib/rubocop/cop/lint/ambiguous_operator.rb b/lib/rubocop/cop/lint/ambiguous_operator.rb index 2df1285a861..338dc82c578 100644 --- a/lib/rubocop/cop/lint/ambiguous_operator.rb +++ b/lib/rubocop/cop/lint/ambiguous_operator.rb @@ -43,6 +43,8 @@ def on_new_investigation next unless diagnostic.reason == :ambiguous_prefix offense_node = find_offense_node_by(diagnostic) + next unless offense_node + message = message(diagnostic) add_offense( diff --git a/spec/rubocop/cop/lint/ambiguous_operator_spec.rb b/spec/rubocop/cop/lint/ambiguous_operator_spec.rb index eb30f75b2e5..c8013d438d4 100644 --- a/spec/rubocop/cop/lint/ambiguous_operator_spec.rb +++ b/spec/rubocop/cop/lint/ambiguous_operator_spec.rb @@ -194,4 +194,12 @@ end end end + + context 'when using safe navigation operator with a unary operator' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + do_something&.* -1 + RUBY + end + end end