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

Fixed Constants should be on the right side of comparison #1237

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

LaithAlebrahim
Copy link

@yegor256 , Please can you review this issue#716 and let me know if this what is required from the task.
I searched for all null comparison in the code and this what I found where null should be on right side of the comparsion.
Please let me know if you have any concerns .Tx

@LaithAlebrahim
Copy link
Author

@yegor256 , Could you please review this and tell me if this is what meant in task.Thanks

@yegor256
Copy link
Owner

@pnatashap can you please check this one?

@pnatashap
Copy link
Contributor

pnatashap commented Mar 19, 2024

@LaithAlebrahim you have reverted comparisons with null, while the task is about to create a new rule (the most closest from my point of view is https://docs.pmd-code.org/latest/pmd_rules_java_errorprone.html#compareobjectswithequals) to raise a validation error if you have such code.

@LaithAlebrahim
Copy link
Author

LaithAlebrahim commented Mar 21, 2024

Could you please review it and check if everything is fine .And let me know if you have any concerns.
@yegor256 @pnatashap
This the link of the exact changes (7f20258)
Thanks

Copy link
Contributor

@pnatashap pnatashap left a comment

Choose a reason for hiding this comment

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

@@ -256,4 +256,26 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
</property>
</properties>
</rule>
<rule name="ConstantsOnRightSideOfComparison" language="java" class="net.sourceforge.pmd.lang.rule.XPathRule" message="Constants should be on the right side of comparisons.">
<description>
Enforces the code style guideline that constants (null, string literals, or numbers) should appear on the right side of comparison operators to reduce the risk of accidental assignment and improve readability.
Copy link
Contributor

Choose a reason for hiding this comment

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

  • It is better to have this rule only about numbers and null values (because for string values there is another rule, that will write that you need to use equals method instead of ==) (So if you have code "some" == some, you will get the first warning to revert it, and the second - use equals and it can be confusing)
  • The same rule should be about != operation, as they are similar, so null != some should produce the same error

@LaithAlebrahim
Copy link
Author

@yegor256 @pnatashap I modified the rule to handle the specific requirments just (numeral and null with '!=' and '==' equality)
(d102ee0)
And added Todo for the testing I'll work on it soon.
Please review it and let me know if you have any concerns. Thanks

@yegor256
Copy link
Owner

@LaithAlebrahim would be great to add a test for this new feature, otherwise how do we know that it works?

@pnatashap
Copy link
Contributor

@LaithAlebrahim I have checked this rule and do not see any violations, so looks like something does not work

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