moving tests to CE

This commit is contained in:
Dmitry Jemerov
2009-09-25 20:42:06 +04:00
parent 64ecc03e67
commit 39c67da336
138 changed files with 12 additions and 5 deletions
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems />
@@ -0,0 +1,7 @@
public class Test {
public String foo(String path) {
String smth = new String(path);
if (path == null) return null;
return smth;
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>9</line>
<description>Passing null argument to parameter annotated as @NotNull</description>
</problem>
</problems>
@@ -0,0 +1,11 @@
import org.jetbrains.annotations.*;
public class Test {
private static void foo(@NotNull String smth) {
}
public static void main(String[] args) {
String s = args[0];
foo(null);
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>5</line>
<description>Array access &lt;code&gt;path[0]&lt;/code&gt; may produce &lt;code&gt;java.lang.NullPointerException&lt;/code&gt;</description>
</problem>
</problems>
@@ -0,0 +1,9 @@
public class Test {
public String foo(String[] path) {
if (path != null) return null;
String p = path[0];
return "";
}
}
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>6</line>
<method>
<name>void main(String[] args)</name>
<display_name>main(String[])</display_name>
<package>&lt;default&gt;</package>
<class>
<name>DoubleTrouble</name>
<display_name>DoubleTrouble</display_name>
</class>
</method>
<description>Condition &lt;code&gt;b == Double.NaN&lt;/code&gt; is always &lt;code&gt;false&lt;/code&gt;.</description>
</problem>
<problem>
<file>Test.java</file>
<line>11</line>
<method>
<name>void main(String[] args)</name>
<display_name>main(String[])</display_name>
<package>&lt;default&gt;</package>
<class>
<name>DoubleTrouble</name>
<display_name>DoubleTrouble</display_name>
</class>
</method>
<description>Condition &lt;code&gt;Float.NaN != Float.NaN&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;.</description>
</problem>
<problem>
<file>Test.java</file>
<line>18</line>
<method>
<name>void main(String[] args)</name>
<display_name>main(String[])</display_name>
<package>&lt;default&gt;</package>
<class>
<name>DoubleTrouble</name>
<display_name>DoubleTrouble</display_name>
</class>
</method>
<description>Condition &lt;code&gt;Double.NaN == a&lt;/code&gt; is always &lt;code&gt;false&lt;/code&gt;.</description>
</problem>
<problem>
<file>Test.java</file>
<line>19</line>
<method>
<name>void main(String[] args)</name>
<display_name>main(String[])</display_name>
<package>&lt;default&gt;</package>
<class>
<name>DoubleTrouble</name>
<display_name>DoubleTrouble</display_name>
</class>
</method>
<description>Condition &lt;code&gt;b == a&lt;/code&gt; is always &lt;code&gt;false&lt;/code&gt;.</description>
</problem>
</problems>
@@ -0,0 +1,22 @@
public class DoubleTrouble {
public static void main(String[] args) {
{
Double a = Double.NaN;
double b = a;//Double.NaN;
if (b == Double.NaN) {
;
}
}
if (Float.NaN != Float.NaN) {
}
{
double a = Double.NaN;
double b = Double.NaN;
if (Double.NaN == a) {}
if (b == a) {}
}
}
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>8</line>
<description>Condition 'e == null' is always false</description>
</problem>
<problem>
<file>Test.java</file>
<line>11</line>
<description>Condition 'e instanceof FileNotFoundException' is always false</description>
</problem>
</problems>
@@ -0,0 +1,16 @@
import java.io.*;
public class Foo {
public void foo() {
try {
throw new EOFException("aaa");
} catch (Exception e) {
if (e == null) {
System.out.println("Can't be here.");
}
if (e instanceof FileNotFoundException) {
System.out.println("Can't be here.");
}
}
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -0,0 +1,24 @@
public class CheckedExceptionDominance {
private static class CheckedException extends Exception {}
public static void foo() {
boolean flag = true;
try {
bar();
}
catch (CheckedException e) {
flag = false;
}
catch (Exception e) {
}
if (flag) { // This should not be highlighted as always true;
System.out.println("Must not happen");
}
}
public static void bar() throws CheckedException {
throw new CheckedException();
}
}
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>9</line>
<description>Method invocation 'foo().foo()' may produce NullPointerException</description>
</problem>
<problem>
<file>Test.java</file>
<line>10</line>
<description>Method invocation 'foo().foo()' may produce NullPointerException</description>
</problem>
<problem>
<file>Test.java</file>
<line>10</line>
<description>Method invocation 'foo().foo().foo()' may produce NullPointerException</description>
</problem>
</problems>
@@ -0,0 +1,14 @@
import org.jetbrains.annotations.Nullable;
public class Foo {
@Nullable Foo foo() {
return null;
}
public void bar() {
if (foo() != null &&
foo().foo() != null &&
foo().foo().foo() != null) {
}
}
}
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>4</line>
<method>
<name>void f()</name>
<display_name>f()</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Test</name>
<display_name>Test</display_name>
</class>
</method>
<description>Condition &lt;code&gt;L == 5&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;</description>
</problem>
<problem>
<file>Test.java</file>
<line>9</line>
<method>
<name>void f()</name>
<display_name>f()</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Test</name>
<display_name>Test</display_name>
</class>
</method>
<description>Condition &lt;code&gt;l == 0f&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;</description>
</problem>
<problem>
<file>Test.java</file>
<line>14</line>
<method>
<name>void f()</name>
<display_name>f()</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Test</name>
<display_name>Test</display_name>
</class>
</method>
<description>Condition &lt;code&gt;c == 1L&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;</description>
</problem>
</problems>
@@ -0,0 +1,19 @@
public class Test {
void f() {
Long L = 5L;
if (L == 5) {
;
}
long l = 0;
if (l == 0f) {
;
}
char c = 1;
if (c == 1L) {
;
}
}
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>18</line>
<description>Argument 'obj' might be null</description>
</problem>
<problem>
<file>Test.java</file>
<line>39</line>
<description>Argument 'obj' might be null</description>
</problem>
</problems>
@@ -0,0 +1,51 @@
import org.jetbrains.annotations.*;
public class TestNullableIntervening // See http://www.jetbrains.net/jira/browse/IDEA-2845
{
@Nullable Object obj;
public TestNullableIntervening(final @Nullable Object obj) {
this.obj = obj;
}
void foo() {}
void notnull(@NotNull Object arg) {}
void test1() {
if (obj != null) {
// Method intervening, might change obj; should have warning (OK)
foo();
notnull(obj);
}
}
void test2() {
if (obj != null) {
// Simple assignment intervening, no method calls and nothing
// involving obj...
// Should be no warning, but there is
int x = 10;
notnull(obj);
}
}
void test3() {
if (obj != null) {
// Constructor call intervening; for all we know, this could
// change obj through some strange interaction (constructor
// calls static method which has access to this object which
// then changes obj). Should be a warning (OK).
TestNullableIntervening obj2 = new TestNullableIntervening(null);
notnull(obj);
}
}
void test4() {
if (obj != null) {
// Array construction cannot change other objects
// Should be no warning, but there is
Object[] arr = new Object[5];
notnull(obj);
}
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>4</line>
<description>Dereference of 'path' may produce java.lang.NullPointerException</description>
</problem>
</problems>
@@ -0,0 +1,8 @@
public class Test {
public String foo(String[] path) {
if (path != null) return null;
for (String p: path) {}
return "";
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -0,0 +1,21 @@
public class Npe {
Object foo(Object[] objs) {
boolean skip = true;
for (Object o : objs) {
if (o instanceof String) {
skip = false;
continue;
}
if (skip) {
continue;
}
bar();
}
return "";
}
void bar() {
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>GenericInstanceof.java</file>
<line>8</line>
<description>Condition is always true</description>
</problem>
</problems>
@@ -0,0 +1,12 @@
class Generic<T> {
Generic() {}
}
class Test {
void foo () {
Generic g = new Generic ();
if (g instanceof Generic<String>) {
return;
}
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Npe.java</file>
<line>10</line>
<description>Argument 'null' is probably null</description>
</problem>
</problems>
@@ -0,0 +1,12 @@
public class WrongParameter {
public boolean resolveAction(@NotNull Object action, @NotNull Object combatant, @NotNull Object userInputProvider) {
return false;
}
public void foo() {
resolveAction(
new Object(),
new Object(),
null); // Last parameter should be highlighted
}
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>IDEADEV10489.java</file>
<line>10</line>
<description>Method invocation 's.length()' may produce NullPointerException</description>
</problem>
<problem>
<file>IDEADEV10489.java</file>
<line>12</line>
<description>Method invocation 's.length()' may produce NullPointerException</description>
</problem>
</problems>
@@ -0,0 +1,23 @@
class IDEADEV10489 {
static String getS() {
return null;
}
static void f() {
String s = getS();
if (s != null) s.length();
if (foo()) {
int[] i1 = new int [s.length()];
} else if (foo()) {
int[] i2 = new int [] {s.length()};
} else if (foo()) {
int[][] i3 = new int [(s = "").length()][s.length()];
} else{
int[] i4 = new int[] {(s = "").length(), s.length()};
}
}
private static boolean foo() {
return false;
}
}
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -0,0 +1,6 @@
public class NonconstantCondition {
void test(boolean flag) {
if (flag == (flag = true)) {
}
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Npe.java</file>
<line>9</line>
<description>Condition 'foo != null' is always 'true'</description>
</problem>
</problems>
@@ -0,0 +1,12 @@
import org.jetbrains.annotations.*;
import java.util.*;
class Test {
void foo() {
String[] data = new String[] {"abs", "def"};
for (@NotNull String foo: data) {
assert foo != null; // Condition always true
}
}
}
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -0,0 +1,15 @@
import org.jetbrains.annotations.*;
class Foo {
public void x() throws IOException {
@Nullable String foo = "";
while (foo.length() == 0) {
foo = y();
if (foo == null) throw new IOException("foo");
}
}
public String y() {
return "";
}
}
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Npe.java</file>
<line>14</line>
<description>'length' may produce 'NullPointerException'</description>
</problem>
<problem>
<file>Npe.java</file>
<line>18</line>
<description>'length' may produce 'NullPointerException'</description>
</problem>
<problem>
<file>Npe.java</file>
<line>27</line>
<description>'length' may produce 'NullPointerException'</description>
</problem>
</problems>
@@ -0,0 +1,31 @@
import org.jetbrains.annotations.*;
import java.util.*;
class Test {
@Nullable
public String getString(String s) {
return null;
}
void test(Collection<String> foos) {
Test t = new Test();
final int i = t.getString("foo").length();
System.out.println("i = " + i);
for (String foo : foos) {
final int j = t.getString(foo).length();
System.out.println("i = " + j);
}
}
void test2(Collection<String> foos) {
Test t = new Test();
for (Iterator<String> iterator = foos.iterator(); iterator.hasNext();) {
final int i = getString(iterator.next()).length();
System.out.println("i = " + i);
}
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>4</line>
<description>Condition 'test' at the left side is always 'true'</description>
</problem>
</problems>
@@ -0,0 +1,6 @@
class TestGenericsInstanceof {
public void foo(Object o) {
boolean test = true;
test &= o.hashCode() > 3;
}
}
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>AndAssign.java</file>
<line>19</line>
<description>Condition is always true</description>
</problem>
<problem>
<file>AndAssign.java</file>
<line>26</line>
<description>Condition is always false</description>
</problem>
<problem>
<file>AndAssign.java</file>
<line>33</line>
<description>Condition is always false</description>
</problem>
<problem>
<file>AndAssign.java</file>
<line>40</line>
<description>Condition is always false</description>
</problem>
<problem>
<file>AndAssign.java</file>
<line>47</line>
<description>Condition is always true</description>
</problem>
<problem>
<file>AndAssign.java</file>
<line>54</line>
<description>Condition is always true</description>
</problem>
<problem>
<file>AndAssign.java</file>
<line>61</line>
<description>Condition is always true</description>
</problem>
<problem>
<file>AndAssign.java</file>
<line>68</line>
<description>Condition is always false</description>
</problem>
<problem>
<file>AndAssign.java</file>
<line>17</line>
<description>left always true</description>
</problem>
<problem>
<file>AndAssign.java</file>
<line>24</line>
<description>left always true</description>
</problem>
<problem>
<file>AndAssign.java</file>
<line>31</line>
<description>left always false</description>
</problem>
<problem>
<file>AndAssign.java</file>
<line>38</line>
<description>left always false</description>
</problem>
<problem>
<file>AndAssign.java</file>
<line>45</line>
<description>left always true</description>
</problem>
<problem>
<file>AndAssign.java</file>
<line>52</line>
<description>left always true</description>
</problem>
<problem>
<file>AndAssign.java</file>
<line>59</line>
<description>left always false</description>
</problem>
<problem>
<file>AndAssign.java</file>
<line>66</line>
<description>left always false</description>
</problem>
</problems>
@@ -0,0 +1,72 @@
public class AndAssign {
public void foo(boolean result, Object acc) {
result &= verify(result ? acc : null);
}
public boolean verify(Object o) {
System.out.println(o);
return true;
}
public void positives() {
boolean t = true;
boolean f = false;
boolean r;
r = t;
r &= t; // Always true
if (r) { // Always true
System.out.println("foo");
}
r = t;
r &= f; // Always true
if (r) { // Always false
System.out.println("foo");
}
r = f;
r &= t; // Always false
if (r) { // Always false
System.out.println("foo");
}
r = f;
r &= f; // Always false
if (r) { // Always false
System.out.println("foo");
}
r = t;
r |= t // Always true
if (r) { // Always true
System.out.println("foo");
}
r = t;
r |= f // Always true
if (r) { // Always true
System.out.println("foo");
}
r = f;
r |= t // Always false
if (r) { // Always true
System.out.println("foo");
}
r = f;
r |= f // Always false
if (r) { // Always false
System.out.println("foo");
}
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Foo.java</file>
<line>10</line>
<description>Condition 'o instanceof B' is always false</description>
</problem>
</problems>
@@ -0,0 +1,14 @@
class A {
}
class B extends A {
}
class C {
void foo(Object o) {
if (o instanceof A || o instanceof B) {
System.out.println("Something");
}
}
}
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems />
@@ -0,0 +1,15 @@
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
public class TestNPEafterNew {
@Nullable Object[] arr;
void test(@NotNull Object[] notnull) {
arr = notnull;
System.out.println(arr.length);
}
void test() {
arr = new Object[5];
System.out.println(arr.length);
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Npe.java</file>
<line>7</line>
<description>Condition 'o == null' is always false</description>
</problem>
</problems>
@@ -0,0 +1,10 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Npe {
void bar() {
final @NotNull Object o = call();
if (o == null) {}
}
Object call() {return new Object();}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Npe.java</file>
<line>10</line>
<description>Condition 'o == null' is always false</description>
</problem>
</problems>
@@ -0,0 +1,12 @@
import org.jetbrains.annotations.NotNull;
public class Npe {
@NotNull Object foo() {
return new Object();
}
void bar() {
Object o = foo();
if (o == null) System.out.println("Can't be");
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Npe.java</file>
<line>9</line>
<description>Passing null argument to parameter annotated as @NotNull</description>
</problem>
</problems>
@@ -0,0 +1,11 @@
import org.jetbrains.annotations.NotNull;
public class Npe {
Object foo(@NotNull Object o) {
return o;
}
void bar() {
Object o = foo(null); // null should not be passed here.
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Npe.java</file>
<line>5</line>
<description>Condition 'o == null' is always false</description>
</problem>
</problems>
@@ -0,0 +1,10 @@
import org.jetbrains.annotations.NotNull;
public class Npe {
Object foo(@NotNull Object o) {
if (o == null) {
// Should not get there.
}
return o;
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Npe.java</file>
<line>14</line>
<description>Argument 'nullable()' might be null</description>
</problem>
</problems>
@@ -0,0 +1,16 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Npe {
Object foo(@NotNull Object o) {
return o;
}
@Nullable Object nullable() {
return null;
}
void bar() {
Object o = foo(nullable()); // null should not be passed here.
}
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Npe.java</file>
<line>12</line>
<description>Expression 'o' might evaluate to null</description>
</problem>
<problem>
<file>Npe.java</file>
<line>13</line>
<description>Expression 'o' might evaluate to null</description>
</problem>
</problems>
@@ -0,0 +1,15 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Npe {
@NotNull Object aField;
@Nullable Object nullable() {
return null;
}
void bar() {
Object o = nullable();
aField = o;
@NotNull Object aLocalVariable = o;
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Npe.java</file>
<line>7</line>
<description>'o.hashCode()' may produce NullPointerException</description>
</problem>
</problems>
@@ -0,0 +1,13 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Npe {
void bar() {
final @Nullable Object o = foo();
o.hashCode(); // NPE
}
Object foo() {
return null;
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Npe.java</file>
<line>6</line>
<description>null is returned</description>
</problem>
</problems>
@@ -0,0 +1,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Npe {
@NotNull Object foo() {
return null;
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>8</line>
<description>Expression 'res' might evaluate to null but is returned by the method declared as @NotNull</description>
</problem>
</problems>
@@ -0,0 +1,10 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class Test {
@NotNull Object foo() {
Object res;
res = null;
return res;
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Npe.java</file>
<line>14</line>
<description>Argument '(Object)nullable()' might be null</description>
</problem>
</problems>
@@ -0,0 +1,16 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Npe {
Object foo(@NotNull Object o) {
return o;
}
@Nullable Object nullable() {
return null;
}
void bar() {
Object o = foo((Object)nullable()); // null should not be passed here.
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Npe.java</file>
<line>15</line>
<description>Argument 'o' might be null</description>
</problem>
</problems>
@@ -0,0 +1,17 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Npe {
Object foo(@NotNull Object o) {
return o;
}
@Nullable Object nullable() {
return null;
}
void bar() {
Object o = nullable();
foo(o); // null should not be passed here
}
}
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -0,0 +1,19 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class Npe {
Object foo(@NotNull Object o) {
return o;
}
@Nullable Object nullable() {
return null;
}
void bar() {
Object o = nullable();
if (o != null) {
foo(o); // OK, o can't be null.
}
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>6</line>
<description>Condition 'c == null' is always false</description>
</problem>
</problems>
@@ -0,0 +1,10 @@
import java.io.*;
public class Foo {
public void foo(Object a, String b) {
String c = a + b;
if (c == null) {
System.out.println("Can't be!");
}
}
}
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>14</line>
<description>Condition '!a &amp;&amp; b' always false</description>
</problem>
<problem>
<file>Test.java</file>
<line>14</line>
<description>Condition '!a' always false</description>
</problem>
</problems>
@@ -0,0 +1,16 @@
class Test
{
class Test
{
public void x()
{
boolean a = false;
boolean b = true;
do
{
a = true;
}
while( !a && b );
}
}}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems />
@@ -0,0 +1,5 @@
public class Foo {
public void foo() {
do {} while(false);
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems />
@@ -0,0 +1,12 @@
public class Aaa {
Object getObject() {
return null;
}
void f() {
Object obj = getObject();
if (obj instanceof Aaa || obj == null) {
Aaa a = (Aaa) obj; // inspection reports that ClassCastException can be thrown
}
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Finally.java</file>
<line>9</line>
<description>Method invocation 'o.hashCode()' may produce java.lang.NullPointerException</description>
</problem>
</problems>
@@ -0,0 +1,37 @@
import java.io.*;
public class Finally {
public void foo(Object o) {
try {
if (o == null) return;
}
finally {
System.out.println(o.hashCode()); // Error here.
}
System.out.println(o.hashCode()); // No error here.
}
public void bar(Object o) {
boolean rearrangeChildren = false;
Object typePattern;
try {
typePattern = parseTypePattern();
} catch (FileNotFoundException followsFailure) {
if (o != null) {
typePattern = followsFailure.getParsingResult();
} else {
throw followsFailure;
}
rearrangeChildren = true;
}
if (rearrangeChildren) {
System.out.println("Can be here.");
}
}
Object parseTypePattern() throws IOException {
return null;
}
}
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>3</line>
<description>Condition 't instanceof Test' is redundant</description>
</problem>
<problem>
<file>Test.java</file>
<line>9</line>
<description>Condition 't instanceof Test' always true</description>
</problem>
<problem>
<file>Test.java</file>
<line>14</line>
<description>Condition 'foo(null) instanceof Test' is redundant</description>
</problem>
</problems>
@@ -0,0 +1,17 @@
public class Test {
public Test foo(Test t) {
if (t instanceof Test) { // redundant instanceof error here. t can be null
foo(null);
}
}
public Object bar(Test t) {
if (t == null) return;
if (t instanceof Test) { // always true error here. t can't be null
foo(null);
}
if (bar(null) instanceof Test) return null; // no error here.
if (foo(null) instanceof Test) return null; // redundant instanceof error here. foo(null) can be null
return null;
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>NullTest.java</file>
<line>6</line>
<description>Dereference of 't' may produce java.lang.NullPointerException</description>
</problem>
</problems>
@@ -0,0 +1,9 @@
public class NullTest {
int m;
void f() {};
public void x() {
NullTest t = null;
t.m = 12;
t.f();
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -0,0 +1,28 @@
public class CodeFlowTest {
public static void main (String[] args) {
String string;
Exception exception;
try {
string = getString();
exception = null;
} catch (SomeException e1) {
string = null;
exception = e1;
}
if (string != null)
System.out.println ("Not null");
else
exception.printStackTrace();
}
private static String getString () throws SomeException {
if (Math.random() < 0.5)
throw new SomeException();
else
return "";
}
private static class SomeException extends Exception {
}
}
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -0,0 +1,61 @@
import java.sql.*;
public class Test {
/**
* Lädt den Datensatz.
*/
private boolean loadValues(Transaction transaction) {
try {
boolean res=false;
PreparedStatement stmt=transaction.dirty() ? transaction.dbstore().stmtGetDirty(this) : transaction.dbstore().stmtGetClean(this);
if (stmt==null) return false;
synchronized(stmt) {
try {
transaction.dbmanager.dbstore().loadsCount++;
try {
fillPHPK(stmt, transaction);
} catch (SQLException e) {
DBManager.resetThread(stmt);
throw e;
}
ResultSet rs=stmt.executeQuery();
try {
if (rs.next()) {
loadOrdered(rs);
res=true;
} else if (transaction.dbmanager.accessCommitBug()) {
// Scheint manchmal in Access zu passieren....
PreparedStatement stmt1=transaction.dirty() ? stmtGetDirty(transaction.dbstore()) : stmtGetClean(transaction.dbstore());
synchronized(stmt1)
{
fillPHPK(stmt1, transaction);
ResultSet rs1=stmt1.executeQuery();
if (rs1.next()) {
Dbg.pw("Second try to load object returns an object: "+toString()+"???");
loadOrdered(rs1);
res=true;
}
rs1.close();
stmt1.close();
}
}
} finally {
try {
rs.close();
} catch (SQLException e) {
Dbg.pw(e);
}
}
} catch (SQLException e) {
handleSQLException(e, stmt);
}
}
if (res) loadBlobs(transaction);
return res;
} catch (SQLException e) {
throw new ExceptionBag(e);
}
}
}
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems />
@@ -0,0 +1,12 @@
interface Inter {}
class Impl implements Inter {}
class TestGenericsInstanceof<I extends Inter>
{
I member;
{
boolean test = member instanceof Impl;
}
}
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems />
@@ -0,0 +1,34 @@
public class Test {
public void foo() {
Properties properties = new Properties();
InputStream inStream = null;
try {
removeCustomPrefixFromProperties(propertiesFile);
inStream = new FileInputStream(propertiesFile);
properties.load(inStream);
Enumeration<?> propertyNames = properties.propertyNames();
while (propertyNames.hasMoreElements()) {
String name = (String) propertyNames.nextElement();
setValue(name, properties.getProperty(name));
}
} catch (FileNotFoundException e) {
System.err.println(e.getMessage());
System.exit(-1);
}
catch (IOException e) {
LOG.error(e.getMessage(), e);
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
LOG.info(e);
}
}
}
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>10</line>
<description>Dereference of 'getE()' may produce 'NullPointerException'</description>
</problem>
</problems>
@@ -0,0 +1,15 @@
import org.jetbrains.annotations.Nullable;
public class Test {
enum E { A, B }
@Nullable
static E getE() { return null; }
public static void main(String[] args) {
switch (getE()) { // <<< should be highlighted as potential NPE
case A:
case B:
}
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>9</line>
<description>'foo() != null' is always true</description>
</problem>
</problems>
@@ -0,0 +1,11 @@
import org.jetbrains.annotations.*;
public class Test {
@NotNull
public Object foo() {
return new Object();
}
public void qqq() {
int c = foo() != null ? foo().hashCode() : 0;
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>4</line>
<description>r is always true</description>
</problem>
</problems>
@@ -0,0 +1,8 @@
public class Test {
public void foo(boolean x, boolean y, boolean z) {
boolean r = true;
r &= x;
r &= y;
r &= z;
}
}
@@ -0,0 +1,345 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>5</line>
<method>
<name>void f(int k)</name>
<display_name>f(int)</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Auto</name>
<display_name>Auto</display_name>
</class>
</method>
<problem_class>Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;i==j&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;</description>
</problem>
<problem>
<file>Test.java</file>
<line>9</line>
<method>
<name>void f(int k)</name>
<display_name>f(int)</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Auto</name>
<display_name>Auto</display_name>
</class>
</method>
<problem_class>Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;i==j2&lt;/code&gt; is always &lt;code&gt;false&lt;/code&gt;</description>
</problem>
<problem>
<file>Test.java</file>
<line>12</line>
<method>
<name>void f(int k)</name>
<display_name>f(int)</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Auto</name>
<display_name>Auto</display_name>
</class>
</method>
<problem_class>Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;i==0&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;</description>
</problem>
<problem>
<file>Test.java</file>
<line>15</line>
<method>
<name>void f(int k)</name>
<display_name>f(int)</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Auto</name>
<display_name>Auto</display_name>
</class>
</method>
<problem_class>Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;i==1&lt;/code&gt; is always &lt;code&gt;false&lt;/code&gt;</description>
</problem>
<problem>
<file>Test.java</file>
<line>22</line>
<method>
<name>void f(int k)</name>
<display_name>f(int)</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Auto</name>
<display_name>Auto</display_name>
</class>
</method>
<problem_class>Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;big1==333&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;</description>
</problem>
<problem>
<file>Test.java</file>
<line>24</line>
<method>
<name>void f(int k)</name>
<display_name>f(int)</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Auto</name>
<display_name>Auto</display_name>
</class>
</method>
<problem_class>Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;333 == big2&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;</description>
</problem>
<problem>
<file>Test.java</file>
<line>26</line>
<method>
<name>void f(int k)</name>
<display_name>f(int)</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Auto</name>
<display_name>Auto</display_name>
</class>
</method>
<problem_class>Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;333==333&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;</description>
</problem>
<problem>
<file>Test.java</file>
<line>28</line>
<method>
<name>void f(int k)</name>
<display_name>f(int)</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Auto</name>
<display_name>Auto</display_name>
</class>
</method>
<problem_class>Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;big1==big2&lt;/code&gt; is always &lt;code&gt;false&lt;/code&gt;</description>
</problem>
<problem>
<file>Test.java</file>
<line>32</line>
<method>
<name>void f(int k)</name>
<display_name>f(int)</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Auto</name>
<display_name>Auto</display_name>
</class>
</method>
<problem_class>Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;prim==big2&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;</description>
</problem>
<problem>
<file>Test.java</file>
<line>36</line>
<method>
<name>void f(int k)</name>
<display_name>f(int)</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Auto</name>
<display_name>Auto</display_name>
</class>
</method>
<problem_class>Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;big1 == 0&lt;/code&gt; is always &lt;code&gt;false&lt;/code&gt;</description>
</problem>
<problem>
<file>Test.java</file>
<line>39</line>
<method>
<name>void f(int k)</name>
<display_name>f(int)</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Auto</name>
<display_name>Auto</display_name>
</class>
</method>
<problem_class>Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;big1 == 332&lt;/code&gt; is always &lt;code&gt;false&lt;/code&gt;</description>
</problem>
<problem>
<file>Test.java</file>
<line>44</line>
<method>
<name>void f(int k)</name>
<display_name>f(int)</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Auto</name>
<display_name>Auto</display_name>
</class>
</method>
<problem_class>Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;c==1234&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;</description>
</problem>
<problem>
<file>Test.java</file>
<line>47</line>
<method>
<name>void f(int k)</name>
<display_name>f(int)</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Auto</name>
<display_name>Auto</display_name>
</class>
</method>
<problem_class>Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;c==124&lt;/code&gt; is always &lt;code&gt;false&lt;/code&gt;</description>
</problem>
<problem>
<file>Test.java</file>
<line>51</line>
<method>
<name>void f(int k)</name>
<display_name>f(int)</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Auto</name>
<display_name>Auto</display_name>
</class>
</method>
<problem_class>Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;c==1234&lt;/code&gt; is always &lt;code&gt;false&lt;/code&gt;</description>
</problem>
<problem>
<file>Test.java</file>
<line>54</line>
<method>
<name>void f(int k)</name>
<display_name>f(int)</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Auto</name>
<display_name>Auto</display_name>
</class>
</method>
<problem_class>Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;c==c&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;</description>
</problem>
<problem>
<file>Test.java</file>
<line>58</line>
<method>
<name>void f(int k)</name>
<display_name>f(int)</display_name>
<package>&lt;default&gt;</package>
<class>
<name>Auto</name>
<display_name>Auto</display_name>
</class>
</method>
<problem_class>Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;c==c2&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;</description>
</problem>
<problem>
<file>Test.java</file>
<line>73</line>
<method>
<name>void canBeStatic(int x)</name>
<display_name>canBeStatic(int)</display_name>
<package>&lt;default&gt;</package>
<class>
<name>aaa</name>
<display_name>aaa</display_name>
</class>
</method>
<problem_class>Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;a == 5&lt;/code&gt; is always &lt;code&gt;false&lt;/code&gt;</description>
</problem>
<problem>
<file>Test.java</file>
<line>83</line>
<method>
<name>void f(int p)</name>
<display_name>f(int)</display_name>
<package>&lt;default&gt;</package>
<class>
<name>aaa</name>
<display_name>aaa</display_name>
</class>
</method>
<problem_class>Constant conditions &amp; exceptions</problem_class>
<description>Condition &lt;code&gt;i1 == i2&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;</description>
</problem>
<!--
<problem>
<file>Test.java</file>
<line>103</line>
<description>'r==r1' is always false</description>
</problem>
-->
<problem>
<file>Test.java</file>
<line>107</line>
<description>'r2==r3' is always false</description>
</problem>
<!--
<problem>
<file>Test.java</file>
<line>112</line>
<description>'fr==fr1' is always false</description>
</problem>
-->
<problem>
<file>Test.java</file>
<line>116</line>
<description>'fr2==fr3' is always false</description>
</problem>
</problems>
@@ -0,0 +1,119 @@
public class Auto {
void f(int k) {
Integer i = 0;
Integer j=0;
if (i==j) {
}
Integer j2 = 1;
if (i==j2) {
}
if (i==0) {
}
if (i==1) {
}
//////////////////
Integer big1 = 333;
Integer big2= 333;
if (big1==333) {
}
if (333 == big2) {
}
if (333==333) {
}
if (big1==big2) {
}
int prim = big1;
if (prim==big2) {
}
if (big1 == 0) {
}
if (big1 == 332) {
}
Character c = 1234;
if (c==1234) {
}
if (c==124) {
}
c = 1;
if (c==1234) {
}
if (c==c) {
}
Character c2 = 1;
if (c==c2) {
}
}
}
class aaa {
int a;
int b;
void canBeStatic(int x) {
for (int i=0;i<10;i++) {
a = i;
}
a = 4;
if (a == 5) {
}
}
void f(int p) {
{
int i = 1;
Integer i1 = i;
Integer i2 = i;
if (i1 == i2) {
}
}
{
int i = p;
Integer i1 = i;
Integer i2 = i;
if (i1 == i2) {
}
}
}
}
class UsesDoubleAndFloat {
void f() {
double dd = 10.0;
Double r = dd;
Double r1 = dd;
System.out.println("(r==r1) is "+(r==r1));
Double r2 = 10.0;
Double r3 = 10.0;
System.out.println("(r2==r3) is "+(r2==r3));
float fd = 10.0f;
Float fr = fd;
Float fr1 = fd;
System.out.println("(r==r1) is "+(fr==fr1));
Float fr2 = 10.0f;
Float fr3 = 10.0f;
System.out.println("(r2==r3) is "+(fr2==fr3));
}
}
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>14</line>
<description>Condition &lt;code&gt;c&lt;/code&gt; is always &lt;code&gt;false&lt;/code&gt;.</description>
</problem>
<problem>
<file>Test.java</file>
<line>20</line>
<description>Condition &lt;code&gt;c&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;.</description>
</problem>
<problem>
<file>Test.java</file>
<line>33</line>
<description>Condition &lt;code&gt;o&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;.</description>
</problem>
<problem>
<file>Test.java</file>
<line>39</line>
<description>Condition &lt;code&gt;o&lt;/code&gt; is always &lt;code&gt;false&lt;/code&gt;.</description>
</problem>
<problem>
<file>Test.java</file>
<line>45</line>
<description>Condition &lt;code&gt;o&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;.</description>
</problem>
<problem>
<file>Test.java</file>
<line>51</line>
<description>Condition &lt;code&gt;o&lt;/code&gt; at the left side of assignment expression is always &lt;code&gt;false&lt;/code&gt;. Can be simplified to normal assignment.</description>
</problem>
</problems>
@@ -0,0 +1,55 @@
public class S {
void f(Boolean override) {
if (override == null) {
//doSomething();
} else if (override) { // always false?
//doOverride();
}
}
public void te0(boolean b){
Boolean c = false;
// if (b) c = true;
if (c) {
}
}
public void te1(boolean b){
Boolean c = true;
// if (b) c = true;
if (c) {
}
}
public void te2(boolean b){
Boolean c = false;
if (b) c = true;
if (c) {
}
}
public void te3(boolean b){
Boolean c = Boolean.FALSE;
boolean o = !c;
if (o) {
}
}
public void te4(boolean b){
Boolean c = Boolean.FALSE;
boolean o = c;
if (o) {
}
}
public void te5(boolean b){
Boolean c = Boolean.TRUE;
boolean o = b||c;
if (o) {
}
}
public void te6(boolean b){
Boolean c = Boolean.TRUE;
boolean o = !c;
o |= c&b;
if (o) {
}
}
}

Some files were not shown because too many files have changed in this diff Show More