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

Assignment of if result incorrectly autocorrected #2323

Closed
amw opened this issue Oct 15, 2015 · 7 comments
Closed

Assignment of if result incorrectly autocorrected #2323

amw opened this issue Oct 15, 2015 · 7 comments

Comments

@amw
Copy link

amw commented Oct 15, 2015

I got this bad autocorrect. The original code:

  def expression= value
    self[:expression_id] =
      if value.kind_of? Expression
        value.id
      else
        nil
      end
  end

Was changed to:

  def expression= value
    self[:expression_id] =
      value.id if value.is_a? Expression
end

This of course is incompatible. It makes the whole assignment conditional.

If you think this has to be corrected at all then you should use ternary operator:

  def expression= value
    self[:expression_id] = value.is_a?(Expression) ? value.id : nil
  end
@alexdowad
Copy link
Contributor

This is caused by the Style/IfUnlessModifier cop. Style/EmptyElse is also involved, but the transform it does is valid.

@swatosh
Copy link

swatosh commented Oct 27, 2015

Unless self[:expression_id] has a value that should be overwritten with nil
On Oct 26, 2015 1:56 PM, "Alex Dowad" [email protected] wrote:

This is caused by the Style/IfUnlessModifier cop. Style/EmptyElse is also
involved, but the transform it does is valid.


Reply to this email directly or view it on GitHub
#2323 (comment).

@alexdowad
Copy link
Contributor

Sorry, @swatosh, what do you mean?

@swatosh
Copy link

swatosh commented Oct 27, 2015

If self[:expression_id == 1234 before the method call, there is a code path
in the original that would change it to nil.

Sorry, @swatosh https://github.com/swatosh, what do you mean?


Reply to this email directly or view it on GitHub
#2323 (comment).

@alexdowad
Copy link
Contributor

If self[:expression_id == 1234 before the method call, there is a code path
in the original that would change it to nil.

Yes. That is the whole point of the linked PR.

Currently, RuboCop removes the code path which sets self[:expression_id] to nil. With the linked PR merged, it will not do so.

@swatosh
Copy link

swatosh commented Oct 27, 2015

Sorry, I missed the PR. I was responding to "...but the transform it does
is valid." Again, sorry for misunderstanding.
On Oct 27, 2015 12:45 PM, "Alex Dowad" [email protected] wrote:

If self[:expression_id == 1234 before the method call, there is a code path
in the original that would change it to nil.

Yes. That is the whole point of the linked PR.

Currently, RuboCop removes the code path which sets self[:expression_id]
to nil. With the linked PR merged, it will not do so.


Reply to this email directly or view it on GitHub
#2323 (comment).

@amw
Copy link
Author

amw commented Oct 27, 2015

@swatosh I think it meant that Style/EmptyElse transform is valid and only Style/IfUnlessModifier needs fixing.

alexdowad added a commit to alexdowad/rubocop that referenced this issue Oct 30, 2015
…recting in Style/IfUnlessModifier cop

...Otherwise, we could change the meaning of the "corrected" code.
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

No branches or pull requests

3 participants