-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Conversation
|
||
def used_in_condition_with_assignment?(node, condition) | ||
if condition.send_type? && condition.assignment_method? | ||
node.child_nodes == node.child_nodes & condition.child_nodes |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
I updated this PR. Thank you so much. |
It's funny how many edge cases something that seemed so simply ended up having. :) Thanks! |
Follow up: #10653 (comment)
Before submitting the PR make sure the following are checked:
* [ ] Commit message starts with[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.