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

Remove comparison to true for booleans #51723

Merged
merged 1 commit into from
Feb 1, 2020

Conversation

rjernst
Copy link
Member

@rjernst rjernst commented Jan 31, 2020

While we use == false as a more visible form of boolean negation
(instead of !), the true case is implied and the true value does not
need to explicitly checked. This commit converts cases that have slipped
into the code checking for == true.

While we use `== false` as a more visible form of boolean negation
(instead of `!`), the true case is implied and the true value does not
need to explicitly checked. This commit converts cases that have slipped
into the code checking for `== true`.
@rjernst rjernst added >non-issue :Core/Infra/Core Core issues without another label v8.0.0 v7.7.0 labels Jan 31, 2020
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (:Core/Infra/Core)

Copy link
Contributor

@pugnascotia pugnascotia left a comment

Choose a reason for hiding this comment

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

LGTM.

I did a CheckStyle experiment, and you can forbid such comparisons with this:

    <!-- Forbid equality comparisons with `true` -->
    <module name="DescendantToken">
      <property name="tokens" value="EQUAL"/>
      <property name="limitedTokens" value="LITERAL_TRUE"/>
      <property name="maximumNumber" value="0"/>
      <property name="maximumDepth" value="1"/>
      <message key="descendant.token.max" value="Do not check for equality with 'true', since it is implied"/>
    </module>

The minor irritation with that is that it doesn't detect all the violations in one go, rather (1) CheckStyle finds some, (2) you fix them, (3) GOTO 1. What do you think? If it's worth creating a PR to remove these, it's probably worth preventing them from being added in the first place.

@rjernst
Copy link
Member Author

rjernst commented Feb 1, 2020

@pugnascotia If the check isn't too expensive, I do think it would be worthwhile to add. Could we also add the case to not allow ! and force == false? Please feel free to open a PR.

@rjernst rjernst merged commit bf317e8 into elastic:master Feb 1, 2020
@rjernst rjernst deleted the cleanup_booleans branch February 1, 2020 00:34
rjernst added a commit that referenced this pull request Feb 1, 2020
While we use `== false` as a more visible form of boolean negation
(instead of `!`), the true case is implied and the true value does not
need to explicitly checked. This commit converts cases that have slipped
into the code checking for `== true`.
pugnascotia added a commit to pugnascotia/elasticsearch that referenced this pull request Feb 3, 2020
Add a Checkstyle rule to forbid equality checks against a literal 'true'
value, since this is redundant. Follow up to elastic#51723.
pugnascotia added a commit that referenced this pull request Feb 4, 2020
Add a Checkstyle rule to forbid equality checks against a literal 'true'
value, since this is redundant. Follow up to #51723.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants