Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #2847
- Loading branch information
Showing
6 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
.../src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UseExplicitTypesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
48 changes: 48 additions & 0 deletions
48
.../test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UseExplicitTypes.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |