-
Notifications
You must be signed in to change notification settings - Fork 135
Add check: Use Collection.isEmpty() instead of comparing size() with 0 #2530
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
Add check: Use Collection.isEmpty() instead of comparing size() with 0 #2530
Conversation
Generate changelog in
|
baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/CollectionsIsEmpty.java
Outdated
Show resolved
Hide resolved
baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/CollectionsIsEmpty.java
Outdated
Show resolved
Hide resolved
baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/CollectionsIsEmpty.java
Outdated
Show resolved
Hide resolved
baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/CollectionsIsEmptyTest.java
Outdated
Show resolved
Hide resolved
baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/CollectionsIsEmptyTest.java
Outdated
Show resolved
Hide resolved
baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/CollectionsIsEmptyTest.java
Outdated
Show resolved
Hide resolved
baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/CollectionsIsEmpty.java
Outdated
Show resolved
Hide resolved
baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/CollectionsIsEmpty.java
Outdated
Show resolved
Hide resolved
…grate-refaster-templates-to-errorprone-rules
baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/CardinalityEqualsZero.java
Outdated
Show resolved
Hide resolved
baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/CardinalityEqualsZero.java
Outdated
Show resolved
Hide resolved
…grate-refaster-templates-to-errorprone-rules
…ub.com:palantir/gradle-baseline into migrate-refaster-templates-to-errorprone-rules
|
|
||
| private static boolean isExpressionThis(ExpressionTree tree) { | ||
| switch (tree.getKind()) { | ||
| case IDENTIFIER: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the memberselect case is technically necessary here for "qualified this" usages e.g.
class Outer {
int foo();
Inner getInner() {
return new Inner();
}
class Inner {
int foo() {
// 'return foo()' would recurse infinitely
// this allows us to reference components on an enclosing class from a non-static inner class.
return Outer.this.foo();
}
}
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah. Have not seen this before. Updated, and added a test.
carterkozak
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great tests!
|
Released 5.2.0 |
###### _excavator_ is a bot for automating changes across repositories. Changes produced by the roomba/latest-baseline-oss check. # Release Notes ## 5.2.0 | Type | Description | Link | | ---- | ----------- | ---- | | Improvement | Add check: Use Collection.isEmpty() instead of comparing size() with 0 | palantir/gradle-baseline#2530 | To enable or disable this check, please contact the maintainers of Excavator.
|
|
||
| EqualsZeroExpression equalsZeroExpression = maybeEqualsZeroExpression.get(); | ||
| ExpressionTree operand = equalsZeroExpression.operand(); | ||
| ExpressionTree collectionInstance = ASTHelpers.getReceiver(operand); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed #2543
PR palantir/tritium#1688 is failing attempting to upgrade to gradle-baseline with this change on https://github.com/palantir/tritium/blob/13799047a8b715b4ebf395de80deed78a86b734c/tritium-registry/src/main/java/com/palantir/tritium/metrics/registry/TagMap.java#L120-L123
> Task :tritium-registry:compileJava FAILED
/home/circleci/project/tritium-registry/src/main/java/com/palantir/tritium/metrics/registry/TagMap.java:123: error: An unhandled exception was thrown by the Error Prone static analysis plugin.
if (comparisonResult == 0) {
^
Please report this at https://github.com/google/error-prone/issues/new and include the following:
error-prone version: 2.18.0
BugPattern: CardinalityEqualsZero
Stack Trace:
java.lang.IllegalStateException: Expected expression 'comparisonResult' to be a method invocation or field access, but was IDENTIFIER
at com.google.errorprone.util.ASTHelpers.getReceiver(ASTHelpers.java:605)
at com.palantir.baseline.errorprone.CardinalityEqualsZero.matchBinary(CardinalityEqualsZero.java:61)
at com.google.errorprone.scanner.ErrorProneScanner.processMatchers(ErrorProneScanner.java:449)
at com.google.errorprone.scanner.ErrorProneScanner.visitBinary(ErrorProneScanner.java:512)
at com.google.errorprone.scanner.ErrorProneScanner.visitBinary(ErrorProneScanner.java:150)
Before this PR
We had a refaster template which suggested that
{implementation of Collection}.size() == 0be changed to{implementation of Collection}.isEmpty(). This was removed in this PR.After this PR
==COMMIT_MSG==
Add check: Use Collection.isEmpty() instead of comparing size() with 0
==COMMIT_MSG==