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
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions qulice-pmd/src/main/resources/com/qulice/pmd/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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

</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value><![CDATA[
//EqualityExpression[
(
PrimaryExpression/PrimaryPrefix/Literal
or
PrimaryExpression/PrimaryPrefix/Name[@Image='null']
)
and
PrimaryExpression/PrimarySuffix
]
[not(./PrimaryExpression[1]/PrimaryPrefix/Literal or ./PrimaryExpression[1]/PrimaryPrefix/Name[@Image='null'])]
]]></value>
</property>
</properties>
</rule>
</ruleset>