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 double negative conditional bug in NegatedIf and NegatedWhile style rules #3146

Merged

Conversation

natalzia-paperless
Copy link
Contributor

@natalzia-paperless natalzia-paperless commented May 18, 2016

Currently you receive a negated if/while error from Rubocop if you have a conditional that is double negated. Rubocop should have awareness of whether there is a single ! or two. In the event that your line reads something akin to if !!conditional Rubocop should only fail on the DoubleNegation rule.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • 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.

@natalzia-paperless
Copy link
Contributor Author

cc @ivantsepp

@natalzia-paperless natalzia-paperless changed the title Fix double negative conditional bug in NegatedIf and NegatedWhile style rules Fix double negative conditional bug in NegatedIf and NegatedWhile style rules May 19, 2016
@natalzia-paperless natalzia-paperless force-pushed the fix_double_negative_conditional_bug branch 3 times, most recently from 880752b to 089b385 Compare May 19, 2016 15:20
['if !!condition',
' some_method',
'end'])
expect(cop.offenses).to be_empty
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there should also be a test for modifier if usage (single-line if). Refer this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated!

@natalzia-paperless natalzia-paperless force-pushed the fix_double_negative_conditional_bug branch from 089b385 to c4409a8 Compare May 19, 2016 15:58
@tejasbubane
Copy link
Contributor

Please squash the commits @natalzia-paperless

@natalzia-paperless natalzia-paperless force-pushed the fix_double_negative_conditional_bug branch 2 times, most recently from 9d876cd to 35e4ffc Compare May 20, 2016 14:00
@natalzia-paperless
Copy link
Contributor Author

@tejasbubane squashed!


_object, method = *condition

return unless method == :! && !(node.loc.respond_to?(:else) &&
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems to method == :! should be removed now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm I think we still need this check to see if the only operator is !. the additions here only rule out a double negation, which wouldn't be enough to check for this rule.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, yeah. I got confused here. Still, it seems to me we can directly check that we're dealing with single instead of double negation and that would simplify the logic. Right now the structure of the methods seems a bit convoluted.

@bbatsov
Copy link
Collaborator

bbatsov commented May 20, 2016

I've added a couple of remarks.

@natalzia-paperless natalzia-paperless force-pushed the fix_double_negative_conditional_bug branch 2 times, most recently from 934a2a9 to 7545baf Compare May 20, 2016 19:24
@natalzia-paperless
Copy link
Contributor Author

@bbatsov pushed some new code. how do you feel about this approach? it removed the method == :! check entirely in favor of using the def_node_matcher method to ensure that there is only a single negation

@natalzia-paperless natalzia-paperless force-pushed the fix_double_negative_conditional_bug branch from 7545baf to 3478ba7 Compare May 20, 2016 19:26
Previously this file only looked at the first operator in the line
With these changes there is now a pattern match to ensure `!!`
isn't flagged by these rules

Add tests for double negation in while and if specs

Update CHANGELOG.md
@natalzia-paperless natalzia-paperless force-pushed the fix_double_negative_conditional_bug branch from 3478ba7 to a4eca96 Compare May 20, 2016 19:34
@bbatsov
Copy link
Collaborator

bbatsov commented May 20, 2016

Yep, that's much better.

@bbatsov bbatsov merged commit 98ca136 into rubocop:master May 20, 2016
Neodelf pushed a commit to Neodelf/rubocop that referenced this pull request Oct 15, 2016
…rubocop#3146)

Previously this file only looked at the first operator in the line.
With these changes there is now a pattern match to ensure `!!`
isn't flagged by these rules.
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