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

[#1242] fix diamond operator false positive warning #1243

Merged
merged 2 commits into from Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -101,8 +101,7 @@ public void visitToken(final DetailAST node) {
instance = assign.getFirstChild().getFirstChild();
}
if (instance != null && instance.getType() == TokenTypes.LITERAL_NEW
&& DiamondOperatorCheck.isNotObjectBlock(instance)
&& DiamondOperatorCheck.isNotArray(instance)) {
&& DiamondOperatorCheck.validUsage(instance)) {
final DetailAST type =
DiamondOperatorCheck.findFirstChildNodeOfType(
instance, TokenTypes.TYPE_ARGUMENTS
Expand All @@ -113,6 +112,18 @@ public void visitToken(final DetailAST node) {
}
}

/**
* Checks if diamond is not required.
*
* @param node Node
* @return True if not array
*/
private static boolean validUsage(final DetailAST node) {
return DiamondOperatorCheck.isNotObjectBlock(node)
&& DiamondOperatorCheck.isNotArray(node)
&& !DiamondOperatorCheck.isInitUsingDiamond(node);
}

/**
* Checks if node is not array.
*
Expand All @@ -133,6 +144,44 @@ private static boolean isNotObjectBlock(final DetailAST node) {
return node.getLastChild().getType() != TokenTypes.OBJBLOCK;
}

/**
* Checks if node has initialization with diamond operator.
*
* @param node Node
* @return True if not object block
*/
private static boolean isInitUsingDiamond(final DetailAST node) {
final DetailAST init = node.findFirstToken(TokenTypes.ELIST);
boolean typed = false;
if (init != null) {
final DetailAST inst = DiamondOperatorCheck.secondChild(init);
if (inst != null && inst.getType() == TokenTypes.LITERAL_NEW) {
typed =
DiamondOperatorCheck.isDiamondOperatorUsed(
inst.findFirstToken(TokenTypes.TYPE_ARGUMENTS)
);
}
}
return typed;
}

/**
* Checks if node has initialization with diamond operator.
*
* @param node Node
* @return True if not object block
*/
private static DetailAST secondChild(final DetailAST node) {
DetailAST result = null;
if (node != null) {
final DetailAST first = node.getFirstChild();
if (first != null) {
result = first.getFirstChild();
}
}
return result;
}

/**
* Checks if node contains empty set of type parameters and
* comprises angle brackets only (<>).
Expand Down
Expand Up @@ -5,6 +5,9 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.cactoos.map.MapEntry;
import org.cactoos.map.MapOf;

/**
* Better to use diamond operator where possible.
Expand Down Expand Up @@ -33,6 +36,16 @@ public static void innerClassUsage() {
new SimpleInterface.InnerClass<>();
}

/**
* Correct non-diamonds, types required for inner entity.
*/
public static void foo() {
final Map<String, String> params = new MapOf<String, String>(
new MapEntry<>("a", "foo"),
new MapEntry<>("b", "foo")
);
}

/**
* Simple interface, used as wrapper.
* @since 1.0
Expand Down
1 change: 1 addition & 0 deletions qulice-pmd/src/main/resources/com/qulice/pmd/ruleset.xml
Expand Up @@ -50,6 +50,7 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<exclude name="DefaultPackage"/>
<exclude name="LinguisticNaming"/>
<exclude name="CallSuperInConstructor"/>
<exclude name="UseDiamondOperator"/>
</rule>
<rule ref="category/java/design.xml">
<exclude name="LoosePackageCoupling"/>
Expand Down