Move to codestyle

other 'unnecessary*' rule are there
This commit is contained in:
Clément Fournier committed 2021-06-25 17:31:43 +02:00
1 parent 5a9b609645
commit a720616e2f
5 files changed
+39 -37

No files matched your search

@@ -2,7 +2,7 @@
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.bestpractices;
package net.sourceforge.pmd.lang.java.rule.codestyle;
import static net.sourceforge.pmd.util.CollectionUtil.setOf;
@@ -131,9 +131,9 @@ public class UnnecessaryConversionRule extends AbstractJavaRulechainRule {
boolean simpleConv = isReferenceSubtype(sourceType, conversionInput);
final String reason;
if (simpleConv && conversionInput.unbox() == conversionOutput) {
if (simpleConv && conversionInput.unbox().equals(conversionOutput)) {
reason = "explicit unboxing";
} else if (simpleConv && conversionInput.box() == conversionOutput) {
} else if (simpleConv && conversionInput.box().equals(conversionOutput)) {
reason = "explicit boxing";
} else if (sourceType.equals(conversionOutput)) {
reason = "boxing of boxed value";
@@ -1358,37 +1358,6 @@ class Foo{
</example>
</rule>
<rule name="UnnecessaryConversion"
language="java"
since="7.0.0"
message="Unnecessary {0}"
class="net.sourceforge.pmd.lang.java.rule.bestpractices.UnnecessaryConversionRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#unnecessaryboxing">
<description>
Reports explicit conversions that may safely be removed, either because
they would implicitly take place (eg widening a `byte` to an `int`), or
because they're semantically a noop (eg unboxing a value to rebox it immediately).
This handles widening conversion between primitives, boxing and unboxing
conversions (which since Java 5 are inserted by the compiler implicitly),
and unnecessary casts.
</description>
<priority>3</priority>
<example><![CDATA[
{
// Instead of
Integer integer = Integer.valueOf(2);
// you may just write
Integer integer = 2;
int i = integer.intValue(); // similarly for unboxing
// todo cast examples
}
]]>
</example>
</rule>
<rule name="UnusedAssignment"
language="java"
since="6.26.0"
@@ -1572,6 +1572,36 @@ public class Foo {
</example>
</rule>
<rule name="UnnecessaryConversion"
language="java"
since="7.0.0"
message="Unnecessary {0}"
class="net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryConversionRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#unnecessaryboxing">
<description>
Reports explicit conversions that may safely be removed, either because
they would implicitly take place (eg widening a `byte` to an `int`), or
because they're semantically a noop (eg unboxing a value to rebox it immediately).
This handles widening conversion between primitives, boxing and unboxing
conversions (which since Java 5 are inserted by the compiler implicitly).
Unnecessary casts that command a conversion are reported by {% rule UnnecessaryCast %}
instead.
</description>
<priority>3</priority>
<example><![CDATA[
{
// Instead of
Integer integer = Integer.valueOf(2);
// you may just write
Integer integer = 2;
int i = integer.intValue(); // similarly for unboxing
}
]]>
</example>
</rule>
<rule name="UnnecessaryFullyQualifiedName"
language="java"
since="5.0"
@@ -1,8 +1,8 @@
/**
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.bestpractices;
package net.sourceforge.pmd.lang.java.rule.codestyle;
import net.sourceforge.pmd.testframework.PmdRuleTst;
@@ -144,7 +144,10 @@ public class Foo {
<test-code>
<!-- Casts are left to UnnecessaryCast -->
<description>Unnecessary (primitive -> primitive) casts</description>
<expected-problems>0</expected-problems>
<expected-problems>1</expected-problems>
<expected-messages>
<message>Unnecessary explicit boxing</message>
</expected-messages>
<code><![CDATA[
class Scratch {
public static void main(String[] args) {