IDEA-170892 Inspection: Suspicious Arrays.fill() call

This commit is contained in:
Tagir Valeev
2017-04-20 18:27:11 +07:00
parent 813055a0d0
commit dab8efdeaf
6 changed files with 181 additions and 0 deletions
@@ -0,0 +1,95 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInspection;
import com.intellij.psi.*;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import java.util.Set;
/**
* @author Tagir Valeev
*/
public class SuspiciousArrayMethodCallInspection extends BaseJavaBatchLocalInspectionTool {
private static final Set<String> INTERESTING_NAMES = ContainerUtil.set("fill", "binarySearch", "equals", "mismatch");
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
return new JavaElementVisitor() {
@Override
public void visitMethodCallExpression(PsiMethodCallExpression call) {
PsiElement nameElement = call.getMethodExpression().getReferenceNameElement();
if (nameElement == null) return;
String name = nameElement.getText();
if (!INTERESTING_NAMES.contains(name)) return;
PsiMethod method = call.resolveMethod();
if (method == null) return;
PsiClass aClass = method.getContainingClass();
if (aClass == null || !CommonClassNames.JAVA_UTIL_ARRAYS.equals(aClass.getQualifiedName())) return;
PsiExpression[] args = call.getArgumentList().getExpressions();
switch (name) {
case "fill":
case "binarySearch":
if (args.length == 2) {
handleArrayElement(args[0], args[1]);
}
else if (args.length == 4) {
handleArrayElement(args[0], args[3]);
}
break;
case "equals":
case "mismatch":
if (args.length == 2) {
handleArrays(nameElement, args[0], args[1]);
}
else if (args.length == 6) {
handleArrays(nameElement, args[0], args[3]);
}
break;
}
}
private void handleArrayElement(PsiExpression array, PsiExpression element) {
PsiType arrayType = array.getType();
PsiType elementType = element.getType();
if (elementType == null || !(arrayType instanceof PsiArrayType)) return;
PsiType arrayElementType = ((PsiArrayType)arrayType).getComponentType();
// incompatible primitive array will be reported anyways as compilation error
if (arrayElementType instanceof PsiPrimitiveType) return;
if (!TypeConversionUtil.areTypesConvertible(elementType, arrayElementType)) {
holder.registerProblem(element, InspectionsBundle.message("inspection.suspicious.array.method.call.problem.element"));
}
}
private void handleArrays(PsiElement context, PsiExpression array1, PsiExpression array2) {
PsiType array1Type = array1.getType();
PsiType array2Type = array2.getType();
if (!(array1Type instanceof PsiArrayType) || !(array2Type instanceof PsiArrayType)) return;
PsiType array1ElementType = ((PsiArrayType)array1Type).getComponentType();
PsiType array2ElementType = ((PsiArrayType)array2Type).getComponentType();
// incompatible primitive array will be reported anyways as compilation error
if (array1ElementType instanceof PsiPrimitiveType || array2ElementType instanceof PsiPrimitiveType) return;
if (!TypeConversionUtil.areTypesConvertible(array1ElementType, array2ElementType) ||
!TypeConversionUtil.areTypesConvertible(array2ElementType, array1ElementType)) {
holder.registerProblem(context, InspectionsBundle.message("inspection.suspicious.array.method.call.problem.arrays"));
}
}
};
}
}
@@ -0,0 +1,30 @@
import java.util.Arrays;
public class SuspiciousArrayMethodCall {
void test() {
int[][] data = new int[10][];
Arrays.fill(data, <warning descr="Element type is not compatible with array type">-1</warning>);
}
void testBoxing() {
int[] data = new int[10];
Arrays.fill(data, -1);
}
int findString(String[] data) {
return Arrays.binarySearch(data, <warning descr="Element type is not compatible with array type">1</warning>);
}
int findCharSequence(CharSequence[] data, Integer i) {
return Arrays.binarySearch(data, <warning descr="Element type is not compatible with array type">i</warning>);
}
int findCharSequenceNumber(CharSequence[] data, Number n) {
return Arrays.binarySearch(data, n);
}
boolean testEquality(String[] arr, Integer[] arr2) {
return Arrays.<warning descr="Array types are incompatible: arrays are always different">equals</warning>(arr, arr2) ||
Arrays.<warning descr="Array types are incompatible: arrays are always different">equals</warning>(arr2, arr);
}
}
@@ -0,0 +1,39 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInspection;
import com.intellij.JavaTestUtil;
import com.siyeh.ig.LightInspectionTestCase;
import org.jetbrains.annotations.Nullable;
/**
* @author Tagir Valeev
*/
public class SuspiciousArrayMethodCallInspectionTest extends LightInspectionTestCase {
@Override
protected String getBasePath() {
return JavaTestUtil.getRelativeJavaTestDataPath() + "/inspection/suspiciousArrayMethodCall";
}
@Nullable
@Override
protected InspectionProfileEntry getInspection() {
return new SuspiciousArrayMethodCallInspection();
}
public void testSuspiciousArrayMethodCall() throws Exception { doTest();}
}
@@ -223,6 +223,10 @@ inspection.suspicious.collections.method.calls.display.name=Suspicious collectio
inspection.suspicious.collections.method.calls.problem.descriptor=''{0}'' may not contain objects of type ''{1}''
inspection.suspicious.collections.method.calls.problem.descriptor1=Suspicious call to ''{0}''
inspection.suspicious.array.method.call.display.name=Suspicious array method calls
inspection.suspicious.array.method.call.problem.element=Element type is not compatible with array type
inspection.suspicious.array.method.call.problem.arrays=Array types are incompatible: arrays are always different
inspection.raw.variable.type.can.be.generic.name=Raw type can be generic
inspection.raw.variable.type.can.be.generic.quickfix=Change type of {0} to {1}
inspection.raw.variable.type.can.be.generic.family.quickfix=Add generic parameters to the type
@@ -0,0 +1,9 @@
<html>
<body>
<p>Reports when non-generic array manipulation method like <code>Arrays.fill</code> is called with mismatched argument types.
Such call will not do anything useless and likely to be a mistake.
</p>
<!-- tooltip end -->
<p><small>New in 2017.2</small></p>
</body>
</html>
+4
View File
@@ -649,6 +649,10 @@
key="inspection.suspicious.collections.method.calls.display.name" groupKey="group.names.probable.bugs" enabledByDefault="true"
level="WARNING"
implementationClass="com.intellij.codeInspection.miscGenerics.SuspiciousCollectionsMethodCallsInspection"/>
<localInspection groupPath="Java" language="JAVA" shortName="SuspiciousArrayMethodCall" bundle="messages.InspectionsBundle"
key="inspection.suspicious.array.method.call.display.name" groupKey="group.names.probable.bugs" enabledByDefault="true"
level="WARNING"
implementationClass="com.intellij.codeInspection.SuspiciousArrayMethodCallInspection"/>
<localInspection groupPath="Java" language="JAVA" shortName="RawTypeCanBeGeneric" bundle="messages.InspectionsBundle"
key="inspection.raw.variable.type.can.be.generic.name" groupKey="group.names.code.style.issues"
enabledByDefault="true" level="INFORMATION"