RedundantCast15Test -> LightCodeInsightFixtureTestCase

This commit is contained in:
Tagir Valeev
2018-09-14 15:22:23 +07:00
parent 0ee04c6289
commit 22f7e6eede
51 changed files with 27 additions and 594 deletions
@@ -1,16 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>8</line>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant type cast</problem_class>
<description>Casting &lt;code&gt;2&lt;/code&gt; to &lt;code&gt;Integer&lt;/code&gt; is redundant</description>
</problem>
<problem>
<file>Test.java</file>
<line>18</line>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant type cast</problem_class>
<description>Casting &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;Integer&lt;/code&gt; is redundant</description>
</problem>
</problems>
@@ -1,22 +0,0 @@
//This is a test for JDK_15 LanguageLevel
public class Test {
private Integer foo(String s, Integer i) {
return s == null ? i : (Integer)2;
}
private int foo1(String s, Integer i) {
return s == null ? i : (Integer)2;
}
}
class RedundantCastTest {
public static void main(String[] args) {
Integer foo = 5;
Integer bar = null;
print(args == null || args.length == 0 ? bar : (Integer) 1);
int i = args == null || args.length == 0 ? bar : (Integer) 1;
}
private static void print(Integer i) {}
}
@@ -1,3 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
</problems>
@@ -1,7 +0,0 @@
//This is a test for JDK_15 LanguageLevel
public class Test {
void foo () {
int x = 4;
((Integer) x).toString();
}
}
@@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -1,20 +0,0 @@
class TestCasting
{
public void testRedundantCast() throws Exception
{
Object o = getObject();
double d = 0.0;
if( o instanceof Integer )
{
d = (double) (Integer)o;
}
System.out.println( "d = " + d );
}
private Object getObject()
{
return new Integer(42);
}
}
@@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>7</line>
<description>Casting &lt;code&gt;e&lt;/code&gt; to &lt;code&gt;Ex&lt;/code&gt; is redundant</description>
</problem>
<problem>
<file>Test.java</file>
<line>3</line>
<description>Casting &lt;code&gt;e&lt;/code&gt; to &lt;code&gt;FileNotFoundEx&lt;/code&gt; is redundant</description>
</problem>
</problems>
@@ -1,13 +0,0 @@
public class RedundantCast{
void m(IOEx e) throws IOEx {
throw (FileNotFoundEx)e;
}
void foo(RuntimeException e) {
throw (Ex)e;
}
static class Ex extends RuntimeException {}
static class IOEx extends Exception {}
static class FileNotFoundEx extends Exception {}
}
@@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -1,9 +0,0 @@
import java.util.*;
import java.util.Map;
class Test {
{
Map<Number, String> map1 = null;
Map<Integer, String> map2 = (Map<Integer, String>) (Map<?, ?>) map1;
}
}
@@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>A.java</file>
<line>5</line>
<problem_class>Redundant type cast</problem_class>
<description>Casting &lt;code&gt;a&lt;/code&gt; to &lt;code&gt;AA&lt;/code&gt; is redundant</description>
</problem>
</problems>
@@ -1,32 +0,0 @@
import org.jetbrains.annotations.*;
class A {
static String doit(A a) {
String d = ((AA)a).danuna();
String notNull = ((AA)a).doadd();
return notNull + d;
}
@Nullable
String doadd() {
return null;
}
@NotNull
String danuna() {
return "";
}
}
class AA extends A {
@NotNull
String doadd() {
return "";
}
@Override
@NotNull
String danuna() {
return "";
}
}
@@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>3</line>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant type cast</problem_class>
<description>Casting &lt;code&gt;&quot;&quot;&lt;/code&gt; to &lt;code&gt;String&lt;/code&gt; is redundant</description>
</problem>
</problems>
@@ -1,7 +0,0 @@
enum Test {
A((String) "");
Test(String s) {
}
}
@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>7</line>
<description>Casting &lt;code&gt;list&lt;/code&gt; to &lt;code&gt;ArrayList&lt;String&gt;&lt;/code&gt; is redundant</description>
</problem>
</problems>
@@ -1,12 +0,0 @@
import java.util.ArrayList;
import java.util.List;
public class RedundantCast {
void redundantCasts() {
List<String> list = new ArrayList<>();
for (String s : (ArrayList<String>) list) {}
Object o = new ArrayList<>();
for (String s : (ArrayList<String>) o) {}
}
}
@@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -1,11 +0,0 @@
import java.util.ArrayList;
import java.util.List;
class SomeClass {
public void test() {
List<?> objects = new ArrayList<>();
for (String value : (Iterable<? extends String>) objects) {
System.out.println(value);
}
}
}
@@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -1,10 +0,0 @@
class B { }
class A extends B { }
class C {
void m(Class<? extends A> c) {}
void x(B b) {
m(((A)b).getClass());
}
}
@@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -1,15 +0,0 @@
public class RedundantIntCast
{
public static void main(String[] args) {
Integer i = 8;
method((int)i, 1); // int cast is marked as redundant
}
static void method(Object o, Object o1) {
System.out.println("this method works on objects");
}
static void method(int i, int i1) {
System.out.println("this method works on ints");
}
}
@@ -8,7 +8,7 @@ class CastPreventsNPEDetection {
void f() {
((ChildCastImpl)this).getParent().toString();
((CastPreventsNPEDetection)this).getParent().toString();
((<warning descr="Casting 'this' to 'CastPreventsNPEDetection' is redundant">CastPreventsNPEDetection</warning>)this).getParent().toString();
}
}
@@ -1,14 +0,0 @@
<problems>
<problem>
<file>Test.java</file>
<line>11</line>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant type cast</problem_class>
<description>Casting &lt;code&gt;this&lt;/code&gt; to &lt;code&gt;CastPreventsNPEDetection&lt;/code&gt; is redundant</description>
</problem>
<problem>
<file>Suspicious.java</file>
<line>9</line>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant type cast</problem_class>
<description>Casting &lt;code&gt;str&lt;/code&gt; to &lt;code&gt;String&lt;/code&gt; is redundant</description>
</problem>
</problems>
@@ -1,12 +1,12 @@
import java.util.Map;
public class Suspicious {
public class IgnoreSuspicious {
Map<String, String> map;
void f(Object s){
String str = (String) s;
map.remove((String)s);
map.remove((String)str);
map.remove((<warning descr="Casting 'str' to 'String' is redundant">String</warning>)str);
}
}
@@ -1,3 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
</problems>
@@ -1,20 +0,0 @@
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> T getX() {
return null;
}
}
public static void main(String[] args) {
//cast is needed for 'String' to be infered!
System.getProperty((String)new SomeClass().getX());
}
}
@@ -1,3 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
</problems>
@@ -1,32 +0,0 @@
import java.util.HashSet;
import java.util.Set;
final class Pair<A, B> {
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 <A, B> Pair<A, B> create(A first, B second) {
return new Pair<A, B>(first, second);
}
}
class Test {
final Set<String> strings = new HashSet<String>();
final Pair<Set<String>, Set<String>> x = Boolean.TRUE.booleanValue()
? Pair.create(strings, strings)
: Pair.create(((Set<String>) null), (Set<String>) null); //these casts are not redundant
}
@@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>31</line>
<description>Casting 'null' to 'Set&lt;String&gt;' is redundant</description>
</problem>
<problem>
<file>Test.java</file>
<line>31</line>
<description>Casting 'null' to 'Set&lt;String&gt;' is redundant</description>
</problem>
</problems>
@@ -1,32 +0,0 @@
import java.util.HashSet;
import java.util.Set;
final class Pair<A, B> {
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 <A> Pair<A, A> create(A first, A second) {
return new Pair<A, A>(first, second);
}
}
class Test {
final Set<String> strings = new HashSet<String>();
final Pair<Set<String>, Set<String>> x = Boolean.TRUE.booleanValue()
? Pair.create(strings, strings)
: Pair.create(((Set<String>) null), (Set<String>) null); //both casts are marked, but one is required for correct inference
}
@@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>5</line>
<description>Casting &lt;code&gt;getComponent()&lt;/code&gt; to &lt;code&gt;Bar&lt;/code&gt; is redundant</description>
</problem>
<problem>
<file>Test.java</file>
<line>6</line>
<description>Casting &lt;code&gt;getComponent()&lt;/code&gt; to &lt;code&gt;Bar&lt;/code&gt; is redundant</description>
</problem>
</problems>
@@ -1,18 +0,0 @@
class Test {
{
f((Bar) getComponent());
f1((Bar) getComponent());
Bar b = (Bar) getComponent();
}
private <J extends Bar> void f(J j) {}
private <J> void f1(J j) {}
private <T> T getComponent() {
return null;
}
static class Bar {}
}
@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>8</line>
<description>Casting &lt;code&gt;o&lt;/code&gt; to &lt;code&gt;List&lt;/code&gt; is redundant</description>
</problem>
</problems>
@@ -1,10 +0,0 @@
import java.util.ArrayList;
import java.util.List;
public class RedundantCast {
boolean redundantCasts(Object o) {
int p = 0;
if ((Number)p instanceof Integer) {}
return (List)o instanceof ArrayList;
}
}
@@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>8</line>
<description>Casting null to Class[] is redundant</description>
</problem>
<problem>
<file>Test.java</file>
<line>9</line>
<description>Casting null to Class[] is redundant</description>
</problem>
</problems>
@@ -1,11 +0,0 @@
class Test {
void f(Class... classes) {
}
void g() {
f(((Class[])null));
f(((Class)null));
f(((Class)null),
((Class)null));
}
}
@@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -1,21 +0,0 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class Main {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
Map<String, String> map = (Map) properties;
System.out.println(map);
}
}
interface I {}
class C implements I {}
class U {
void foo() {
List<C> listOfC = null;
List<I> listOfI = (List) listOfC;
}
}
@@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -1,10 +0,0 @@
class Test {
{
class TypedQuery<T> {}
class TemporalDataDTO<K> {}
class FOLDER_ID {}
TypedQuery<TemporalDataDTO> h = null;
TypedQuery<TemporalDataDTO<FOLDER_ID>> typedQuery = (TypedQuery<TemporalDataDTO<FOLDER_ID>>) (TypedQuery<?>)h;
}
}
@@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -1,9 +0,0 @@
import java.util.*;
public class RedundantCast{
<T> void foo2(List<Object[]> x) {
for (Object[] l : x) {
String[] s = (String[]) l;
}
}
}
@@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -1,7 +0,0 @@
public abstract class Foo<T extends Foo<T>> {
private int field;
public int bar(T t){
return ((Foo<T>)t).field;
}
}
@@ -1,48 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>WrapperToPrimitiveCast.java</file>
<line>8</line>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant type cast</problem_class>
<description>Casting &lt;code&gt;J&lt;/code&gt; to &lt;code&gt;int&lt;/code&gt; is redundant</description>
</problem>
<problem>
<file>WrapperToPrimitiveCast.java</file>
<line>9</line>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant type cast</problem_class>
<description>Casting &lt;code&gt;I&lt;/code&gt; to &lt;code&gt;int&lt;/code&gt; is redundant</description>
</problem>
<problem>
<file>WrapperToPrimitiveCast.java</file>
<line>12</line>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant type cast</problem_class>
<description>Casting &lt;code&gt;p&lt;/code&gt; to &lt;code&gt;Integer&lt;/code&gt; is redundant</description>
</problem>
<problem>
<file>WrapperToPrimitiveCast.java</file>
<line>15</line>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant type cast</problem_class>
<description>Casting &lt;code&gt;p&lt;/code&gt; to &lt;code&gt;Integer&lt;/code&gt; is redundant</description>
</problem>
<problem>
<file>WrapperToPrimitiveCast.java</file>
<line>24</line>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant type cast</problem_class>
<description>Casting &lt;code&gt;lnumber&lt;/code&gt; to &lt;code&gt;long&lt;/code&gt; is redundant</description>
</problem>
<problem>
<file>WrapperToPrimitiveCast.java</file>
<line>24</line>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant type cast</problem_class>
<description>Casting &lt;code&gt;lnumber&lt;/code&gt; to &lt;code&gt;long&lt;/code&gt; is redundant</description>
</problem>
</problems>
@@ -1,32 +0,0 @@
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;
}
}
class Foo<T> {
T foo() {
return (T)(Object)1;
}
}
@@ -1,47 +1,27 @@
/*
* 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.
*/
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.codeInspection;
import com.intellij.codeInspection.ex.LocalInspectionToolWrapper;
import com.intellij.JavaTestUtil;
import com.intellij.codeInspection.redundantCast.RedundantCastInspection;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
import com.intellij.openapi.roots.ModuleRootModificationUtil;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.testFramework.InspectionTestCase;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
public class RedundantCast15Test extends InspectionTestCase {
public class RedundantCast15Test extends LightCodeInsightFixtureTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
LanguageLevelProjectExtension.getInstance(myJavaFacade.getProject()).setLanguageLevel(LanguageLevel.JDK_1_5);
ModuleRootModificationUtil.setModuleSdk(getModule(), getTestProjectSdk());
protected String getBasePath() {
return JavaTestUtil.getRelativeJavaTestDataPath() + "/inspection/redundantCast/generics";
}
@NotNull
@Override
protected Sdk getTestProjectSdk() {
// in jdk 8 some casts are unnecessary
return IdeaTestUtil.getMockJdk17();
protected LightProjectDescriptor getProjectDescriptor() {
return JAVA_1_7;
}
private void doTest() {
final LocalInspectionToolWrapper toolWrapper = new LocalInspectionToolWrapper(new RedundantCastInspection());
doTest("redundantCast/generics/" + getTestName(false), toolWrapper, "java 1.5");
myFixture.enableInspections(new RedundantCastInspection());
myFixture.testHighlighting(getTestName(false) + ".java");
}
public void testBoxingInRef() { doTest(); }
@@ -63,7 +43,8 @@ public class RedundantCast15Test extends InspectionTestCase {
public void testRawCast() { doTest();}
public void testCastToUnboundWildcard() { doTest();}
public void testRawCastsToAvoidIncompatibility() { doTest();}
public void testCastCast() { doTest();}
public void testReturnValue() { doTest();}
public void testIDEA22899() { doTest();}
public void testRawCast1() { doTest();}
@@ -76,20 +57,25 @@ public class RedundantCast15Test extends InspectionTestCase {
public void testSafeTempVarName() { doTest();}
public void testTypeParameterAccessChecksJava7() {
IdeaTestUtil.setTestVersion(JavaSdkVersion.JDK_1_7, getModule(), getTestRootDisposable());
doTest();
}
public void testBoxingTopCast() {
IdeaTestUtil.setTestVersion(JavaSdkVersion.JDK_1_7, getModule(), getTestRootDisposable());
doTest();
}
public void testIgnore() {
final RedundantCastInspection castInspection = new RedundantCastInspection();
castInspection.IGNORE_SUSPICIOUS_METHOD_CALLS = true;
final LocalInspectionToolWrapper tool = new LocalInspectionToolWrapper(castInspection);
doTest("redundantCast/generics/" + getTestName(false), tool, "java 1.5");
myFixture.enableInspections(castInspection);
myFixture.testHighlighting(getTestName(false) + ".java");
}
public void testIgnoreSuspicious() {
final RedundantCastInspection castInspection = new RedundantCastInspection();
castInspection.IGNORE_SUSPICIOUS_METHOD_CALLS = true;
myFixture.enableInspections(castInspection);
myFixture.testHighlighting(getTestName(false) + ".java");
}
public void testDifferentNullness() { doTest();}
}