Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a false positive for Style/FetchEnvVar in the body with assignment method #10670

Merged
merged 1 commit into from
May 29, 2022

Conversation

ydah
Copy link
Member

@ydah ydah commented May 26, 2022

Follow up: #10653 (comment)

if ENV['X'] = x
  puts ENV['X']
end

# The above is corrected into,

if ENV['X'] = x
  puts ENV.fetch('X', nil)
end

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
    * [ ] Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.


def used_in_condition_with_assignment?(node, condition)
if condition.send_type? && condition.assignment_method?
node.child_nodes == node.child_nodes & condition.child_nodes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this condition necessary? If the condition can be eliminated, the following looks simple.

diff --git a/lib/rubocop/cop/style/fetch_env_var.rb b/lib/rubocop/cop/style/fetch_env_var.rb
index 06b3fdfc4..5e8bb5247 100644
--- a/lib/rubocop/cop/style/fetch_env_var.rb
+++ b/lib/rubocop/cop/style/fetch_env_var.rb
@@ -112,22 +112,12 @@ module RuboCop
         end

         def used_in_condition?(node, condition)
-          if condition.send_type? &&
-             (!condition.comparison_method? &&
-              !condition.predicate_method? &&
-              !condition.assignment_method?)
-            return false
+          if condition.send_type?
+            return true if condition.assignment_method?
+            return false if !condition.comparison_method? && !condition.predicate_method?
           end

-          used_in_condition_with_assignment?(node, condition) || condition.child_nodes.any?(node)
-        end
-
-        def used_in_condition_with_assignment?(node, condition)
-          if condition.send_type? && condition.assignment_method?
-            node.child_nodes == node.child_nodes & condition.child_nodes
-          else
-            false
-          end
+          condition.child_nodes.any?(node)
         end

         def offensive?(node)

So just update used_in_condition? method.

         def used_in_condition?(node, condition)
           if condition.send_type?
             return true if condition.assignment_method?
             return false if !condition.comparison_method? && !condition.predicate_method?
           end

           condition.child_nodes.any?(node)
         end

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much.
I've modified the conditional statement. However, the following conditional statement is still needed:

node.child_nodes == node.child_nodes & condition.child_nodes

The following code cannot be considered offensive:

if ENV['X'] = x
  puts ENV['Y']
end

@ydah ydah requested a review from koic May 26, 2022 21:13
@ydah
Copy link
Member Author

ydah commented May 26, 2022

I updated this PR. Thank you so much.

@bbatsov bbatsov merged commit 9476274 into rubocop:master May 29, 2022
@bbatsov
Copy link
Collaborator

bbatsov commented May 29, 2022

It's funny how many edge cases something that seemed so simply ended up having. :) Thanks!

@ydah ydah deleted the fix_fetch_env_var branch May 29, 2022 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants