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

RawTypes no longer flags implicit raw types from lambdas #2119

Merged
merged 2 commits into from
Mar 18, 2022
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 @@ -51,49 +51,53 @@ public final class RawTypes extends BugChecker
BugChecker.VariableTreeMatcher {
@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
return testType(tree.getType());
return testType(tree.getType(), state);
}

@Override
public Description matchNewClass(NewClassTree tree, VisitorState state) {
return testType(tree.getIdentifier());
return testType(tree.getIdentifier(), state);
}

@Override
public Description matchClass(ClassTree tree, VisitorState state) {
Description extendsResult = testType(tree.getExtendsClause());
Description extendsResult = testType(tree.getExtendsClause(), state);
if (extendsResult != Description.NO_MATCH) {
return extendsResult;
}
return testTypes(tree.getImplementsClause());
return testTypes(tree.getImplementsClause(), state);
}

@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
return testType(tree.getReturnType());
return testType(tree.getReturnType(), state);
}

@Override
public Description matchTypeCast(TypeCastTree tree, VisitorState state) {
return testType(tree.getType());
return testType(tree.getType(), state);
}

private Description testTypes(Iterable<? extends Tree> types) {
private Description testTypes(Iterable<? extends Tree> types, VisitorState state) {
for (Tree type : types) {
Description description = testType(type);
Description description = testType(type, state);
if (description != Description.NO_MATCH) {
return description;
}
}
return Description.NO_MATCH;
}

private Description testType(Tree type) {
private Description testType(Tree type, VisitorState state) {
if (type == null) {
return Description.NO_MATCH;
}
Type realType = ASTHelpers.getType(type);
if (realType != null && realType.isRaw()) {
if (state.getEndPosition(type) < 0) {
// No source for the type node, it may be an implicit type in a lambda
return Description.NO_MATCH;
}
return buildDescription(type)
.setMessage("Avoid raw types; add appropriate type parameters if possible. "
+ "The type was: "
Expand All @@ -103,7 +107,7 @@ private Description testType(Tree type) {
.build();
}
if (type.getKind() == Tree.Kind.PARAMETERIZED_TYPE) {
return testTypes(((ParameterizedTypeTree) type).getTypeArguments());
return testTypes(((ParameterizedTypeTree) type).getTypeArguments(), state);
}
return Description.NO_MATCH;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,21 @@ void testNegativeImplements3() {
.doTest();
}

@Test
void testNegativeImplicitRawTypes() {
helper().addSourceLines(
"Test.java",
"import static org.assertj.core.api.Assertions.*;",
"import java.util.function.*;",
"import com.codahale.metrics.*;",
"class Test {",
" void func(Metric metric) {",
" assertThat(metric).isInstanceOfSatisfying(Gauge.class, value -> {});",
" }",
"}")
.doTest();
}

private CompilationTestHelper helper() {
return CompilationTestHelper.newInstance(RawTypes.class, getClass());
}
Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2119.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: RawTypes no longer flags implicit raw types from lambdas
links:
- https://github.com/palantir/gradle-baseline/pull/2119