From d42ef3a864820c1ea35f68d50f5854b990419f6a Mon Sep 17 00:00:00 2001 From: anna Date: Fri, 24 Jun 2011 15:13:58 +0400 Subject: [PATCH] redundant cast: move test data; ignore casts to invoke @NotNull methods; ignore suspicious collections method calls --- .../GenericsInspectionToolBase.java | 2 + ...ciousCollectionsMethodCallsInspection.java | 178 ++++++++++-------- .../RedundantCastInspection.java | 62 ++++++ .../redundantCast/AmbigousParm1/expected.xml | 9 + .../AmbigousParm1/src/AmbigousParm1.java | 13 ++ .../redundantCast/AmbigousParm2/expected.xml | 14 ++ .../AmbigousParm2/src/.AmbigousParm2.java.swp | Bin 0 -> 12288 bytes .../AmbigousParm2/src/AmbigousParm2.java | 17 ++ .../redundantCast/AmbigousParm3/expected.xml | 9 + .../AmbigousParm3/src/AmbigousParm3.java | 15 ++ .../redundantCast/AmbigousParm4/expected.xml | 3 + .../AmbigousParm4/src/AmbigousParm4.java | 8 + .../redundantCast/AmbigousParm5/expected.xml | 3 + .../AmbigousParm5/src/AmbigousParm5.java | 8 + .../redundantCast/AnyOfTwo/expected.xml | 3 + .../redundantCast/AnyOfTwo/src/AnyOfTwo.java | 11 ++ .../redundantCast/Assignment1/expected.xml | 9 + .../Assignment1/src/Assignment1.java | 6 + .../ComparingToNull/expected.xml | 9 + .../ComparingToNull/src/Test.java | 6 + .../DifferentFields/expected.xml | 3 + .../DifferentFields/src/Test.java | 12 ++ .../redundantCast/DoubleCast1/expected.xml | 9 + .../DoubleCast1/src/DoubleCast1.java | 9 + .../redundantCast/DoubleCast2/expected.xml | 9 + .../DoubleCast2/src/DoubleCast2.java | 9 + .../redundantCast/DoubleCast3/expected.xml | 14 ++ .../DoubleCast3/src/DoubleCast3.java | 6 + .../redundantCast/DoubleCast4/expected.xml | 14 ++ .../DoubleCast4/src/DoubleCast4.java | 6 + .../redundantCast/DoubleCast5/expected.xml | 3 + .../DoubleCast5/src/DoubleCast5.java | 5 + .../redundantCast/IDEADEV15170/expected.xml | 2 + .../redundantCast/IDEADEV15170/src/Test.java | 16 ++ .../redundantCast/IDEADEV25675/expected.xml | 9 + .../redundantCast/IDEADEV25675/src/A.java | 9 + .../redundantCast/IDEADEV6818/expected.xml | 9 + .../redundantCast/IDEADEV6818/src/A.java | 15 ++ .../redundantCast/InConditional/expected.xml | 9 + .../redundantCast/InConditional/src/A.java | 5 + .../redundantCast/Inaccessible/expected.xml | 3 + .../redundantCast/Inaccessible/src/Test.java | 12 ++ .../redundantCast/Initializer1/expected.xml | 9 + .../Initializer1/src/Initializer1.java | 5 + .../redundantCast/IntToDouble/expected.xml | 3 + .../redundantCast/IntToDouble/src/Test.java | 5 + .../redundantCast/NestedCast/expected.xml | 9 + .../NestedCast/src/CastTest.java | 9 + .../redundantCast/NestedThings/expected.xml | 9 + .../NestedThings/src/NestedThings.java | 26 +++ .../redundantCast/New1/expected.xml | 9 + .../redundantCast/New1/src/New1.java | 7 + .../redundantCast/OneOfTwo/expected.xml | 9 + .../redundantCast/OneOfTwo/src/OneOfTwo.java | 10 + .../redundantCast/SCR11555/expected.xml | 3 + .../redundantCast/SCR11555/src/Test.java | 9 + .../redundantCast/SCR13397/expected.xml | 3 + .../redundantCast/SCR13397/src/Test.java | 21 +++ .../redundantCast/SCR14502/expected.xml | 9 + .../redundantCast/SCR14502/src/Test.java | 5 + .../redundantCast/SCR14559/expected.xml | 3 + .../redundantCast/SCR14559/src/Test.java | 15 ++ .../redundantCast/SCR15236/expected.xml | 8 + .../redundantCast/SCR15236/src/Test.java | 5 + .../redundantCast/SCR6907/expected.xml | 9 + .../redundantCast/SCR6907/src/Test.java | 9 + .../redundantCast/ShortToShort/expected.xml | 9 + .../redundantCast/ShortToShort/src/Test.java | 7 + .../redundantCast/ShortVsInt/expected.xml | 3 + .../ShortVsInt/src/ShortVsInt.java | 8 + .../redundantCast/Truncation/expected.xml | 3 + .../redundantCast/Truncation/src/Test.java | 5 + .../redundantCast/VirtualMethod1/expected.xml | 9 + .../VirtualMethod1/src/VirtualMethod1.java | 14 ++ .../redundantCast/VirtualMethod2/expected.xml | 3 + .../VirtualMethod2/src/VirtualMethod2.java | 14 ++ .../redundantCast/VirtualMethod3/expected.xml | 9 + .../VirtualMethod3/src/VirtualMethod3.java | 6 + .../generics/BoxingInRef/expected.xml | 3 + .../generics/BoxingInRef/src/Test.java | 7 + .../generics/EnumConstant/expected.xml | 9 + .../generics/EnumConstant/src/Test.java | 7 + .../generics/Ignore/expected.xml | 14 ++ .../generics/Ignore/scr/Suspicious.java | 12 ++ .../generics/Ignore/scr/Test.java | 21 +++ .../generics/Inference1/expected.xml | 3 + .../generics/Inference1/src/Test.java | 20 ++ .../generics/Inference2/expected.xml | 3 + .../generics/Inference2/src/Test.java | 32 ++++ .../generics/Inference3/expected.xml | 13 ++ .../generics/Inference3/src/Test.java | 32 ++++ .../NullInVarargsParameter/expected.xml | 13 ++ .../NullInVarargsParameter/src/Test.java | 11 ++ .../generics/RawCast/expected.xml | 2 + .../generics/RawCast/src/Test.java | 11 ++ .../expected.xml | 2 + .../src/CastCast.java | 13 ++ .../src/ReturnValue.java | 16 ++ .../WrapperToPrimitiveCast/expected.xml | 48 +++++ .../src/WrapperToPrimitiveCast.java | 26 +++ .../codeInspection/RedundantCast15Test.java | 47 +++++ .../codeInspection/RedundantCastTest.java | 93 +++++++++ 102 files changed, 1248 insertions(+), 82 deletions(-) create mode 100644 java/java-tests/testData/inspection/redundantCast/AmbigousParm1/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/AmbigousParm1/src/AmbigousParm1.java create mode 100644 java/java-tests/testData/inspection/redundantCast/AmbigousParm2/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/AmbigousParm2/src/.AmbigousParm2.java.swp create mode 100644 java/java-tests/testData/inspection/redundantCast/AmbigousParm2/src/AmbigousParm2.java create mode 100644 java/java-tests/testData/inspection/redundantCast/AmbigousParm3/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/AmbigousParm3/src/AmbigousParm3.java create mode 100644 java/java-tests/testData/inspection/redundantCast/AmbigousParm4/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/AmbigousParm4/src/AmbigousParm4.java create mode 100644 java/java-tests/testData/inspection/redundantCast/AmbigousParm5/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/AmbigousParm5/src/AmbigousParm5.java create mode 100644 java/java-tests/testData/inspection/redundantCast/AnyOfTwo/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/AnyOfTwo/src/AnyOfTwo.java create mode 100644 java/java-tests/testData/inspection/redundantCast/Assignment1/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/Assignment1/src/Assignment1.java create mode 100644 java/java-tests/testData/inspection/redundantCast/ComparingToNull/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/ComparingToNull/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/DifferentFields/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/DifferentFields/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/DoubleCast1/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/DoubleCast1/src/DoubleCast1.java create mode 100644 java/java-tests/testData/inspection/redundantCast/DoubleCast2/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/DoubleCast2/src/DoubleCast2.java create mode 100644 java/java-tests/testData/inspection/redundantCast/DoubleCast3/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/DoubleCast3/src/DoubleCast3.java create mode 100644 java/java-tests/testData/inspection/redundantCast/DoubleCast4/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/DoubleCast4/src/DoubleCast4.java create mode 100644 java/java-tests/testData/inspection/redundantCast/DoubleCast5/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/DoubleCast5/src/DoubleCast5.java create mode 100644 java/java-tests/testData/inspection/redundantCast/IDEADEV15170/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/IDEADEV15170/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/IDEADEV25675/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/IDEADEV25675/src/A.java create mode 100644 java/java-tests/testData/inspection/redundantCast/IDEADEV6818/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/IDEADEV6818/src/A.java create mode 100644 java/java-tests/testData/inspection/redundantCast/InConditional/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/InConditional/src/A.java create mode 100644 java/java-tests/testData/inspection/redundantCast/Inaccessible/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/Inaccessible/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/Initializer1/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/Initializer1/src/Initializer1.java create mode 100644 java/java-tests/testData/inspection/redundantCast/IntToDouble/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/IntToDouble/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/NestedCast/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/NestedCast/src/CastTest.java create mode 100644 java/java-tests/testData/inspection/redundantCast/NestedThings/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/NestedThings/src/NestedThings.java create mode 100644 java/java-tests/testData/inspection/redundantCast/New1/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/New1/src/New1.java create mode 100644 java/java-tests/testData/inspection/redundantCast/OneOfTwo/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/OneOfTwo/src/OneOfTwo.java create mode 100644 java/java-tests/testData/inspection/redundantCast/SCR11555/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/SCR11555/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/SCR13397/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/SCR13397/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/SCR14502/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/SCR14502/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/SCR14559/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/SCR14559/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/SCR15236/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/SCR15236/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/SCR6907/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/SCR6907/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/ShortToShort/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/ShortToShort/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/ShortVsInt/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/ShortVsInt/src/ShortVsInt.java create mode 100644 java/java-tests/testData/inspection/redundantCast/Truncation/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/Truncation/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/VirtualMethod1/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/VirtualMethod1/src/VirtualMethod1.java create mode 100644 java/java-tests/testData/inspection/redundantCast/VirtualMethod2/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/VirtualMethod2/src/VirtualMethod2.java create mode 100644 java/java-tests/testData/inspection/redundantCast/VirtualMethod3/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/VirtualMethod3/src/VirtualMethod3.java create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/BoxingInRef/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/BoxingInRef/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/EnumConstant/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/EnumConstant/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/Ignore/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/Ignore/scr/Suspicious.java create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/Ignore/scr/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/Inference1/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/Inference1/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/Inference2/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/Inference2/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/Inference3/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/Inference3/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/NullInVarargsParameter/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/NullInVarargsParameter/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/RawCast/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/RawCast/src/Test.java create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/RawCastsToAvoidIncompatibility/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/RawCastsToAvoidIncompatibility/src/CastCast.java create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/RawCastsToAvoidIncompatibility/src/ReturnValue.java create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/WrapperToPrimitiveCast/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/WrapperToPrimitiveCast/src/WrapperToPrimitiveCast.java create mode 100644 java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast15Test.java create mode 100644 java/java-tests/testSrc/com/intellij/codeInspection/RedundantCastTest.java diff --git a/java/java-impl/src/com/intellij/codeInspection/miscGenerics/GenericsInspectionToolBase.java b/java/java-impl/src/com/intellij/codeInspection/miscGenerics/GenericsInspectionToolBase.java index ff4361e0d331..f29f8c4cafc5 100644 --- a/java/java-impl/src/com/intellij/codeInspection/miscGenerics/GenericsInspectionToolBase.java +++ b/java/java-impl/src/com/intellij/codeInspection/miscGenerics/GenericsInspectionToolBase.java @@ -21,6 +21,7 @@ import com.intellij.codeInspection.ex.BaseLocalInspectionTool; import com.intellij.psi.*; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; @@ -62,5 +63,6 @@ public abstract class GenericsInspectionToolBase extends BaseLocalInspectionTool return null; } + @Nullable public abstract ProblemDescriptor[] getDescriptions(PsiElement place, InspectionManager manager, boolean isOnTheFly); } diff --git a/java/java-impl/src/com/intellij/codeInspection/miscGenerics/SuspiciousCollectionsMethodCallsInspection.java b/java/java-impl/src/com/intellij/codeInspection/miscGenerics/SuspiciousCollectionsMethodCallsInspection.java index 2a83d9140083..ec648e2894af 100644 --- a/java/java-impl/src/com/intellij/codeInspection/miscGenerics/SuspiciousCollectionsMethodCallsInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/miscGenerics/SuspiciousCollectionsMethodCallsInspection.java @@ -29,8 +29,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; @@ -105,88 +103,12 @@ public class SuspiciousCollectionsMethodCallsInspection extends BaseLocalInspect @Override public void visitMethodCallExpression(PsiMethodCallExpression methodCall) { super.visitMethodCallExpression(methodCall); - final PsiReferenceExpression methodExpression = methodCall.getMethodExpression(); - final PsiExpression qualifier = methodExpression.getQualifierExpression(); - if (qualifier == null || qualifier instanceof PsiThisExpression || qualifier instanceof PsiSuperExpression) return; - final PsiExpression[] args = methodCall.getArgumentList().getExpressions(); - if (args.length != 1) return; - PsiType argType = args[0].getType(); - if (argType instanceof PsiPrimitiveType) { - argType = ((PsiPrimitiveType)argType).getBoxedType(methodCall); - } - - if (!(argType instanceof PsiClassType)) return; - - final JavaResolveResult resolveResult = methodExpression.advancedResolve(false); - PsiMethod calleeMethod = (PsiMethod)resolveResult.getElement(); - if (calleeMethod == null) return; - PsiMethod contextMethod = PsiTreeUtil.getParentOfType(methodCall, PsiMethod.class); - - synchronized (patternMethods) { - if (patternMethods.isEmpty()) { - setupPatternMethods(methodCall.getManager(), methodCall.getResolveScope(), patternMethods, indices); - } - } - - for (int i = 0; i < patternMethods.size(); i++) { - PsiMethod patternMethod = patternMethods.get(i); - if (!patternMethod.getName().equals(methodExpression.getReferenceName())) continue; - int index = indices.get(i); - - //we are in collections method implementation - if (contextMethod != null && isInheritorOrSelf(contextMethod, patternMethod)) return; - - final PsiClass calleeClass = calleeMethod.getContainingClass(); - PsiSubstitutor substitutor = resolveResult.getSubstitutor(); - final PsiClass patternClass = patternMethod.getContainingClass(); - substitutor = TypeConversionUtil.getClassSubstitutor(patternClass, calleeClass, substitutor); - if (substitutor == null) continue; - - if (!patternMethod.getSignature(substitutor).equals(calleeMethod.getSignature(PsiSubstitutor.EMPTY))) continue; - - PsiTypeParameter[] typeParameters = patternClass.getTypeParameters(); - if (typeParameters.length <= index) return; - final PsiTypeParameter typeParameter = typeParameters[index]; - PsiType typeParamMapping = substitutor.substitute(typeParameter); - if (typeParamMapping == null) return; - String message = null; - if (typeParamMapping instanceof PsiCapturedWildcardType) { - typeParamMapping = ((PsiCapturedWildcardType)typeParamMapping).getWildcard(); - } - if (!typeParamMapping.isAssignableFrom(argType)) { - if (typeParamMapping.isConvertibleFrom(argType)) { - if (REPORT_CONVERTIBLE_METHOD_CALLS) { - message = InspectionsBundle.message("inspection.suspicious.collections.method.calls.problem.descriptor1", - PsiFormatUtil.formatMethod(calleeMethod, substitutor, - PsiFormatUtil.SHOW_NAME | PsiFormatUtil - .SHOW_CONTAINING_CLASS, PsiFormatUtil.SHOW_TYPE)); - } - } - else { - PsiType qualifierType = qualifier.getType(); - LOG.assertTrue(qualifierType != null); - - message = InspectionsBundle.message("inspection.suspicious.collections.method.calls.problem.descriptor", - PsiFormatUtil.formatType(qualifierType, 0, PsiSubstitutor.EMPTY), - PsiFormatUtil.formatType(argType, 0, PsiSubstitutor.EMPTY)); - } - } - if (message != null) { - holder.registerProblem(args[0], message); - } - return; + final String message = getSuspiciousMethodCallMessage(methodCall, REPORT_CONVERTIBLE_METHOD_CALLS, patternMethods, indices + ); + if (message != null) { + holder.registerProblem(methodCall.getArgumentList().getExpressions()[0], message); } } - - private boolean isInheritorOrSelf(PsiMethod inheritorCandidate, PsiMethod base) { - PsiClass aClass = inheritorCandidate.getContainingClass(); - PsiClass bClass = base.getContainingClass(); - if (aClass == null || bClass == null) return false; - PsiSubstitutor substitutor = TypeConversionUtil.getClassSubstitutor(bClass, aClass, PsiSubstitutor.EMPTY); - return substitutor != null && - MethodSignatureUtil.findMethodBySignature(bClass, inheritorCandidate.getSignature(substitutor), false) == base; - } - }; } @@ -204,4 +126,96 @@ public class SuspiciousCollectionsMethodCallsInspection extends BaseLocalInspect public String getShortName() { return "SuspiciousMethodCalls"; } + + @Nullable + private static String getSuspiciousMethodCallMessage(final PsiMethodCallExpression methodCall, + final boolean reportConvertibleMethodCalls, final List patternMethods, + final IntArrayList indices) { + final PsiExpression[] args = methodCall.getArgumentList().getExpressions(); + if (args.length != 1) return null; + PsiType argType = args[0].getType(); + return getSuspiciousMethodCallMessage(methodCall, argType, reportConvertibleMethodCalls, patternMethods, indices); + } + + @Nullable + public static String getSuspiciousMethodCallMessage(PsiMethodCallExpression methodCall, + PsiType argType, boolean reportConvertibleMethodCalls, List patternMethods, + IntArrayList indices) { + final PsiReferenceExpression methodExpression = methodCall.getMethodExpression(); + final PsiExpression qualifier = methodExpression.getQualifierExpression(); + if (qualifier == null || qualifier instanceof PsiThisExpression || qualifier instanceof PsiSuperExpression) return null; + if (argType instanceof PsiPrimitiveType) { + argType = ((PsiPrimitiveType)argType).getBoxedType(methodCall); + } + + if (!(argType instanceof PsiClassType)) return null; + + final JavaResolveResult resolveResult = methodExpression.advancedResolve(false); + PsiMethod calleeMethod = (PsiMethod)resolveResult.getElement(); + if (calleeMethod == null) return null; + PsiMethod contextMethod = PsiTreeUtil.getParentOfType(methodCall, PsiMethod.class); + + synchronized (patternMethods) { + if (patternMethods.isEmpty()) { + setupPatternMethods(methodCall.getManager(), methodCall.getResolveScope(), patternMethods, indices); + } + } + + for (int i = 0; i < patternMethods.size(); i++) { + PsiMethod patternMethod = patternMethods.get(i); + if (!patternMethod.getName().equals(methodExpression.getReferenceName())) continue; + int index = indices.get(i); + + //we are in collections method implementation + if (contextMethod != null && isInheritorOrSelf(contextMethod, patternMethod)) return null; + + final PsiClass calleeClass = calleeMethod.getContainingClass(); + PsiSubstitutor substitutor = resolveResult.getSubstitutor(); + final PsiClass patternClass = patternMethod.getContainingClass(); + substitutor = TypeConversionUtil.getClassSubstitutor(patternClass, calleeClass, substitutor); + if (substitutor == null) continue; + + if (!patternMethod.getSignature(substitutor).equals(calleeMethod.getSignature(PsiSubstitutor.EMPTY))) continue; + + PsiTypeParameter[] typeParameters = patternClass.getTypeParameters(); + if (typeParameters.length <= index) return null; + final PsiTypeParameter typeParameter = typeParameters[index]; + PsiType typeParamMapping = substitutor.substitute(typeParameter); + if (typeParamMapping == null) return null; + String message = null; + if (typeParamMapping instanceof PsiCapturedWildcardType) { + typeParamMapping = ((PsiCapturedWildcardType)typeParamMapping).getWildcard(); + } + if (!typeParamMapping.isAssignableFrom(argType)) { + if (typeParamMapping.isConvertibleFrom(argType)) { + if (reportConvertibleMethodCalls) { + message = InspectionsBundle.message("inspection.suspicious.collections.method.calls.problem.descriptor1", + PsiFormatUtil.formatMethod(calleeMethod, substitutor, + PsiFormatUtilBase.SHOW_NAME | + PsiFormatUtilBase.SHOW_CONTAINING_CLASS, + PsiFormatUtilBase.SHOW_TYPE)); + } + } + else { + PsiType qualifierType = qualifier.getType(); + LOG.assertTrue(qualifierType != null); + + message = InspectionsBundle.message("inspection.suspicious.collections.method.calls.problem.descriptor", + PsiFormatUtil.formatType(qualifierType, 0, PsiSubstitutor.EMPTY), + PsiFormatUtil.formatType(argType, 0, PsiSubstitutor.EMPTY)); + } + } + return message; + } + return null; + } + + private static boolean isInheritorOrSelf(PsiMethod inheritorCandidate, PsiMethod base) { + PsiClass aClass = inheritorCandidate.getContainingClass(); + PsiClass bClass = base.getContainingClass(); + if (aClass == null || bClass == null) return false; + PsiSubstitutor substitutor = TypeConversionUtil.getClassSubstitutor(bClass, aClass, PsiSubstitutor.EMPTY); + return substitutor != null && + MethodSignatureUtil.findMethodBySignature(bClass, inheritorCandidate.getSignature(substitutor), false) == base; + } } diff --git a/java/java-impl/src/com/intellij/codeInspection/redundantCast/RedundantCastInspection.java b/java/java-impl/src/com/intellij/codeInspection/redundantCast/RedundantCastInspection.java index e6a3878700b9..75558796a915 100644 --- a/java/java-impl/src/com/intellij/codeInspection/redundantCast/RedundantCastInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/redundantCast/RedundantCastInspection.java @@ -15,18 +15,29 @@ */ package com.intellij.codeInspection.redundantCast; +import com.intellij.codeInsight.AnnotationUtil; import com.intellij.codeInsight.CodeInsightUtilBase; import com.intellij.codeInsight.daemon.GroupNames; import com.intellij.codeInspection.*; import com.intellij.codeInspection.miscGenerics.GenericsInspectionToolBase; +import com.intellij.codeInspection.miscGenerics.SuspiciousCollectionsMethodCallsInspection; +import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.WriteExternalException; import com.intellij.psi.*; +import com.intellij.psi.search.LocalSearchScope; +import com.intellij.psi.search.searches.OverridingMethodsSearch; +import com.intellij.psi.util.PsiUtil; import com.intellij.psi.util.RedundantCastUtil; import com.intellij.util.IncorrectOperationException; +import com.intellij.util.containers.IntArrayList; +import org.jdom.Element; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import javax.swing.*; import java.util.ArrayList; import java.util.List; @@ -40,10 +51,15 @@ public class RedundantCastInspection extends GenericsInspectionToolBase { private static final String DISPLAY_NAME = InspectionsBundle.message("inspection.redundant.cast.display.name"); @NonNls private static final String SHORT_NAME = "RedundantCast"; + public boolean IGNORE_ANNOTATED_METHODS = false; + public boolean IGNORE_SUSPICIOUS_METHOD_CALLS = false; + + public RedundantCastInspection() { myQuickFixAction = new AcceptSuggested(); } + @Nullable public ProblemDescriptor[] getDescriptions(PsiElement where, InspectionManager manager, boolean isOnTheFly) { List redundantCasts = RedundantCastUtil.getRedundantCastsInside(where); if (redundantCasts.isEmpty()) return null; @@ -58,10 +74,56 @@ public class RedundantCastInspection extends GenericsInspectionToolBase { return descriptions.toArray(new ProblemDescriptor[descriptions.size()]); } + @Override + public void writeSettings(Element node) throws WriteExternalException { + if (IGNORE_ANNOTATED_METHODS || IGNORE_SUSPICIOUS_METHOD_CALLS) { + super.writeSettings(node); + } + } + + @Override + public JComponent createOptionsPanel() { + final MultipleCheckboxOptionsPanel optionsPanel = new MultipleCheckboxOptionsPanel(this); + optionsPanel.addCheckbox("Ignore casts appeared in suspicious collections method calls", "IGNORE_SUSPICIOUS_METHOD_CALLS"); + optionsPanel.addCheckbox("Ignore casts to invoke @NotNull method which overrides @Nullable", "IGNORE_ANNOTATED_METHODS"); + return optionsPanel; + } + + @Nullable private ProblemDescriptor createDescription(@NotNull PsiTypeCastExpression cast, @NotNull InspectionManager manager, boolean onTheFly) { PsiExpression operand = cast.getOperand(); PsiTypeElement castType = cast.getCastType(); if (operand == null || castType == null) return null; + PsiElement parent = cast.getParent(); + while (parent instanceof PsiParenthesizedExpression){ + parent = parent.getParent(); + } + if (parent instanceof PsiReferenceExpression) { + if (IGNORE_ANNOTATED_METHODS) { + final PsiElement gParent = parent.getParent(); + if (gParent instanceof PsiMethodCallExpression) { + final PsiMethod psiMethod = ((PsiMethodCallExpression)gParent).resolveMethod(); + if (psiMethod != null && AnnotationUtil.isNotNull(psiMethod)) { + final PsiClass superClass = PsiUtil.resolveClassInType(operand.getType()); + for (PsiMethod method : psiMethod.findSuperMethods(superClass)) { + if (AnnotationUtil.isNullable(method)) { + return null; + } + } + } + } + } + } else if (parent instanceof PsiExpressionList) { + final PsiElement gParent = parent.getParent(); + if (gParent instanceof PsiMethodCallExpression && IGNORE_SUSPICIOUS_METHOD_CALLS) { + final String message = SuspiciousCollectionsMethodCallsInspection + .getSuspiciousMethodCallMessage((PsiMethodCallExpression)gParent, operand.getType(), true, new ArrayList(), new IntArrayList()); + if (message != null) { + return null; + } + } + } + String message = InspectionsBundle.message("inspection.redundant.cast.problem.descriptor", "" + operand.getText() + "", "#ref #loc"); return manager.createProblemDescriptor(castType, message, myQuickFixAction, ProblemHighlightType.LIKE_UNUSED_SYMBOL, onTheFly); diff --git a/java/java-tests/testData/inspection/redundantCast/AmbigousParm1/expected.xml b/java/java-tests/testData/inspection/redundantCast/AmbigousParm1/expected.xml new file mode 100644 index 000000000000..a34576ea01d1 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/AmbigousParm1/expected.xml @@ -0,0 +1,9 @@ + + + + AmbigousParm1.java + 11 + Casting c to A is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/AmbigousParm1/src/AmbigousParm1.java b/java/java-tests/testData/inspection/redundantCast/AmbigousParm1/src/AmbigousParm1.java new file mode 100644 index 000000000000..d2e90c4196d2 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/AmbigousParm1/src/AmbigousParm1.java @@ -0,0 +1,13 @@ +interface A {} + +interface B {} + +class C implements A, B {} + +public class AmbigousParameter { + public void ua(A a) {} + + public void caller(C c) { + ua((A)c); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/AmbigousParm2/expected.xml b/java/java-tests/testData/inspection/redundantCast/AmbigousParm2/expected.xml new file mode 100644 index 000000000000..4190a659ddce --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/AmbigousParm2/expected.xml @@ -0,0 +1,14 @@ + + + + AmbigousParm2.java + 14 + Casting c to A is redundant + + + AmbigousParm2.java + 15 + Casting a to A is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/AmbigousParm2/src/.AmbigousParm2.java.swp b/java/java-tests/testData/inspection/redundantCast/AmbigousParm2/src/.AmbigousParm2.java.swp new file mode 100644 index 0000000000000000000000000000000000000000..17471d1ee90b7fdc8399d3a270061fdc695a97b5 GIT binary patch literal 12288 zcmeI&%}?7f7zS{|h1wTv};LD3f-Xpi0Yy0}>Kq_cyLL862mwogP4A|2nTj zCoxsP8T43s+UBw2D8FNq`tax3IejliO2(#Af5Iu3>7Iu_O^ zuUrKM1R(I7z_+8(;lchM?QCB?w*)-|AOHafKmY;|fB*y_@Jz3j+cWfB*y_009U<00Izz00bZafp;RnGkNC!L8&hv<%Z<>fBp|(EWcmM zUzxv`u;fxQk&Gk$3y{0uX=z1Rwwb2tWV=5P$##An+Ch{wN~qKZu&apc?Y< zN3Mt}Vmc;i^s?xuedpZ26@8%}kLF$Ntfob4XQbIV69!Y#L%LIWx4v!$6RM@@ueNOD wB7L(wT|17Ep1cyzjY%ftT^^^VWShA&jqyp=eWgj>PUSc9!!Oom<+#f85AG#*o&W#< literal 0 HcmV?d00001 diff --git a/java/java-tests/testData/inspection/redundantCast/AmbigousParm2/src/AmbigousParm2.java b/java/java-tests/testData/inspection/redundantCast/AmbigousParm2/src/AmbigousParm2.java new file mode 100644 index 000000000000..714aee653550 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/AmbigousParm2/src/AmbigousParm2.java @@ -0,0 +1,17 @@ +interface A {} + +interface B {} + +class C implements A, B {} + +public class AmbigousParameter { + void m(A a) {} + void m(B b) {} + + public void caller(C c) { + m((A)c); + m((A)null); + A a = (A)c; + m((A)a); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/AmbigousParm3/expected.xml b/java/java-tests/testData/inspection/redundantCast/AmbigousParm3/expected.xml new file mode 100644 index 000000000000..e211556e6065 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/AmbigousParm3/expected.xml @@ -0,0 +1,9 @@ + + + + AmbigousParm3.java + 13 + Casting null to A is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/AmbigousParm3/src/AmbigousParm3.java b/java/java-tests/testData/inspection/redundantCast/AmbigousParm3/src/AmbigousParm3.java new file mode 100644 index 000000000000..70356c0c8016 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/AmbigousParm3/src/AmbigousParm3.java @@ -0,0 +1,15 @@ +interface A {} +interface B {} + +public class AmbigousParameter { + public void f(A a) {} + public void f(B b) {} + public void f(Object o) {} + + public void g(Object o) {} + + public void caller() { + f((A)null); + g((A)null); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/AmbigousParm4/expected.xml b/java/java-tests/testData/inspection/redundantCast/AmbigousParm4/expected.xml new file mode 100644 index 000000000000..9ac879d78616 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/AmbigousParm4/expected.xml @@ -0,0 +1,3 @@ + + + diff --git a/java/java-tests/testData/inspection/redundantCast/AmbigousParm4/src/AmbigousParm4.java b/java/java-tests/testData/inspection/redundantCast/AmbigousParm4/src/AmbigousParm4.java new file mode 100644 index 000000000000..3ca606903d1b --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/AmbigousParm4/src/AmbigousParm4.java @@ -0,0 +1,8 @@ +import javax.swing.*; +import java.awt.*; + +public class AmbigousParameter { + public void caller() { + new JDialog((Frame)null, "Title", true); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/AmbigousParm5/expected.xml b/java/java-tests/testData/inspection/redundantCast/AmbigousParm5/expected.xml new file mode 100644 index 000000000000..9ac879d78616 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/AmbigousParm5/expected.xml @@ -0,0 +1,3 @@ + + + diff --git a/java/java-tests/testData/inspection/redundantCast/AmbigousParm5/src/AmbigousParm5.java b/java/java-tests/testData/inspection/redundantCast/AmbigousParm5/src/AmbigousParm5.java new file mode 100644 index 000000000000..fb61553a80bc --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/AmbigousParm5/src/AmbigousParm5.java @@ -0,0 +1,8 @@ +import javax.swing.*; +import java.awt.*; + +public class AmbigousParameter { + public void caller() { + new JDialog( ((Frame)null), "Title", true); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/AnyOfTwo/expected.xml b/java/java-tests/testData/inspection/redundantCast/AnyOfTwo/expected.xml new file mode 100644 index 000000000000..9ac879d78616 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/AnyOfTwo/expected.xml @@ -0,0 +1,3 @@ + + + diff --git a/java/java-tests/testData/inspection/redundantCast/AnyOfTwo/src/AnyOfTwo.java b/java/java-tests/testData/inspection/redundantCast/AnyOfTwo/src/AnyOfTwo.java new file mode 100644 index 000000000000..fb922acf6071 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/AnyOfTwo/src/AnyOfTwo.java @@ -0,0 +1,11 @@ +package com; + +public class Test { + static void f(Object s, Object o){} + static void f(String s, String o){} + + void foo(){ + Object o; + f((String)o, (String)o); + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/Assignment1/expected.xml b/java/java-tests/testData/inspection/redundantCast/Assignment1/expected.xml new file mode 100644 index 000000000000..706f86ed9ae0 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/Assignment1/expected.xml @@ -0,0 +1,9 @@ + + + + Assignment1.java + 4 + Casting null to String is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/Assignment1/src/Assignment1.java b/java/java-tests/testData/inspection/redundantCast/Assignment1/src/Assignment1.java new file mode 100644 index 000000000000..0ff6c4b45cad --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/Assignment1/src/Assignment1.java @@ -0,0 +1,6 @@ +class Test{ + static f(){ + Object o; + o = (String)null; + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/ComparingToNull/expected.xml b/java/java-tests/testData/inspection/redundantCast/ComparingToNull/expected.xml new file mode 100644 index 000000000000..c942f37a7bd0 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/ComparingToNull/expected.xml @@ -0,0 +1,9 @@ + + + + Test.java + 4 + Casting o to String is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/ComparingToNull/src/Test.java b/java/java-tests/testData/inspection/redundantCast/ComparingToNull/src/Test.java new file mode 100644 index 000000000000..68d9829f951b --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/ComparingToNull/src/Test.java @@ -0,0 +1,6 @@ +public class Test { + public boolean foo() { + Object o = null; + return (String) o == null; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/DifferentFields/expected.xml b/java/java-tests/testData/inspection/redundantCast/DifferentFields/expected.xml new file mode 100644 index 000000000000..5e933496b9cf --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/DifferentFields/expected.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/DifferentFields/src/Test.java b/java/java-tests/testData/inspection/redundantCast/DifferentFields/src/Test.java new file mode 100644 index 000000000000..df25a830ee47 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/DifferentFields/src/Test.java @@ -0,0 +1,12 @@ +class Y { + int size = 4; +} + +class Z extends Y { + int size = 5; + + public static void main(String[] args) { + Z z = new Z(); + System.out.println("z.size = " + ((Y)z).size); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/DoubleCast1/expected.xml b/java/java-tests/testData/inspection/redundantCast/DoubleCast1/expected.xml new file mode 100644 index 000000000000..89a59ecef41a --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/DoubleCast1/expected.xml @@ -0,0 +1,9 @@ + + + + DoubleCast1.java + 7 + Casting o to List is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/DoubleCast1/src/DoubleCast1.java b/java/java-tests/testData/inspection/redundantCast/DoubleCast1/src/DoubleCast1.java new file mode 100644 index 000000000000..f05698dc1f8f --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/DoubleCast1/src/DoubleCast1.java @@ -0,0 +1,9 @@ +import java.util.List; +import java.util.ArrayList; + +class Test{ + static f(){ + Object o; + ArrayList list = (ArrayList)(List)o; + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/DoubleCast2/expected.xml b/java/java-tests/testData/inspection/redundantCast/DoubleCast2/expected.xml new file mode 100644 index 000000000000..56829e12fe37 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/DoubleCast2/expected.xml @@ -0,0 +1,9 @@ + + + + DoubleCast2.java + 7 + Casting o to List is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/DoubleCast2/src/DoubleCast2.java b/java/java-tests/testData/inspection/redundantCast/DoubleCast2/src/DoubleCast2.java new file mode 100644 index 000000000000..8bb8b64e0805 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/DoubleCast2/src/DoubleCast2.java @@ -0,0 +1,9 @@ +import java.util.List; +import java.util.ArrayList; + +class Test{ + static f(){ + Object o; + ArrayList list = (ArrayList)((List)o); + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/DoubleCast3/expected.xml b/java/java-tests/testData/inspection/redundantCast/DoubleCast3/expected.xml new file mode 100644 index 000000000000..e8c201ce3315 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/DoubleCast3/expected.xml @@ -0,0 +1,14 @@ + + + + DoubleCast3.java + 4 + Casting '(String) o' to String is redundant + + + DoubleCast3.java + 4 + Casting 'o' to String is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/DoubleCast3/src/DoubleCast3.java b/java/java-tests/testData/inspection/redundantCast/DoubleCast3/src/DoubleCast3.java new file mode 100644 index 000000000000..36da9cba2007 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/DoubleCast3/src/DoubleCast3.java @@ -0,0 +1,6 @@ +class Test{ + static f(){ + Object o; + String s = (String) (String) o; + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/DoubleCast4/expected.xml b/java/java-tests/testData/inspection/redundantCast/DoubleCast4/expected.xml new file mode 100644 index 000000000000..70d5cfefdd37 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/DoubleCast4/expected.xml @@ -0,0 +1,14 @@ + + + + DoubleCast4.java + 4 + Casting '(String) o' to Object is redundant + + + DoubleCast4.java + 4 + Casting 'o' to String is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/DoubleCast4/src/DoubleCast4.java b/java/java-tests/testData/inspection/redundantCast/DoubleCast4/src/DoubleCast4.java new file mode 100644 index 000000000000..24b90094595a --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/DoubleCast4/src/DoubleCast4.java @@ -0,0 +1,6 @@ +class Test{ + static f(){ + Object o; + Object o2 = (Object) (String) o; + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/DoubleCast5/expected.xml b/java/java-tests/testData/inspection/redundantCast/DoubleCast5/expected.xml new file mode 100644 index 000000000000..9ac879d78616 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/DoubleCast5/expected.xml @@ -0,0 +1,3 @@ + + + diff --git a/java/java-tests/testData/inspection/redundantCast/DoubleCast5/src/DoubleCast5.java b/java/java-tests/testData/inspection/redundantCast/DoubleCast5/src/DoubleCast5.java new file mode 100644 index 000000000000..b0d970329170 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/DoubleCast5/src/DoubleCast5.java @@ -0,0 +1,5 @@ +class Test{ + static f(double a){ + double b = (double)(int)a / 100; + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/IDEADEV15170/expected.xml b/java/java-tests/testData/inspection/redundantCast/IDEADEV15170/expected.xml new file mode 100644 index 000000000000..796a315e49f5 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/IDEADEV15170/expected.xml @@ -0,0 +1,2 @@ + + diff --git a/java/java-tests/testData/inspection/redundantCast/IDEADEV15170/src/Test.java b/java/java-tests/testData/inspection/redundantCast/IDEADEV15170/src/Test.java new file mode 100644 index 000000000000..5bda42c6e8e6 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/IDEADEV15170/src/Test.java @@ -0,0 +1,16 @@ +class A { + void foo() throws Exception {} +} + +class B extends A { + void foo() { + + } +} + +class C { + { + A a = new B(); + ((B) a).foo(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/IDEADEV25675/expected.xml b/java/java-tests/testData/inspection/redundantCast/IDEADEV25675/expected.xml new file mode 100644 index 000000000000..c440a49c4378 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/IDEADEV25675/expected.xml @@ -0,0 +1,9 @@ + + + + A.java + 3 + Casting 'array' to String[] is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/IDEADEV25675/src/A.java b/java/java-tests/testData/inspection/redundantCast/IDEADEV25675/src/A.java new file mode 100644 index 000000000000..cbead21aa913 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/IDEADEV25675/src/A.java @@ -0,0 +1,9 @@ +public class Test { + public void foo(Object[] array) { + ((String[]) array)[0] = " "; + } + + public void bar(String[] array) { + ((Object[]) array)[0] = new Object(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/IDEADEV6818/expected.xml b/java/java-tests/testData/inspection/redundantCast/IDEADEV6818/expected.xml new file mode 100644 index 000000000000..0d178e909db5 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/IDEADEV6818/expected.xml @@ -0,0 +1,9 @@ + + + + A.java + 13 + Casting prices.get(null) to BidAsk is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/IDEADEV6818/src/A.java b/java/java-tests/testData/inspection/redundantCast/IDEADEV6818/src/A.java new file mode 100644 index 000000000000..00e6fbd46922 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/IDEADEV6818/src/A.java @@ -0,0 +1,15 @@ +class Getter { + BidAsk get(String s) { + return null; + } +} + +class BidAsk { + public Object getOpenQuote; +} + +public class A { + static Object f(Getter prices) { + return System.currentTimeMillis() == 2.0 ? ((BidAsk) prices.get(null)).getOpenQuote : null; + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/InConditional/expected.xml b/java/java-tests/testData/inspection/redundantCast/InConditional/expected.xml new file mode 100644 index 000000000000..d93fb1cec86d --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/InConditional/expected.xml @@ -0,0 +1,9 @@ + + + + A.java + 3 + Redundant type cast + Casting <code>null</code> to <code>String</code> is redundant + + diff --git a/java/java-tests/testData/inspection/redundantCast/InConditional/src/A.java b/java/java-tests/testData/inspection/redundantCast/InConditional/src/A.java new file mode 100644 index 000000000000..24ac60c1f4e0 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/InConditional/src/A.java @@ -0,0 +1,5 @@ +class A { + { + String s = true ? "" : (String) null; //cast is needed + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/Inaccessible/expected.xml b/java/java-tests/testData/inspection/redundantCast/Inaccessible/expected.xml new file mode 100644 index 000000000000..580a889dbfd6 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/Inaccessible/expected.xml @@ -0,0 +1,3 @@ + + + diff --git a/java/java-tests/testData/inspection/redundantCast/Inaccessible/src/Test.java b/java/java-tests/testData/inspection/redundantCast/Inaccessible/src/Test.java new file mode 100644 index 000000000000..4c36d5bd0c28 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/Inaccessible/src/Test.java @@ -0,0 +1,12 @@ +class Entry { + private final Object obj; + protected Entry(Directory parent) { + obj = ((Entry)parent).obj; //cast is needed because 'obj' is not visible with 'Directory' access class + } +} + +class Directory extends Entry { + public Directory(Directory parent) { + super(parent); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/Initializer1/expected.xml b/java/java-tests/testData/inspection/redundantCast/Initializer1/expected.xml new file mode 100644 index 000000000000..5054e5bbb001 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/Initializer1/expected.xml @@ -0,0 +1,9 @@ + + + + Initializer1.java + 3 + Casting null to String is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/Initializer1/src/Initializer1.java b/java/java-tests/testData/inspection/redundantCast/Initializer1/src/Initializer1.java new file mode 100644 index 000000000000..de9bef7abe55 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/Initializer1/src/Initializer1.java @@ -0,0 +1,5 @@ +class Test{ + static f(){ + Object o = (String)null; + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/IntToDouble/expected.xml b/java/java-tests/testData/inspection/redundantCast/IntToDouble/expected.xml new file mode 100644 index 000000000000..9ac879d78616 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/IntToDouble/expected.xml @@ -0,0 +1,3 @@ + + + diff --git a/java/java-tests/testData/inspection/redundantCast/IntToDouble/src/Test.java b/java/java-tests/testData/inspection/redundantCast/IntToDouble/src/Test.java new file mode 100644 index 000000000000..0790268df735 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/IntToDouble/src/Test.java @@ -0,0 +1,5 @@ +class A{ + void f(){ + double d = (double)1 / 5; + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/NestedCast/expected.xml b/java/java-tests/testData/inspection/redundantCast/NestedCast/expected.xml new file mode 100644 index 000000000000..ee594a2a237a --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/NestedCast/expected.xml @@ -0,0 +1,9 @@ + + + + CastTest.java + 7 + Redundant type cast + Casting <code>ct1</code> to <code>CastTest</code> is redundant + + \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/NestedCast/src/CastTest.java b/java/java-tests/testData/inspection/redundantCast/NestedCast/src/CastTest.java new file mode 100644 index 000000000000..84bbabbf665e --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/NestedCast/src/CastTest.java @@ -0,0 +1,9 @@ +class CastTest +{ + public static void main (String[] args) throws CloneNotSupportedException + { + CastTest ct1 = new CastTest (); + // The cast of ct1 is obviously redundant (although the cast of the result is necessary) + CastTest ct2 = (CastTest) ((CastTest)ct1).clone(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/NestedThings/expected.xml b/java/java-tests/testData/inspection/redundantCast/NestedThings/expected.xml new file mode 100644 index 000000000000..c381c3410379 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/NestedThings/expected.xml @@ -0,0 +1,9 @@ + + + + NestedThings.java + 13 + Casting "" to String is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/NestedThings/src/NestedThings.java b/java/java-tests/testData/inspection/redundantCast/NestedThings/src/NestedThings.java new file mode 100644 index 000000000000..ab19af9b39d9 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/NestedThings/src/NestedThings.java @@ -0,0 +1,26 @@ +class XXX { + String f() { + new Runnable() { + public void run() { + new Runnable() { + public void run() { + new Runnable() { + public void run() { + new Runnable() { + public void run() { + new Runnable() { + public void run() { + String s = (String)""; + } + }; + } + }; + } + }; + } + }; + } + }; + return ""; + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/New1/expected.xml b/java/java-tests/testData/inspection/redundantCast/New1/expected.xml new file mode 100644 index 000000000000..a35fe675e263 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/New1/expected.xml @@ -0,0 +1,9 @@ + + + + New1.java + 5 + Casting null to String is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/New1/src/New1.java b/java/java-tests/testData/inspection/redundantCast/New1/src/New1.java new file mode 100644 index 000000000000..f1a6625c1b93 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/New1/src/New1.java @@ -0,0 +1,7 @@ +class Test{ + Test(Object o){} + + static f(){ + new Test((String)null); + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/OneOfTwo/expected.xml b/java/java-tests/testData/inspection/redundantCast/OneOfTwo/expected.xml new file mode 100644 index 000000000000..f19096e540f0 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/OneOfTwo/expected.xml @@ -0,0 +1,9 @@ + + + + OneOfTwo.java + 8 + Casting o to String is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/OneOfTwo/src/OneOfTwo.java b/java/java-tests/testData/inspection/redundantCast/OneOfTwo/src/OneOfTwo.java new file mode 100644 index 000000000000..dff6850ac77f --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/OneOfTwo/src/OneOfTwo.java @@ -0,0 +1,10 @@ +package com; + +public class Test { + static void f(String s, Object o){} + + void foo(){ + Object o; + f((String)o, (String)o); + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/SCR11555/expected.xml b/java/java-tests/testData/inspection/redundantCast/SCR11555/expected.xml new file mode 100644 index 000000000000..9ac879d78616 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/SCR11555/expected.xml @@ -0,0 +1,3 @@ + + + diff --git a/java/java-tests/testData/inspection/redundantCast/SCR11555/src/Test.java b/java/java-tests/testData/inspection/redundantCast/SCR11555/src/Test.java new file mode 100644 index 000000000000..439a70b1a530 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/SCR11555/src/Test.java @@ -0,0 +1,9 @@ + +import java.awt.*; + +public class Test { + void foo(){ + Component c = null; + ((Frame) c).show(); + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/SCR13397/expected.xml b/java/java-tests/testData/inspection/redundantCast/SCR13397/expected.xml new file mode 100644 index 000000000000..ec272abeaa3a --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/SCR13397/expected.xml @@ -0,0 +1,3 @@ + + + diff --git a/java/java-tests/testData/inspection/redundantCast/SCR13397/src/Test.java b/java/java-tests/testData/inspection/redundantCast/SCR13397/src/Test.java new file mode 100644 index 000000000000..772f47c2a11f --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/SCR13397/src/Test.java @@ -0,0 +1,21 @@ +class RedundantCastProblem { + public abstract static class Top { + public String f(Object o) { + return "Top.f(Object)"; + } + } + + public static class Sub extends Top { + public String f(String s) { + return "Middle.f(String)"; + } + } + + public static void main(String[] args) { + Sub sub = new Sub(); + String aString = ""; + + System.out.println(((Top)sub).f(aString)); + System.out.println(sub.f(aString)); + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/SCR14502/expected.xml b/java/java-tests/testData/inspection/redundantCast/SCR14502/expected.xml new file mode 100644 index 000000000000..ea60f9b517be --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/SCR14502/expected.xml @@ -0,0 +1,9 @@ + + + + Test.java + 3 + Casting new Test() to Test is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/SCR14502/src/Test.java b/java/java-tests/testData/inspection/redundantCast/SCR14502/src/Test.java new file mode 100644 index 000000000000..844664c0506c --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/SCR14502/src/Test.java @@ -0,0 +1,5 @@ +public class Test { + public void foo(Test t) { + foo(((Test) new Test())); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/SCR14559/expected.xml b/java/java-tests/testData/inspection/redundantCast/SCR14559/expected.xml new file mode 100644 index 000000000000..ec272abeaa3a --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/SCR14559/expected.xml @@ -0,0 +1,3 @@ + + + diff --git a/java/java-tests/testData/inspection/redundantCast/SCR14559/src/Test.java b/java/java-tests/testData/inspection/redundantCast/SCR14559/src/Test.java new file mode 100644 index 000000000000..26eb609277c1 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/SCR14559/src/Test.java @@ -0,0 +1,15 @@ +public class Test { + class Super { + Object foo() { return new Object(); } + } + class Sub extends Super{ + String foo() { return ""; } + } + public String get(final Super obj) { + if (obj instanceof Sub) { + return ((Sub)obj).foo(); + } else { + return "The value is " + obj.foo(); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/SCR15236/expected.xml b/java/java-tests/testData/inspection/redundantCast/SCR15236/expected.xml new file mode 100644 index 000000000000..ab033e5de25a --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/SCR15236/expected.xml @@ -0,0 +1,8 @@ + + + + Test.java + 3 + Casting s to String is redundant + + diff --git a/java/java-tests/testData/inspection/redundantCast/SCR15236/src/Test.java b/java/java-tests/testData/inspection/redundantCast/SCR15236/src/Test.java new file mode 100644 index 000000000000..dbb5fe9f13a7 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/SCR15236/src/Test.java @@ -0,0 +1,5 @@ +public class Test { + public void test(Object s) { + Object o = ((String) s); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/SCR6907/expected.xml b/java/java-tests/testData/inspection/redundantCast/SCR6907/expected.xml new file mode 100644 index 000000000000..ca36c52f7ead --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/SCR6907/expected.xml @@ -0,0 +1,9 @@ + + + + Test.java + 7 + Casting null to String is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/SCR6907/src/Test.java b/java/java-tests/testData/inspection/redundantCast/SCR6907/src/Test.java new file mode 100644 index 000000000000..6747030c9222 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/SCR6907/src/Test.java @@ -0,0 +1,9 @@ + +public class Test { + void foo(String msg){} + void foo(Object o){} + + void method(){ + foo((String)null); + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/ShortToShort/expected.xml b/java/java-tests/testData/inspection/redundantCast/ShortToShort/expected.xml new file mode 100644 index 000000000000..a30bea023369 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/ShortToShort/expected.xml @@ -0,0 +1,9 @@ + + + + Test.java + 4 + Casting v to short is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/ShortToShort/src/Test.java b/java/java-tests/testData/inspection/redundantCast/ShortToShort/src/Test.java new file mode 100644 index 000000000000..ede4aa3808a6 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/ShortToShort/src/Test.java @@ -0,0 +1,7 @@ +class Test{ + short foo(){ + short v = 0; + short s = (short)v; + return s; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/ShortVsInt/expected.xml b/java/java-tests/testData/inspection/redundantCast/ShortVsInt/expected.xml new file mode 100644 index 000000000000..9ac879d78616 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/ShortVsInt/expected.xml @@ -0,0 +1,3 @@ + + + diff --git a/java/java-tests/testData/inspection/redundantCast/ShortVsInt/src/ShortVsInt.java b/java/java-tests/testData/inspection/redundantCast/ShortVsInt/src/ShortVsInt.java new file mode 100644 index 000000000000..d0b690d83fc8 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/ShortVsInt/src/ShortVsInt.java @@ -0,0 +1,8 @@ +class A{ + String toHex(int i) {} + String toHex(short i) {} + + void f(){ + String result = toHex((short)'i'); + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/Truncation/expected.xml b/java/java-tests/testData/inspection/redundantCast/Truncation/expected.xml new file mode 100644 index 000000000000..9ac879d78616 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/Truncation/expected.xml @@ -0,0 +1,3 @@ + + + diff --git a/java/java-tests/testData/inspection/redundantCast/Truncation/src/Test.java b/java/java-tests/testData/inspection/redundantCast/Truncation/src/Test.java new file mode 100644 index 000000000000..68707f630414 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/Truncation/src/Test.java @@ -0,0 +1,5 @@ +class A{ + void f(){ + double d = (int) 1.5; + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/VirtualMethod1/expected.xml b/java/java-tests/testData/inspection/redundantCast/VirtualMethod1/expected.xml new file mode 100644 index 000000000000..e2ed2b831218 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/VirtualMethod1/expected.xml @@ -0,0 +1,9 @@ + + + + VirtualMethod1.java + 12 + Casting a B is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/VirtualMethod1/src/VirtualMethod1.java b/java/java-tests/testData/inspection/redundantCast/VirtualMethod1/src/VirtualMethod1.java new file mode 100644 index 000000000000..f6d174179343 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/VirtualMethod1/src/VirtualMethod1.java @@ -0,0 +1,14 @@ +class A{ + void f(){} +} + +class B extends A{ + void f(){} +} + +class Test{ + static foo(){ + A a; + ((B)a).f(); + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/VirtualMethod2/expected.xml b/java/java-tests/testData/inspection/redundantCast/VirtualMethod2/expected.xml new file mode 100644 index 000000000000..9ac879d78616 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/VirtualMethod2/expected.xml @@ -0,0 +1,3 @@ + + + diff --git a/java/java-tests/testData/inspection/redundantCast/VirtualMethod2/src/VirtualMethod2.java b/java/java-tests/testData/inspection/redundantCast/VirtualMethod2/src/VirtualMethod2.java new file mode 100644 index 000000000000..ab027cff9687 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/VirtualMethod2/src/VirtualMethod2.java @@ -0,0 +1,14 @@ +class A{ + private void f(){} +} + +class B extends A{ + void f(){} +} + +class Test{ + static foo(){ + A a; + ((B)a).f(); + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/VirtualMethod3/expected.xml b/java/java-tests/testData/inspection/redundantCast/VirtualMethod3/expected.xml new file mode 100644 index 000000000000..bbf660d0e243 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/VirtualMethod3/expected.xml @@ -0,0 +1,9 @@ + + + + VirtualMethod3.java + 4 + Casting o to String is redundant + + + diff --git a/java/java-tests/testData/inspection/redundantCast/VirtualMethod3/src/VirtualMethod3.java b/java/java-tests/testData/inspection/redundantCast/VirtualMethod3/src/VirtualMethod3.java new file mode 100644 index 000000000000..9c13b2ee2a30 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/VirtualMethod3/src/VirtualMethod3.java @@ -0,0 +1,6 @@ +class Test{ + static foo(){ + Object o; + boolean res = ((String)o).equals(null); + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/generics/BoxingInRef/expected.xml b/java/java-tests/testData/inspection/redundantCast/generics/BoxingInRef/expected.xml new file mode 100644 index 000000000000..580a889dbfd6 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/BoxingInRef/expected.xml @@ -0,0 +1,3 @@ + + + diff --git a/java/java-tests/testData/inspection/redundantCast/generics/BoxingInRef/src/Test.java b/java/java-tests/testData/inspection/redundantCast/generics/BoxingInRef/src/Test.java new file mode 100644 index 000000000000..dd4dd2d8df7b --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/BoxingInRef/src/Test.java @@ -0,0 +1,7 @@ +//This is a test for JDK_15 LanguageLevel +public class Test { + void foo () { + int x = 4; + ((Integer) x).toString(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/generics/EnumConstant/expected.xml b/java/java-tests/testData/inspection/redundantCast/generics/EnumConstant/expected.xml new file mode 100644 index 000000000000..c8c865b6cf6e --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/EnumConstant/expected.xml @@ -0,0 +1,9 @@ + + + + Test.java + 3 + Redundant type cast + Casting <code>""</code> to <code>String</code> is redundant + + \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/generics/EnumConstant/src/Test.java b/java/java-tests/testData/inspection/redundantCast/generics/EnumConstant/src/Test.java new file mode 100644 index 000000000000..1dcbdccb6477 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/EnumConstant/src/Test.java @@ -0,0 +1,7 @@ + +enum Test { + A((String) ""); + + Test(String s) { + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/generics/Ignore/expected.xml b/java/java-tests/testData/inspection/redundantCast/generics/Ignore/expected.xml new file mode 100644 index 000000000000..e6aeaad02d23 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/Ignore/expected.xml @@ -0,0 +1,14 @@ + + + Test.java + 11 + Redundant type cast + Casting <code>this</code> to <code>CastPreventsNPEDetection</code> is redundant + + + Suspicious.java + 9 + Redundant type cast + Casting <code>str</code> to <code>String</code> is redundant + + \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/generics/Ignore/scr/Suspicious.java b/java/java-tests/testData/inspection/redundantCast/generics/Ignore/scr/Suspicious.java new file mode 100644 index 000000000000..45719b08de72 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/Ignore/scr/Suspicious.java @@ -0,0 +1,12 @@ +import java.util.Map; + +public class Suspicious { + Map map; + + void f(Object s){ + String str = (String) s; + map.remove((String)s); + map.remove((String)str); + } + +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/generics/Ignore/scr/Test.java b/java/java-tests/testData/inspection/redundantCast/generics/Ignore/scr/Test.java new file mode 100644 index 000000000000..f712d3338e9d --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/Ignore/scr/Test.java @@ -0,0 +1,21 @@ +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +class CastPreventsNPEDetection { + @Nullable Object getParent() { + return null; + } + + void f() { + ((ChildCastImpl)this).getParent().toString(); + ((CastPreventsNPEDetection)this).getParent().toString(); + } +} + +class ChildCastImpl extends CastPreventsNPEDetection { + @NotNull + @Override + Object getParent() { + return super.getParent(); //To change body of overridden methods use File | Settings | File Templates. + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/generics/Inference1/expected.xml b/java/java-tests/testData/inspection/redundantCast/generics/Inference1/expected.xml new file mode 100644 index 000000000000..580a889dbfd6 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/Inference1/expected.xml @@ -0,0 +1,3 @@ + + + diff --git a/java/java-tests/testData/inspection/redundantCast/generics/Inference1/src/Test.java b/java/java-tests/testData/inspection/redundantCast/generics/Inference1/src/Test.java new file mode 100644 index 000000000000..7e3836a62573 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/Inference1/src/Test.java @@ -0,0 +1,20 @@ +import java.util.Map; +class Test2 { + public String s; + public void maina(Object key, Map parameters) { + s = ((String[]) parameters.get(key))[0]; + } +} + +public class Test { + static class SomeClass { + public T getX() { + return null; + } + } + + public static void main(String[] args) { + //cast is needed for 'String' to be infered! + System.getProperty((String)new SomeClass().getX()); + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/generics/Inference2/expected.xml b/java/java-tests/testData/inspection/redundantCast/generics/Inference2/expected.xml new file mode 100644 index 000000000000..5e933496b9cf --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/Inference2/expected.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/generics/Inference2/src/Test.java b/java/java-tests/testData/inspection/redundantCast/generics/Inference2/src/Test.java new file mode 100644 index 000000000000..c121c0569a29 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/Inference2/src/Test.java @@ -0,0 +1,32 @@ +import java.util.HashSet; +import java.util.Set; + +final class Pair { + public final A first; + public final B second; + + public Pair(A first, B second) { + this.first = first; + this.second = second; + } + + public final A getFirst() { + return first; + } + + public final B getSecond() { + return second; + } + + public static Pair create(A first, B second) { + return new Pair(first, second); + } + +} + +class Test { + final Set strings = new HashSet(); + final Pair, Set> x = Boolean.TRUE.booleanValue() + ? Pair.create(strings, strings) + : Pair.create(((Set) null), (Set) null); //these casts are not redundant +} diff --git a/java/java-tests/testData/inspection/redundantCast/generics/Inference3/expected.xml b/java/java-tests/testData/inspection/redundantCast/generics/Inference3/expected.xml new file mode 100644 index 000000000000..380236bb5a9d --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/Inference3/expected.xml @@ -0,0 +1,13 @@ + + + + Test.java + 31 + Casting 'null' to 'Set<String>' is redundant + + + Test.java + 31 + Casting 'null' to 'Set<String>' is redundant + + \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/generics/Inference3/src/Test.java b/java/java-tests/testData/inspection/redundantCast/generics/Inference3/src/Test.java new file mode 100644 index 000000000000..64cf6d2e0a12 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/Inference3/src/Test.java @@ -0,0 +1,32 @@ +import java.util.HashSet; +import java.util.Set; + +final class Pair { + public final A first; + public final B second; + + public Pair(A first, B second) { + this.first = first; + this.second = second; + } + + public final A getFirst() { + return first; + } + + public final B getSecond() { + return second; + } + + public static Pair create(A first, A second) { + return new Pair(first, second); + } + +} + +class Test { + final Set strings = new HashSet(); + final Pair, Set> x = Boolean.TRUE.booleanValue() + ? Pair.create(strings, strings) + : Pair.create(((Set) null), (Set) null); //both casts are marked, but one is required for correct inference +} diff --git a/java/java-tests/testData/inspection/redundantCast/generics/NullInVarargsParameter/expected.xml b/java/java-tests/testData/inspection/redundantCast/generics/NullInVarargsParameter/expected.xml new file mode 100644 index 000000000000..48906bd0df8b --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/NullInVarargsParameter/expected.xml @@ -0,0 +1,13 @@ + + + + Test.java + 8 + Casting null to Class[] is redundant + + + Test.java + 9 + Casting null to Class[] is redundant + + diff --git a/java/java-tests/testData/inspection/redundantCast/generics/NullInVarargsParameter/src/Test.java b/java/java-tests/testData/inspection/redundantCast/generics/NullInVarargsParameter/src/Test.java new file mode 100644 index 000000000000..c17d2647711d --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/NullInVarargsParameter/src/Test.java @@ -0,0 +1,11 @@ +class Test { + void f(Class... classes) { + } + + void g() { + f(((Class[])null)); + f(((Class)null)); + f(((Class)null), + ((Class)null)); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/generics/RawCast/expected.xml b/java/java-tests/testData/inspection/redundantCast/generics/RawCast/expected.xml new file mode 100644 index 000000000000..4704d91e891d --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/RawCast/expected.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/generics/RawCast/src/Test.java b/java/java-tests/testData/inspection/redundantCast/generics/RawCast/src/Test.java new file mode 100644 index 000000000000..2039039b8691 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/RawCast/src/Test.java @@ -0,0 +1,11 @@ +import java.util.Map; +import java.util.Properties; + +public class Main { + public static void main(String[] args) throws Exception { + Properties properties = new Properties(); + + Map map = (Map) properties; + System.out.println(map); + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/generics/RawCastsToAvoidIncompatibility/expected.xml b/java/java-tests/testData/inspection/redundantCast/generics/RawCastsToAvoidIncompatibility/expected.xml new file mode 100644 index 000000000000..c63c9258595e --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/RawCastsToAvoidIncompatibility/expected.xml @@ -0,0 +1,2 @@ + + diff --git a/java/java-tests/testData/inspection/redundantCast/generics/RawCastsToAvoidIncompatibility/src/CastCast.java b/java/java-tests/testData/inspection/redundantCast/generics/RawCastsToAvoidIncompatibility/src/CastCast.java new file mode 100644 index 000000000000..ef81defd32fc --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/RawCastsToAvoidIncompatibility/src/CastCast.java @@ -0,0 +1,13 @@ +import java.util.List; + + +class RedundantCasts { + List myTranslators; + void t() { + b((List) (List)myTranslators); + } + + void b(List l){} +} +interface Compiler{} +interface TranslatingCompiler extends Compiler{} diff --git a/java/java-tests/testData/inspection/redundantCast/generics/RawCastsToAvoidIncompatibility/src/ReturnValue.java b/java/java-tests/testData/inspection/redundantCast/generics/RawCastsToAvoidIncompatibility/src/ReturnValue.java new file mode 100644 index 000000000000..f30870b78d53 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/RawCastsToAvoidIncompatibility/src/ReturnValue.java @@ -0,0 +1,16 @@ +interface GenericValue { + T getValue(); +} + +interface GenericAttValue extends GenericValue { +} + +interface Property { + GenericAttValue getValue(); +} + +class RedCast { + public GenericValue getDataSourceName(Property property) { + return (GenericValue) property.getValue(); + } +} diff --git a/java/java-tests/testData/inspection/redundantCast/generics/WrapperToPrimitiveCast/expected.xml b/java/java-tests/testData/inspection/redundantCast/generics/WrapperToPrimitiveCast/expected.xml new file mode 100644 index 000000000000..7574cc740a49 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/WrapperToPrimitiveCast/expected.xml @@ -0,0 +1,48 @@ + + + + WrapperToPrimitiveCast.java + 8 + Redundant type cast + Casting <code>J</code> to <code>int</code> is redundant + + + + WrapperToPrimitiveCast.java + 9 + Redundant type cast + Casting <code>I</code> to <code>int</code> is redundant + + + + WrapperToPrimitiveCast.java + 12 + Redundant type cast + Casting <code>p</code> to <code>Integer</code> is redundant + + + + WrapperToPrimitiveCast.java + 15 + Redundant type cast + Casting <code>p</code> to <code>Integer</code> is redundant + + + + + WrapperToPrimitiveCast.java + 24 + Redundant type cast + Casting <code>lnumber</code> to <code>long</code> is redundant + + + + + WrapperToPrimitiveCast.java + 24 + Redundant type cast + Casting <code>lnumber</code> to <code>long</code> is redundant + + + + diff --git a/java/java-tests/testData/inspection/redundantCast/generics/WrapperToPrimitiveCast/src/WrapperToPrimitiveCast.java b/java/java-tests/testData/inspection/redundantCast/generics/WrapperToPrimitiveCast/src/WrapperToPrimitiveCast.java new file mode 100644 index 000000000000..d9397902bf91 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/WrapperToPrimitiveCast/src/WrapperToPrimitiveCast.java @@ -0,0 +1,26 @@ +public class Main { + public static void main(String[] args) { + Integer J = 4; + Integer I = new Integer(4); + + System.out.println(I == J); + System.out.println((int) I == J); + int j = (int)J; + System.out.println((int) I == j); + + int p = 555555; + Integer W = (Integer) p; + System.out.println((Integer) p == W); + int w = W; + System.out.println((Integer) p == w); + + Integer test = 10; + double d = ((double)test/100); + + Double number = Double.valueOf(3); + long integerPart = (long) (double) number; + + Long lnumber = Long.valueOf(3); + long integerPartL = (long) (long) lnumber; + } +} diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast15Test.java b/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast15Test.java new file mode 100644 index 000000000000..9fc42f08319b --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast15Test.java @@ -0,0 +1,47 @@ +package com.intellij.codeInspection; + +import com.intellij.codeInspection.ex.LocalInspectionToolWrapper; +import com.intellij.codeInspection.redundantCast.RedundantCastInspection; +import com.intellij.openapi.roots.LanguageLevelProjectExtension; +import com.intellij.pom.java.LanguageLevel; +import com.intellij.testFramework.InspectionTestCase; + +public class RedundantCast15Test extends InspectionTestCase { + @Override + protected void setUp() throws Exception { + super.setUp(); + LanguageLevelProjectExtension.getInstance(myJavaFacade.getProject()).setLanguageLevel(LanguageLevel.JDK_1_5); + } + + + private void doTest() throws Exception { + final LocalInspectionToolWrapper tool = new LocalInspectionToolWrapper(new RedundantCastInspection()); + doTest("redundantCast/generics/" + getTestName(false), tool, "java 1.5"); + } + + public void testBoxingInRef() throws Exception { doTest(); } + + public void testInference1() throws Exception { doTest(); } + + public void testInference2() throws Exception { doTest(); } + + public void testInference3() throws Exception { doTest(); } + + public void testNullInVarargsParameter() throws Exception { doTest(); } + + public void testWrapperToPrimitiveCast() throws Exception { doTest(); } + + public void testEnumConstant() throws Exception { doTest(); } + + public void testRawCast() throws Exception { doTest();} + + public void testRawCastsToAvoidIncompatibility() throws Exception { doTest();} + + public void testIgnore() throws Exception { + final RedundantCastInspection castInspection = new RedundantCastInspection(); + castInspection.IGNORE_ANNOTATED_METHODS = true; + castInspection.IGNORE_SUSPICIOUS_METHOD_CALLS = true; + final LocalInspectionToolWrapper tool = new LocalInspectionToolWrapper(castInspection); + doTest("redundantCast/generics/" + getTestName(false), tool, "java 1.5"); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCastTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCastTest.java new file mode 100644 index 000000000000..0e04743e8dce --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCastTest.java @@ -0,0 +1,93 @@ +package com.intellij.codeInspection; + +import com.intellij.codeInspection.redundantCast.RedundantCastInspection; +import com.intellij.openapi.roots.LanguageLevelProjectExtension; +import com.intellij.pom.java.LanguageLevel; +import com.intellij.testFramework.InspectionTestCase; + +public class RedundantCastTest extends InspectionTestCase { + + @Override + protected void setUp() throws Exception { + super.setUp(); + LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_3); + } + + private void doTest() throws Exception { + doTest("redundantCast/" + getTestName(false), new RedundantCastInspection()); + } + + public void testAmbigousParm1() throws Exception { doTest(); } + + public void testAmbigousParm2() throws Exception { doTest(); } + + public void testAmbigousParm3() throws Exception { doTest(); } + + public void testAmbigousParm4() throws Exception { doTest(); } + + public void testAmbigousParm5() throws Exception { doTest(); } + + public void testOneOfTwo() throws Exception { doTest(); } + + public void testAnyOfTwo() throws Exception { doTest(); } + + public void testNew1() throws Exception { doTest(); } + + public void testAssignment1() throws Exception { doTest(); } + + public void testInitializer1() throws Exception { doTest(); } + + public void testShortToShort() throws Exception { doTest(); } + + public void testVirtualMethod1() throws Exception { doTest(); } + + public void testVirtualMethod2() throws Exception { doTest(); } + + public void testVirtualMethod3() throws Exception { doTest(); } + + public void testDoubleCast1() throws Exception { doTest(); } + + public void testDoubleCast2() throws Exception { doTest(); } + + public void testDoubleCast3() throws Exception { doTest(); } + + public void testDoubleCast4() throws Exception { doTest(); } + + public void testDoubleCast5() throws Exception { doTest(); } + + public void testShortVsInt() throws Exception { doTest(); } + + public void testTruncation() throws Exception { doTest(); } + + public void testIntToDouble() throws Exception { doTest(); } + + public void testSCR6907() throws Exception { doTest(); } + + public void testSCR11555() throws Exception { doTest(); } + + public void testSCR13397() throws Exception { doTest(); } + + public void testSCR14502() throws Exception { doTest(); } + + public void testSCR14559() throws Exception { doTest(); } + + public void testSCR15236() throws Exception { doTest(); } + + public void testComparingToNull() throws Exception { doTest(); } + + public void testInaccessible() throws Exception { doTest(); } + + public void testInConditional() throws Exception { doTest(); } + + public void testDifferentFields() throws Exception { doTest(); } + + public void testNestedThings() throws Exception { doTest(); } + + public void testIDEADEV6818() throws Exception { doTest(); } + + public void testIDEADEV15170() throws Exception { doTest(); } + + public void testIDEADEV25675() throws Exception { doTest(); } + + public void testNestedCast() throws Exception { doTest(); } +}