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

Don't flag raised to a power negative numeric literals. #3770

Merged
merged 6 commits into from
Dec 13, 2016

Conversation

amogil
Copy link
Contributor

@amogil amogil commented Dec 4, 2016

Currently (-2)**2 is flagged as "Don't use parentheses around a literal" offense. But removing the parentheses would change the meaning:

(-2)**2 #=> 4
-2**2 #=> -4

Style/RedundantParentheses fixed by adding the exception for raised to a power negative numeric literals.

rubocop -V #=> 0.46.0 (using Parser 2.3.3.1, running on ruby 2.2.2 x86_64-darwin14)


  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests are passing.
  • The new code doesn't generate RuboCop offenses.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

Removing the parentheses would change the meaning of the expressions.
@amogil amogil force-pushed the bug-numeric-literals branch from e22d7c2 to 408d2ef Compare December 4, 2016 22:39
@pocke
Copy link
Collaborator

pocke commented Dec 12, 2016

Hi

2**(-2) and 2**-2 have same result(See http://melpon.org/wandbox/permlink/B3T6F1d3hDTMLSIs ), so we can omit the parentheses, however, the change makes not to add offence the code.

For example

2**(-2)
$ rubocop
Inspecting 1 file
.

1 file inspected, no offenses detected

I think the above code should be detected by RuboCop. What do you think?

@amogil
Copy link
Contributor Author

amogil commented Dec 12, 2016

Fixed.

Sorry, the difference between node and begin_node was not clear to me.

@@ -126,8 +128,17 @@ def only_closing_paren_before_comma?(node)
line_range.source =~ /^\s*\)\s*,/
end

def disallowed_literal?(node)
node.literal? && !ALLOWED_LITERALS.include?(node.type)
def disallowed_literal?(begin_node, node)
Copy link
Collaborator

Choose a reason for hiding this comment

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

begin_node? This name sounds confusing to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've used the same convention as in other places in the file. See check, check_unary, call_chain_starts_with_int? methods.

That was wrong?

!raised_to_power_negative_numeric?(begin_node, node)
end

def raised_to_power_negative_numeric?(begin_node, node)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here.

end

def raised_to_power_negative_numeric?(begin_node, node)
return false unless node.int_type? || node.float_type?
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably we should add a numeric_type? method to the underlying class.


def raised_to_power_negative_numeric?(begin_node, node)
return false unless node.int_type? || node.float_type?
return false if node.children.first >= 0 || begin_node.parent.nil?
Copy link
Collaborator

Choose a reason for hiding this comment

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

node.children.first >= 0? I don't immediately understand what you're checking for here. I'm guessing it's whether the base number is positive of negative but it's really hard to infer from the code you've written.

return false unless node.int_type? || node.float_type?
return false if node.children.first >= 0 || begin_node.parent.nil?

begin_node.parent.children[begin_node.sibling_index + 1] == :**
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here.

Adding a few locals would certainly improve the readability of your code.

@bbatsov
Copy link
Collaborator

bbatsov commented Dec 13, 2016

I think the above code should be detected by RuboCop. What do you think?

True. There can be a lint cop for this.

- Numeric type detection moved into Node class.
- raised_to_power_negative_numeric? was refactored to improve readability.
- One more spec.
@amogil
Copy link
Contributor Author

amogil commented Dec 13, 2016

True. There can be a lint cop for this.

There was a bug and I've fixed it.

Other comments were fixed too, except one about begin_node.

@bbatsov
Copy link
Collaborator

bbatsov commented Dec 13, 2016

OK, the naming there is not that important.

@bbatsov bbatsov merged commit b7d1f32 into rubocop:master Dec 13, 2016
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.

3 participants