Skip to content

Commit

Permalink
[java] New Rule: Use Explicit Types
Browse files Browse the repository at this point in the history
Fixes #2847
  • Loading branch information
adangel committed May 14, 2023
1 parent e8c0d57 commit d49178a
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/pages/release_notes.md
Expand Up @@ -182,6 +182,7 @@ Contributors: [Wener](https://github.com/wener-tiobe) (@wener-tiobe)
* {% rule apex/design/UnusedMethod %} finds unused methods in your code.

**Java**
* {% rule java/bestpractices/UseExplicitTypes %} reports usages of `var` keyword, which was introduced with Java 10.
* {% rule java/codestyle/UnnecessaryBoxing %} reports boxing and unboxing conversions that may be made implicit.

**Kotlin**
Expand Down Expand Up @@ -453,6 +454,7 @@ Language specific fixes:
* [#2806](https://github.com/pmd/pmd/issues/2806): \[java] SwitchStmtsShouldHaveDefault false-positive with Java 14 switch non-fallthrough branches
* [#2822](https://github.com/pmd/pmd/issues/2822): \[java] LooseCoupling rule: Extend to cover user defined implementations and interfaces
* [#2843](https://github.com/pmd/pmd/pull/2843): \[java] Fix UnusedAssignment FP with field accesses
* [#2847](https://github.com/pmd/pmd/issues/2847): \[java] New Rule: Use Explicit Types
* [#2882](https://github.com/pmd/pmd/issues/2882): \[java] UseTryWithResources - false negative for explicit close
* [#2883](https://github.com/pmd/pmd/issues/2883): \[java] JUnitAssertionsShouldIncludeMessage false positive with method call
* [#2890](https://github.com/pmd/pmd/issues/2890): \[java] UnusedPrivateMethod false positive with generics
Expand Down
1 change: 1 addition & 0 deletions docs/pages/release_notes_pmd7.md
Expand Up @@ -537,6 +537,7 @@ Related issue: [[core] Explicitly name all language versions (#4120)](https://gi
* {% rule apex/design/UnusedMethod %} finds unused methods in your code.

**Java**
* {% rule java/bestpractices/UseExplicitTypes %} reports usages of `var` keyword, which was introduced with Java 10.
* {% rule java/codestyle/UnnecessaryBoxing %} reports boxing and unboxing conversions that may be made implicit.

**Kotlin**
Expand Down
1 change: 1 addition & 0 deletions pmd-core/src/main/resources/rulesets/releases/700.xml
Expand Up @@ -10,6 +10,7 @@ This ruleset contains links to rules that are new in PMD v7.0.0

<rule ref="category/apex/design.xml/UnusedMethod"/>

<rule ref="category/java/bestpractices.xml/UseExplicitTypes"/>
<rule ref="category/java/codestyle.xml/UnnecessaryBoxing"/>

<rule ref="category/kotlin/bestpractices.xml/FunctionNameTooShort"/>
Expand Down
28 changes: 28 additions & 0 deletions pmd-java/src/main/resources/category/java/bestpractices.xml
Expand Up @@ -1688,6 +1688,34 @@ public class Foo {
</example>
</rule>

<rule name="UseExplicitTypes"
language="java"
minimumLanguageVersion="10"
since="7.0.0"
message="Use Explicit Types"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#useexplicittypes">
<description>
Java 10 introduced the `var` keyword. This reduces the amount of typing but decreases the reading comprehension of the
code.
</description>
<priority>3</priority>
<properties>
<property name="allowLiterals" type="Boolean" value="false" description="Allow when variables are directly initialized with literals"/>
<property name="allowCtors" type="Boolean" value="false" description="Allow when variables are directly initialized with a constructor call"/>
<property name="version" value="3.1"/>
<property name="xpath">
<value>
<![CDATA[
//LocalVariableDeclaration[@TypeInferred = true()]
[not(VariableDeclarator[*[pmd-java:nodeIs("Literal")]]) or $allowLiterals = false()]
[not(VariableDeclarator[ConstructorCall]) or $allowCtors = false()]
]]>
</value>
</property>
</properties>
</rule>

<rule name="UseStandardCharsets"
language="java"
since="6.34.0"
Expand Down
@@ -0,0 +1,11 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/

package net.sourceforge.pmd.lang.java.rule.bestpractices;

import net.sourceforge.pmd.testframework.PmdRuleTst;

class UseExplicitTypesTest extends PmdRuleTst {
// no additional unit tests
}
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data
xmlns="http://pmd.sourceforge.net/rule-tests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">

<code-fragment id="basic-sample"><![CDATA[
import java.util.function.Function;
public class Foo {
public void bar() {
var s = "a"; // line 5 - violation - literal
var i = 1; // line 6 - violation - literal
String s2 = "a"; // no violation - explicit type
var f = getFoo(); // line 8 - violation
var f = new Foo(); // line 9 - violation - ctor call
Function<Integer, Integer> quadrat = (var x) -> x*x;
}
private String getFoo() {
return "a";
}
}
]]></code-fragment>

<test-code>
<description>No vars anywhere</description>
<expected-problems>4</expected-problems>
<expected-linenumbers>5,6,8,9</expected-linenumbers>
<code-ref id="basic-sample"/>
</test-code>

<test-code>
<description>Allow literals</description>
<rule-property name="allowLiterals">true</rule-property>
<expected-problems>2</expected-problems>
<expected-linenumbers>8,9</expected-linenumbers>
<code-ref id="basic-sample"/>
</test-code>

<test-code>
<description>Allow constructor calls</description>
<rule-property name="allowCtors">true</rule-property>
<expected-problems>3</expected-problems>
<expected-linenumbers>5,6,8</expected-linenumbers>
<code-ref id="basic-sample"/>
</test-code>
</test-data>

0 comments on commit d49178a

Please sign in to comment.