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

Make IsEmptyCallOnCollections also support < and > operators #2612

Merged
merged 1 commit into from
Jan 9, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ static void method(List<String> l) {
// empty body
} else if (l.size() != 0 || 0 != l.size()) {
// empty body
} else if (l.size() > 0 || l.size() < 0) {
// empty body
} else if (0 < l.size() || 0 > l.size()) {
// empty body
}
}
}
Expand All @@ -89,6 +93,10 @@ static void method(List<String> l) {
// empty body
} else if (!l.isEmpty() || !l.isEmpty()) {
// empty body
} else if (!l.isEmpty() || l.size() < 0) {
// empty body
} else if (!l.isEmpty() || 0 > l.size()) {
// empty body
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,17 @@ public JavaVisitor<ExecutionContext> getVisitor() {

@Override
public J visitBinary(J.Binary binary, ExecutionContext ctx) {
if (binary.getOperator() == J.Binary.Type.Equal || binary.getOperator() == J.Binary.Type.NotEqual) {
if (COLLECTION_SIZE.matches(binary.getLeft()) || COLLECTION_SIZE.matches(binary.getRight())) {
if (isZero(binary.getLeft()) || isZero(binary.getRight())) {
J.MethodInvocation sizeCall = (J.MethodInvocation) (COLLECTION_SIZE.matches(binary.getLeft()) ?
binary.getLeft() : binary.getRight());
if (isZero(binary.getLeft()) || isZero(binary.getRight())) {
boolean zeroRight = isZero(binary.getRight());
J.MethodInvocation maybeSizeCall = (J.MethodInvocation) (zeroRight ? binary.getLeft() : binary.getRight());
if (binary.getOperator() == J.Binary.Type.Equal || binary.getOperator() == J.Binary.Type.NotEqual
|| zeroRight && binary.getOperator() == J.Binary.Type.GreaterThan
|| !zeroRight && binary.getOperator() == J.Binary.Type.LessThan) {
if (COLLECTION_SIZE.matches(maybeSizeCall)) {
String op = binary.getOperator() == J.Binary.Type.Equal ? "" : "!";
return (sizeCall.getSelect() == null ?
sizeCall.withTemplate(isEmptyNoReceiver, sizeCall.getCoordinates().replace(), op) :
sizeCall.withTemplate(isEmpty, sizeCall.getCoordinates().replace(), op, sizeCall.getSelect())
return (maybeSizeCall.getSelect() == null ?
maybeSizeCall.withTemplate(isEmptyNoReceiver, maybeSizeCall.getCoordinates().replace(), op) :
maybeSizeCall.withTemplate(isEmpty, maybeSizeCall.getCoordinates().replace(), op, maybeSizeCall.getSelect())
).withPrefix(binary.getPrefix());
}
}
Expand Down