-
Notifications
You must be signed in to change notification settings - Fork 39
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
Introduce NestedOptionals
check
#202
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
98174c1
Bugpattern for disallowing `Optional<Optional<...>>`
Venorcis 2691140
Suppress new (self-check) warning in two spots
Venorcis 18f2dbf
Suggestions
Stephan202 0891ca9
Suggestions
rickie a11793c
Clarify that the suppression should be resolved
Stephan202 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
51 changes: 51 additions & 0 deletions
51
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/NestedOptionals.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,51 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import static com.google.errorprone.BugPattern.LinkType.NONE; | ||
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; | ||
import static com.google.errorprone.BugPattern.StandardTags.FRAGILE_CODE; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.google.common.collect.Iterables; | ||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.suppliers.Supplier; | ||
import com.google.errorprone.suppliers.Suppliers; | ||
import com.google.errorprone.util.ASTHelpers; | ||
import com.sun.source.tree.MethodInvocationTree; | ||
import com.sun.source.tree.Tree; | ||
import com.sun.tools.javac.code.Type; | ||
import com.sun.tools.javac.util.List; | ||
import java.util.Optional; | ||
|
||
/** A {@link BugChecker} which flags nesting of {@link Optional Optionals}. */ | ||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
summary = | ||
"Avoid nesting `Optional`s inside `Optional`s; the resultant code is hard to reason about", | ||
linkType = NONE, | ||
severity = WARNING, | ||
tags = FRAGILE_CODE) | ||
public final class NestedOptionals extends BugChecker implements MethodInvocationTreeMatcher { | ||
private static final long serialVersionUID = 1L; | ||
private static final Supplier<Type> OPTIONAL = Suppliers.typeFromClass(Optional.class); | ||
|
||
@Override | ||
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { | ||
return isOptionalOfOptional(tree, state) ? describeMatch(tree) : Description.NO_MATCH; | ||
} | ||
|
||
private static boolean isOptionalOfOptional(Tree tree, VisitorState state) { | ||
Type optionalType = OPTIONAL.get(state); | ||
Type type = ASTHelpers.getType(tree); | ||
if (!ASTHelpers.isSubtype(type, optionalType, state)) { | ||
return false; | ||
} | ||
|
||
List<Type> typeArguments = type.getTypeArguments(); | ||
return !typeArguments.isEmpty() | ||
&& ASTHelpers.isSubtype(Iterables.getOnlyElement(typeArguments), optionalType, state); | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
...r-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/NestedOptionalsTest.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,42 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import com.google.errorprone.CompilationTestHelper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
final class NestedOptionalsTest { | ||
private final CompilationTestHelper compilationTestHelper = | ||
CompilationTestHelper.newInstance(NestedOptionals.class, getClass()); | ||
|
||
@Test | ||
void identification() { | ||
compilationTestHelper | ||
.addSourceLines( | ||
"A.java", | ||
"import java.util.Optional;", | ||
"import java.util.stream.Stream;", | ||
"", | ||
"class A {", | ||
" void m() {", | ||
" Optional.empty();", | ||
" Optional.of(1);", | ||
" // BUG: Diagnostic contains:", | ||
" Optional.of(Optional.empty());", | ||
" // BUG: Diagnostic contains:", | ||
" Optional.of(Optional.of(1));", | ||
"", | ||
" Optional.ofNullable(null);", | ||
" // BUG: Diagnostic contains:", | ||
" Optional.ofNullable((Optional) null);", | ||
"", | ||
" Optional.of(\"foo\").map(String::length);", | ||
" // BUG: Diagnostic contains:", | ||
" Optional.of(\"foo\").map(Optional::of);", | ||
"", | ||
" Stream.of(\"foo\").findFirst();", | ||
" // BUG: Diagnostic contains:", | ||
" Stream.of(\"foo\").map(Optional::of).findFirst();", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
For readability we could add an empty line here, below the two
ofNullable
examples and before theStream.of
examples. Not a big win though.We do this in some other tests as well.