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
Custom path to Optional class for Optional emptiness handler #288
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1233,6 +1233,166 @@ public void OptionalEmptinessHandlerTest() { | |
.doTest(); | ||
} | ||
|
||
@Test | ||
public void OptionalEmptinessHandlerWithSingleCustomPathTest() { | ||
compilationHelper | ||
.setArgs( | ||
Arrays.asList( | ||
"-d", | ||
temporaryFolder.getRoot().getAbsolutePath(), | ||
"-XepOpt:NullAway:AnnotatedPackages=com.uber", | ||
"-XepOpt:NullAway:UnannotatedSubPackages=com.uber.lib.unannotated", | ||
"-XepOpt:NullAway:CheckOptionalEmptiness=true", | ||
"-XepOpt:NullAway:CheckOptionalEmptinessCustomClasses=com.google.common.base.Optional")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add one more (smaller) test, that checks that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done already 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (True, I missed this one, my bad 😅 ) |
||
.addSourceLines( | ||
"TestNegative.java", | ||
"package com.uber;", | ||
"import com.google.common.base.Optional;", | ||
"import javax.annotation.Nullable;", | ||
"import com.google.common.base.Function;", | ||
"public class TestNegative {", | ||
" void foo() {", | ||
" Optional<Object> a = Optional.absent();", | ||
" // no error since a.isPresent() is called", | ||
" if(a.isPresent()){", | ||
" a.get().toString();", | ||
" }", | ||
" }", | ||
" public void lambdaConsumer(Function a){", | ||
" return;", | ||
" }", | ||
" void bar() {", | ||
" Optional<Object> b = Optional.absent();", | ||
" if(b.isPresent()){", | ||
" lambdaConsumer(v -> b.get().toString());", | ||
" }", | ||
" }", | ||
"}") | ||
.addSourceLines( | ||
"TestPositive.java", | ||
"package com.uber;", | ||
"import java.util.Optional;", | ||
"import javax.annotation.Nullable;", | ||
"import com.google.common.base.Function;", | ||
"public class TestPositive {", | ||
" void foo() {", | ||
" Optional<Object> a = Optional.empty();", | ||
" // BUG: Diagnostic contains: Optional a can be empty", | ||
" a.get().toString();", | ||
" }", | ||
" public void lambdaConsumer(Function a){", | ||
" return;", | ||
" }", | ||
" void bar() {", | ||
" Optional<Object> b = Optional.empty();", | ||
" // BUG: Diagnostic contains: Optional b can be empty", | ||
" lambdaConsumer(v -> b.get().toString());", | ||
" }", | ||
"}") | ||
.addSourceLines( | ||
"TestPositive2.java", | ||
"package com.uber;", | ||
"import com.google.common.base.Optional;", | ||
"import javax.annotation.Nullable;", | ||
"import com.google.common.base.Function;", | ||
"public class TestPositive2 {", | ||
" void foo() {", | ||
" Optional<Object> a = Optional.absent();", | ||
" // BUG: Diagnostic contains: Optional a can be empty", | ||
" a.get().toString();", | ||
" }", | ||
" public void lambdaConsumer(Function a){", | ||
" return;", | ||
" }", | ||
" void bar() {", | ||
" Optional<Object> b = Optional.absent();", | ||
" // BUG: Diagnostic contains: Optional b can be empty", | ||
" lambdaConsumer(v -> b.get().toString());", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void OptionalEmptinessHandlerWithTwoCustomPathsTest() { | ||
compilationHelper | ||
.setArgs( | ||
Arrays.asList( | ||
"-d", | ||
temporaryFolder.getRoot().getAbsolutePath(), | ||
"-XepOpt:NullAway:AnnotatedPackages=com.uber", | ||
"-XepOpt:NullAway:UnannotatedSubPackages=com.uber.lib.unannotated", | ||
"-XepOpt:NullAway:CheckOptionalEmptiness=true", | ||
"-XepOpt:NullAway:CheckOptionalEmptinessCustomClasses=does.not.matter.Optional,com.google.common.base.Optional")) | ||
.addSourceLines( | ||
"TestNegative.java", | ||
"package com.uber;", | ||
"import com.google.common.base.Optional;", | ||
"import javax.annotation.Nullable;", | ||
"import com.google.common.base.Function;", | ||
"public class TestNegative {", | ||
" void foo() {", | ||
" Optional<Object> a = Optional.absent();", | ||
" // no error since a.isPresent() is called", | ||
" if(a.isPresent()){", | ||
" a.get().toString();", | ||
" }", | ||
" }", | ||
" public void lambdaConsumer(Function a){", | ||
" return;", | ||
" }", | ||
" void bar() {", | ||
" Optional<Object> b = Optional.absent();", | ||
" if(b.isPresent()){", | ||
" lambdaConsumer(v -> b.get().toString());", | ||
" }", | ||
" }", | ||
"}") | ||
.addSourceLines( | ||
"TestPositive.java", | ||
"package com.uber;", | ||
"import java.util.Optional;", | ||
"import javax.annotation.Nullable;", | ||
"import com.google.common.base.Function;", | ||
"public class TestPositive {", | ||
" void foo() {", | ||
" Optional<Object> a = Optional.empty();", | ||
" // BUG: Diagnostic contains: Optional a can be empty", | ||
" a.get().toString();", | ||
" }", | ||
" public void lambdaConsumer(Function a){", | ||
" return;", | ||
" }", | ||
" void bar() {", | ||
" Optional<Object> b = Optional.empty();", | ||
" // BUG: Diagnostic contains: Optional b can be empty", | ||
" lambdaConsumer(v -> b.get().toString());", | ||
" }", | ||
"}") | ||
.addSourceLines( | ||
"TestPositive2.java", | ||
"package com.uber;", | ||
"import com.google.common.base.Optional;", | ||
"import javax.annotation.Nullable;", | ||
"import com.google.common.base.Function;", | ||
"public class TestPositive2 {", | ||
" void foo() {", | ||
" Optional<Object> a = Optional.absent();", | ||
" // BUG: Diagnostic contains: Optional a can be empty", | ||
" a.get().toString();", | ||
" }", | ||
" public void lambdaConsumer(Function a){", | ||
" return;", | ||
" }", | ||
" void bar() {", | ||
" Optional<Object> b = Optional.absent();", | ||
" // BUG: Diagnostic contains: Optional b can be empty", | ||
" lambdaConsumer(v -> b.get().toString());", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void OptionalEmptinessUncheckedTest() { | ||
compilationHelper | ||
|
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.
Guava 22 has collectors: https://google.github.io/guava/releases/22.0/api/docs/com/google/common/collect/ImmutableSet.html#toImmutableSet--
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.
So no need to build a Java set and call
copyOf(...)
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.
Thanks! Done 👍