AssignmentOrReturnOfFieldWithMutableType: Guava test & field type check (IDEA-191042)

This commit is contained in:
Tagir Valeev
2018-05-28 09:50:45 +07:00
parent a76c979072
commit 3ccd9278de
3 changed files with 25 additions and 1 deletions
@@ -145,7 +145,9 @@ public class AssignmentOrReturnOfFieldWithMutableTypeInspection extends BaseInsp
if (field == null) return;
final String type = TypeUtils.expressionHasTypeOrSubtype(returnValue, MUTABLE_TYPES);
if (type == null && !(returnValue.getType() instanceof PsiArrayType)) return;
if (CollectionUtils.isConstantEmptyArray(field) || Mutability.getMutability(field).isUnmodifiable()) return;
if (CollectionUtils.isConstantEmptyArray(field) ||
ClassUtils.isImmutable(field.getType()) ||
Mutability.getMutability(field).isUnmodifiable()) return;
registerError(returnValue, field, returnValue, type, Boolean.FALSE);
}
}
@@ -1,6 +1,7 @@
package com.siyeh.igtest.encapsulation;
import java.util.*;
import com.google.common.collect.*;
public class AssignmentOrReturnOfFieldWithMutableType
{
@@ -151,4 +152,12 @@ class ImmutableTest {
public List<String> getList() {
return list;
}
}
class GuavaTest {
private final ImmutableList<?> list = ImmutableList.of();
public ImmutableList<?> getList() {
return list;
}
}
@@ -22,6 +22,19 @@ public class AssignmentOrReturnOfFieldWithMutableTypeInspectionTest extends Ligh
}
};
@Override
protected String[] getEnvironmentClasses() {
return new String[] {
"package com.google.common.collect;\n" +
"\n" +
"import java.util.List;\n" +
"\n" +
"public class ImmutableList<E> implements List<E> {\n" +
" public static ImmutableList<?> of() {return new ImmutableList<>();}\n" +
"}"
};
}
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {