-
Notifications
You must be signed in to change notification settings - Fork 737
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 support for specifying badEnclosingTypes for BadImport
via flags
#4228
Changes from 1 commit
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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -388,4 +388,63 @@ public void doesNotMatchProtos() { | |||||||
"}") | ||||||||
.doTest(); | ||||||||
} | ||||||||
|
||||||||
@Test | ||||||||
public void badEnclosingTypes() { | ||||||||
refactoringTestHelper | ||||||||
.setArgs("-XepOpt:BadImport:BadEnclosingTypes=org.immutables.value.Value") | ||||||||
.addInputLines( | ||||||||
"org/immutables/value/Value.java", | ||||||||
"package org.immutables.value;", | ||||||||
"", | ||||||||
"public @interface Value {", | ||||||||
" @interface Immutable {}", | ||||||||
"}") | ||||||||
.expectUnchanged() | ||||||||
.addInputLines( | ||||||||
"Test.java", | ||||||||
"import org.immutables.value.Value.Immutable;", | ||||||||
"", | ||||||||
"@Immutable", | ||||||||
"interface Test {}") | ||||||||
.addOutputLines( | ||||||||
"Test.java", | ||||||||
"import org.immutables.value.Value;", | ||||||||
"", | ||||||||
"@Value.Immutable", | ||||||||
"interface Test {}") | ||||||||
.doTest(); | ||||||||
} | ||||||||
|
||||||||
@Test | ||||||||
public void badEnclosingTypes_doesNotMatchFullyQualifiedName() { | ||||||||
compilationTestHelper | ||||||||
.setArgs("-XepOpt:BadImport:BadEnclosingTypes=org.immutables.value.Value") | ||||||||
.addSourceLines( | ||||||||
"org/immutables/value/Value.java", | ||||||||
"package org.immutables.value;", | ||||||||
"", | ||||||||
"public @interface Value {", | ||||||||
" @interface Immutable {}", | ||||||||
"}") | ||||||||
.addSourceLines("Test.java", "@org.immutables.value.Value.Immutable", "interface Test {}") | ||||||||
.doTest(); | ||||||||
} | ||||||||
|
||||||||
@Test | ||||||||
public void badEnclosingTypes_staticMethod() { | ||||||||
compilationTestHelper | ||||||||
.setArgs("-XepOpt:BadImport:BadEnclosingTypes=com.google.common.collect.ImmutableList") | ||||||||
.addSourceLines( | ||||||||
"Test.java", | ||||||||
"import static com.google.common.collect.ImmutableList.toImmutableList;", | ||||||||
"import com.google.common.collect.ImmutableList;", | ||||||||
"import java.util.stream.Collector;", | ||||||||
"", | ||||||||
"class Test {", | ||||||||
" // BUG: Diagnostic contains: ImmutableList.toImmutableList()", | ||||||||
" Collector<?, ?, ImmutableList<Object>> immutableList = toImmutableList();", | ||||||||
Comment on lines
+434
to
+446
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. Is disallowing static import desirable? Should we change the behavior only for nested types? 🤔 error-prone/core/src/main/java/com/google/errorprone/bugpatterns/BadImport.java Lines 107 to 109 in 63cf192
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. The current behaviour of treating static imports consistently seems OK to me. Obviously |
||||||||
"}") | ||||||||
.doTest(); | ||||||||
} | ||||||||
} |
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 this is fine because of the guarantees made by the checks in
isFullyQualified
, but we generally try to avoid usingTree#toString
. There's some performance cost for larger trees, and the string representation isn't stable.What do you think about something like:
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.
👀 We shouldn't directly return if it's not a bad nested class, but I think I got your point.
I tried to refactor in 67aa36d, PTAL.