suspicious call: type of new expression must be assignable otherwise it's suspicious (IDEA-164816)

This commit is contained in:
Anna.Kozlova
2016-12-01 16:52:02 +01:00
parent ba117e7328
commit 4e75eeae21
3 changed files with 29 additions and 3 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -96,9 +96,10 @@ public class SuspiciousCollectionsMethodCallsInspection extends BaseJavaBatchLoc
if (args.length != 1) return null;
PsiType argType = args[0].getType();
boolean exactType = args[0] instanceof PsiNewExpression;
final String plainMessage = SuspiciousMethodCallUtil
.getSuspiciousMethodCallMessage(methodCall, args[0], argType, reportConvertibleMethodCalls, patternMethods, indices);
if (plainMessage != null) {
.getSuspiciousMethodCallMessage(methodCall, args[0], argType, exactType || reportConvertibleMethodCalls, patternMethods, indices);
if (plainMessage != null && !exactType) {
final PsiType dfaType = GuessManager.getInstance(methodCall.getProject()).getControlFlowExpressionType(args[0]);
if (dfaType != null && SuspiciousMethodCallUtil
.getSuspiciousMethodCallMessage(methodCall, args[0], dfaType, reportConvertibleMethodCalls, patternMethods, indices) == null) {
@@ -0,0 +1,9 @@
import java.util.*;
class Foo<T extends List<Integer>> {
public void foo() {
Map<T, T> map = new HashMap<>();
map.containsKey(<warning descr="Suspicious call to 'Map.containsKey'">new HashMap<>()</warning>);
}
}
@@ -1,3 +1,18 @@
/*
* Copyright 2000-2016 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;
@@ -25,6 +40,7 @@ public class SuspiciousCollectionMethodCallsTest extends LightCodeInsightFixture
public void testUseDfa() throws Exception { doTest(); }
public void testWildcard() throws Exception { doTest(); }
public void testPolyConditionalExpressionPassedToMapGetCall() throws Exception { doTest(); }
public void testNewExpressionPassedToMapContains() throws Exception { doTest(); }
public void testIgnoreConvertible() throws Exception {
myTool.REPORT_CONVERTIBLE_METHOD_CALLS = false;
doTest();