IDEA-135189 (False positive of "Type may be weakened")

This commit is contained in:
Bas Leijdekkers
2015-01-14 12:36:36 +01:00
parent 852b48aaaf
commit 3f2212a179
4 changed files with 42 additions and 6 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2014 Bas Leijdekkers
* Copyright 2006-2015 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package com.siyeh.ig.abstraction;
import com.intellij.codeInsight.daemon.impl.analysis.JavaHighlightUtil;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
@@ -294,7 +295,7 @@ public class TypeMayBeWeakenedInspection extends BaseInspection {
@Override
public void visitMethod(PsiMethod method) {
super.visitMethod(method);
if (isOnTheFly() && !method.hasModifierProperty(PsiModifier.PRIVATE)) {
if (isOnTheFly() && !method.hasModifierProperty(PsiModifier.PRIVATE) && !ApplicationManager.getApplication().isUnitTestMode()) {
// checking methods with greater visibility is too expensive.
// for error checking in the editor
return;
@@ -1,5 +1,5 @@
/*
* Copyright 2008-2014 Bas Leijdekkers
* Copyright 2008-2015 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -155,11 +155,17 @@ public class WeakestTypeFinder {
checkClass(javaLangIterableClass, weakestTypeClasses);
}
else if (referenceParent instanceof PsiReturnStatement) {
final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(referenceParent, PsiMethod.class);
if (containingMethod == null) {
final PsiElement owner = PsiTreeUtil.getParentOfType(referenceParent, PsiMethod.class, PsiLambdaExpression.class);
final PsiType type;
if (owner instanceof PsiMethod) {
type = ((PsiMethod)owner).getReturnType();
}
else if (owner instanceof PsiLambdaExpression) {
type = LambdaUtil.getFunctionalInterfaceReturnType((PsiLambdaExpression)owner);
}
else {
return Collections.emptyList();
}
final PsiType type = containingMethod.getReturnType();
if (!checkType(type, weakestTypeClasses)) {
return Collections.emptyList();
}
@@ -0,0 +1,23 @@
package com.siyeh.igtest.abstraction.weaken_type;
import java.util.function.Supplier;
public interface Lambda {
int count();
static Lambda newWeaken(int value) {
return new Lambda() {
@Override
public int count() {
return value;
}
};
}
static Supplier<Lambda> newSupplier() {
return () -> {
return Lambda.newWeaken(0);
};
}
}
@@ -9,6 +9,7 @@ public class TypeMayBeWeakenedInspectionTest extends LightInspectionTestCase {
public void testTypeMayBeWeakened() { doTest(); }
public void testNumberAdderDemo() { doTest(); }
public void testAutoClosableTest() { doTest(); }
public void testLambda() { doTest(); }
@Override
protected String[] getEnvironmentClasses() {
@@ -33,6 +34,11 @@ public class TypeMayBeWeakenedInspectionTest extends LightInspectionTestCase {
"@FunctionalInterface " +
"public interface Function<T, R> {" +
" R apply(T t);" +
"}",
"package java.util.function;\n" +
"@FunctionalInterface\n" +
"public interface Supplier<T> {\n" +
" T get();\n" +
"}"
};
}