Lines 7-25
Link Here
|
7 |
* |
7 |
* |
8 |
* Contributors: |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
9 |
* IBM Corporation - initial API and implementation |
|
|
10 |
* Stephan Herrmann <stephan@cs.tu-berlin.de> - Contribution for bug 185682 - Increment/decrement operators mark local variables as read |
10 |
*******************************************************************************/ |
11 |
*******************************************************************************/ |
11 |
package org.eclipse.jdt.core.tests.compiler.regression; |
12 |
package org.eclipse.jdt.core.tests.compiler.regression; |
12 |
|
13 |
|
13 |
import java.util.HashMap; |
14 |
import java.util.HashMap; |
14 |
import java.util.Map; |
15 |
import java.util.Map; |
15 |
|
16 |
|
|
|
17 |
import junit.framework.Test; |
18 |
|
16 |
import org.eclipse.jdt.core.JavaCore; |
19 |
import org.eclipse.jdt.core.JavaCore; |
17 |
import org.eclipse.jdt.core.compiler.CategorizedProblem; |
20 |
import org.eclipse.jdt.core.compiler.CategorizedProblem; |
18 |
import org.eclipse.jdt.internal.compiler.CompilationResult; |
21 |
import org.eclipse.jdt.internal.compiler.CompilationResult; |
19 |
import org.eclipse.jdt.internal.compiler.ICompilerRequestor; |
22 |
import org.eclipse.jdt.internal.compiler.ICompilerRequestor; |
20 |
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; |
23 |
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; |
21 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
24 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
22 |
import junit.framework.Test; |
|
|
23 |
|
25 |
|
24 |
/* Collects potential programming problems tests that are not segregated in a |
26 |
/* Collects potential programming problems tests that are not segregated in a |
25 |
* dedicated test class (aka NullReferenceTest). */ |
27 |
* dedicated test class (aka NullReferenceTest). */ |
Lines 1683-1686
Link Here
|
1683 |
"The assignment to variable nvx has no effect\n" + |
1685 |
"The assignment to variable nvx has no effect\n" + |
1684 |
"----------\n"); |
1686 |
"----------\n"); |
1685 |
} |
1687 |
} |
|
|
1688 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=185682 |
1689 |
public void test0046() { |
1690 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) |
1691 |
return; |
1692 |
Map customOptions = getCompilerOptions(); |
1693 |
customOptions.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.WARNING); |
1694 |
this.runNegativeTest( |
1695 |
new String[] { |
1696 |
"X.java", |
1697 |
"class X {\n" + |
1698 |
" int foo() {\n" + |
1699 |
" int i=1;\n" + |
1700 |
" boolean b=false;\n" + |
1701 |
" b|=true;\n" + // not a relevant usage |
1702 |
" int k = 2;\n" + |
1703 |
" --k;\n" + // not a relevant usage |
1704 |
" k+=3;\n" + // not a relevant usage |
1705 |
" Integer j = 3;\n" + |
1706 |
" j++;\n" + // relevant because unboxing is involved |
1707 |
" return i++;\n" + // value after increment is used |
1708 |
" }\n" + |
1709 |
"}" |
1710 |
}, |
1711 |
"----------\n" + |
1712 |
"1. WARNING in X.java (at line 4)\n" + |
1713 |
" boolean b=false;\n" + |
1714 |
" ^\n" + |
1715 |
"The local variable b is never read\n" + |
1716 |
"----------\n" + |
1717 |
"2. WARNING in X.java (at line 6)\n" + |
1718 |
" int k = 2;\n" + |
1719 |
" ^\n" + |
1720 |
"The local variable k is never read\n" + |
1721 |
"----------\n", |
1722 |
null/*classLibraries*/, |
1723 |
true/*shouldFlushOutputDirectory*/, |
1724 |
customOptions); |
1725 |
} |
1726 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=185682 |
1727 |
// variant with private fields instead of locals |
1728 |
public void test0046_field() { |
1729 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) |
1730 |
return; |
1731 |
Map customOptions = getCompilerOptions(); |
1732 |
customOptions.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.WARNING); |
1733 |
this.runNegativeTest( |
1734 |
new String[] { |
1735 |
"X.java", |
1736 |
"class X {\n" + |
1737 |
" private int i=1;\n" + |
1738 |
" private boolean b=false;\n" + |
1739 |
" private int k = 2;\n" + |
1740 |
" private Integer j = 3;\n" + |
1741 |
" int foo() {\n" + |
1742 |
" b|=true;\n" + // not a relevant usage |
1743 |
" --k;\n" + // not a relevant usage |
1744 |
" k+=3;\n" + // not a relevant usage |
1745 |
" j++;\n" + // relevant because unboxing is involved |
1746 |
" return i++;\n" + // value after increment is used |
1747 |
" }\n" + |
1748 |
"}" |
1749 |
}, |
1750 |
"----------\n" + |
1751 |
"1. WARNING in X.java (at line 3)\n" + |
1752 |
" private boolean b=false;\n" + |
1753 |
" ^\n" + |
1754 |
"The private field X.b is never used\n" + |
1755 |
"----------\n" + |
1756 |
"2. WARNING in X.java (at line 4)\n" + |
1757 |
" private int k = 2;\n" + |
1758 |
" ^\n" + |
1759 |
"The private field X.k is never used\n" + |
1760 |
"----------\n", |
1761 |
null/*classLibraries*/, |
1762 |
true/*shouldFlushOutputDirectory*/, |
1763 |
customOptions); |
1764 |
} |
1765 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=185682 |
1766 |
// variant with private fields instead of locals - this-qualified access |
1767 |
public void test0046_field_this_qualified() { |
1768 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) |
1769 |
return; |
1770 |
Map customOptions = getCompilerOptions(); |
1771 |
customOptions.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.WARNING); |
1772 |
this.runNegativeTest( |
1773 |
new String[] { |
1774 |
"X.java", |
1775 |
"class X {\n" + |
1776 |
" private int i=1;\n" + |
1777 |
" private boolean b=false;\n" + |
1778 |
" private int k = 2;\n" + |
1779 |
" private Integer j = 3;\n" + |
1780 |
" int foo() {\n" + |
1781 |
" this.b|=true;\n" + // not a relevant usage |
1782 |
" --this.k;\n" + // not a relevant usage |
1783 |
" getThis().k+=3;\n" + // not a relevant usage |
1784 |
" this.j++;\n" + // relevant because unboxing is involved |
1785 |
" return this.i++;\n" + // value after increment is used |
1786 |
" }\n" + |
1787 |
" X getThis() { return this; }\n" + |
1788 |
"}" |
1789 |
}, |
1790 |
"----------\n" + |
1791 |
"1. WARNING in X.java (at line 3)\n" + |
1792 |
" private boolean b=false;\n" + |
1793 |
" ^\n" + |
1794 |
"The private field X.b is never used\n" + |
1795 |
"----------\n" + |
1796 |
"2. WARNING in X.java (at line 4)\n" + |
1797 |
" private int k = 2;\n" + |
1798 |
" ^\n" + |
1799 |
"The private field X.k is never used\n" + |
1800 |
"----------\n", |
1801 |
null/*classLibraries*/, |
1802 |
true/*shouldFlushOutputDirectory*/, |
1803 |
customOptions); |
1804 |
} |
1805 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=185682 |
1806 |
// variant with private fields instead of locals - regular qualified access |
1807 |
public void test0046_field_qualified() { |
1808 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) |
1809 |
return; |
1810 |
Map customOptions = getCompilerOptions(); |
1811 |
customOptions.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.WARNING); |
1812 |
this.runNegativeTest( |
1813 |
new String[] { |
1814 |
"X.java", |
1815 |
"class X {\n" + |
1816 |
" private int i=1;\n" + |
1817 |
" private boolean b=false;\n" + |
1818 |
" private int k = 2;\n" + |
1819 |
" private Integer j = 3;\n" + |
1820 |
" int foo(X that) {\n" + |
1821 |
" that.b|=true;\n" + // not a relevant usage |
1822 |
" --that.k;\n" + // not a relevant usage |
1823 |
" that.k+=3;\n" + // not a relevant usage |
1824 |
" that.j++;\n" + // relevant because unboxing is involved |
1825 |
" return that.i++;\n" + // value after increment is used |
1826 |
" }\n" + |
1827 |
"}" |
1828 |
}, |
1829 |
"----------\n" + |
1830 |
"1. WARNING in X.java (at line 3)\n" + |
1831 |
" private boolean b=false;\n" + |
1832 |
" ^\n" + |
1833 |
"The private field X.b is never used\n" + |
1834 |
"----------\n" + |
1835 |
"2. WARNING in X.java (at line 4)\n" + |
1836 |
" private int k = 2;\n" + |
1837 |
" ^\n" + |
1838 |
"The private field X.k is never used\n" + |
1839 |
"----------\n", |
1840 |
null/*classLibraries*/, |
1841 |
true/*shouldFlushOutputDirectory*/, |
1842 |
customOptions); |
1843 |
} |
1844 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=185682 |
1845 |
// variant with fields inside a private type |
1846 |
public void test0046_field_in_private_type() { |
1847 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) |
1848 |
return; |
1849 |
Map customOptions = getCompilerOptions(); |
1850 |
customOptions.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.WARNING); |
1851 |
this.runNegativeTest( |
1852 |
new String[] { |
1853 |
"X.java", |
1854 |
"class X {\n" + |
1855 |
" private class Y {\n" + |
1856 |
" int i=1;\n" + |
1857 |
" public boolean b=false;\n" + |
1858 |
" protected int k = 2;\n" + |
1859 |
" Integer j = 3;\n" + |
1860 |
" }\n" + |
1861 |
" int foo(Y y) {\n" + |
1862 |
" y.b|=true;\n" + // not a relevant usage |
1863 |
" --y.k;\n" + // not a relevant usage |
1864 |
" y.k+=3;\n" + // not a relevant usage |
1865 |
" y.j++;\n" + // relevant because unboxing is involved |
1866 |
" return y.i++;\n" + // value after increment is used |
1867 |
" }\n" + |
1868 |
"}" |
1869 |
}, |
1870 |
"----------\n" + |
1871 |
"1. WARNING in X.java (at line 4)\n" + |
1872 |
" public boolean b=false;\n" + |
1873 |
" ^\n" + |
1874 |
"The field X.Y.b is never used locally\n" + |
1875 |
"----------\n" + |
1876 |
"2. WARNING in X.java (at line 5)\n" + |
1877 |
" protected int k = 2;\n" + |
1878 |
" ^\n" + |
1879 |
"The field X.Y.k is never used locally\n" + |
1880 |
"----------\n", |
1881 |
null/*classLibraries*/, |
1882 |
true/*shouldFlushOutputDirectory*/, |
1883 |
customOptions); |
1884 |
} |
1885 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=185682 |
1886 |
public void test0047() { |
1887 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) |
1888 |
return; |
1889 |
Map customOptions = getCompilerOptions(); |
1890 |
customOptions.put(CompilerOptions.OPTION_ReportUnusedParameter, CompilerOptions.WARNING); |
1891 |
customOptions.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.WARNING); |
1892 |
this.runNegativeTest( |
1893 |
new String[] { |
1894 |
"X.java", |
1895 |
"class X {\n" + |
1896 |
" void foo(int param1, int param2, Integer param3) {\n" + |
1897 |
" boolean b=false;\n" + |
1898 |
" b|=true;\n" + // not a relevant usage |
1899 |
" param1++;\n" + // not a relevant usage |
1900 |
" param2 += 1;\n" + // not a relevant usage |
1901 |
" param3++;\n" + // relevant because unboxing is involved |
1902 |
" }\n" + |
1903 |
"}" |
1904 |
}, |
1905 |
"----------\n" + |
1906 |
"1. WARNING in X.java (at line 2)\n" + |
1907 |
" void foo(int param1, int param2, Integer param3) {\n" + |
1908 |
" ^^^^^^\n" + |
1909 |
"The parameter param1 is never read\n" + |
1910 |
"----------\n" + |
1911 |
"2. WARNING in X.java (at line 2)\n" + |
1912 |
" void foo(int param1, int param2, Integer param3) {\n" + |
1913 |
" ^^^^^^\n" + |
1914 |
"The parameter param2 is never read\n" + |
1915 |
"----------\n" + |
1916 |
"3. WARNING in X.java (at line 3)\n" + |
1917 |
" boolean b=false;\n" + |
1918 |
" ^\n" + |
1919 |
"The local variable b is never read\n" + |
1920 |
"----------\n", |
1921 |
null/*classLibraries*/, |
1922 |
true/*shouldFlushOutputDirectory*/, |
1923 |
customOptions); |
1924 |
} |
1925 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=185682 |
1926 |
// To verify that unused parameter warning is not shown for an implementing method's parameter when |
1927 |
// CompilerOptions.OPTION_ReportUnusedParameterWhenImplementingAbstract is disabled |
1928 |
public void test0048() { |
1929 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) |
1930 |
return; |
1931 |
Map customOptions = getCompilerOptions(); |
1932 |
customOptions.put(CompilerOptions.OPTION_ReportUnusedParameter, CompilerOptions.WARNING); |
1933 |
customOptions.put(CompilerOptions.OPTION_ReportUnusedParameterWhenImplementingAbstract, CompilerOptions.DISABLED); |
1934 |
customOptions.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.WARNING); |
1935 |
this.runNegativeTest( |
1936 |
new String[] { |
1937 |
"X.java", |
1938 |
"public class X extends A implements Y{\n" + |
1939 |
" public void foo(int param1, int param2, Integer param3) {\n" + // implementing method, so dont warn |
1940 |
" boolean b=false;\n" + |
1941 |
" b|=true;\n" + // not a relevant usage |
1942 |
" param1++;\n" + // not a relevant usage |
1943 |
" param2 += 1;\n" + // not a relevant usage |
1944 |
" param3++;\n" + // relevant because unboxing is involved |
1945 |
" }\n" + |
1946 |
" public void foo(int param1, int param2) {\n" + // warn |
1947 |
" boolean b=false;\n" + |
1948 |
" b|=true;\n" + // not a relevant usage |
1949 |
" param1++;\n" + // not a relevant usage |
1950 |
" param2 += 1;\n" + // not a relevant usage |
1951 |
" }\n" + |
1952 |
" public void bar(int param1, int param2, Integer param3) {\n" + // implementing method, so dont warn |
1953 |
" param1++;\n" + // not a relevant usage |
1954 |
" param2 += 1;\n" + // not a relevant usage |
1955 |
" param3++;\n" + // relevant because unboxing is involved |
1956 |
" }\n" + |
1957 |
"}\n" + |
1958 |
"interface Y{\n" + |
1959 |
" public void foo(int param1, int param2, Integer param3);" + |
1960 |
"}\n" + |
1961 |
"abstract class A{\n" + |
1962 |
" public abstract void bar(int param1, int param2, Integer param3);" + |
1963 |
"}\n" |
1964 |
}, |
1965 |
"----------\n" + |
1966 |
"1. WARNING in X.java (at line 3)\n" + |
1967 |
" boolean b=false;\n" + |
1968 |
" ^\n" + |
1969 |
"The local variable b is never read\n" + |
1970 |
"----------\n" + |
1971 |
"2. WARNING in X.java (at line 9)\n" + |
1972 |
" public void foo(int param1, int param2) {\n" + |
1973 |
" ^^^^^^\n" + |
1974 |
"The parameter param1 is never read\n" + |
1975 |
"----------\n" + |
1976 |
"3. WARNING in X.java (at line 9)\n" + |
1977 |
" public void foo(int param1, int param2) {\n" + |
1978 |
" ^^^^^^\n" + |
1979 |
"The parameter param2 is never read\n" + |
1980 |
"----------\n" + |
1981 |
"4. WARNING in X.java (at line 10)\n" + |
1982 |
" boolean b=false;\n" + |
1983 |
" ^\n" + |
1984 |
"The local variable b is never read\n" + |
1985 |
"----------\n", |
1986 |
null/*classLibraries*/, |
1987 |
true/*shouldFlushOutputDirectory*/, |
1988 |
customOptions); |
1989 |
} |
1990 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=185682 |
1991 |
// To verify that unused parameter warning is not shown for an overriding method's parameter when |
1992 |
// CompilerOptions.OPTION_ReportUnusedParameterWhenOverridingConcrete is disabled |
1993 |
public void test0049() { |
1994 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) |
1995 |
return; |
1996 |
Map customOptions = getCompilerOptions(); |
1997 |
customOptions.put(CompilerOptions.OPTION_ReportUnusedParameter, CompilerOptions.WARNING); |
1998 |
customOptions.put(CompilerOptions.OPTION_ReportUnusedParameterWhenOverridingConcrete, CompilerOptions.DISABLED); |
1999 |
customOptions.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.WARNING); |
2000 |
this.runNegativeTest( |
2001 |
new String[] { |
2002 |
"X.java", |
2003 |
"public class X extends A {\n" + |
2004 |
" public void foo(int param1, int param2, Integer param3) {\n" + // overriding method, so dont warn |
2005 |
" boolean b=false;\n" + |
2006 |
" b|=true;\n" + // not a relevant usage |
2007 |
" param1++;\n" + // not a relevant usage |
2008 |
" param2 += 1;\n" + // not a relevant usage |
2009 |
" param3++;\n" + // relevant because unboxing is involved |
2010 |
" }\n" + |
2011 |
" public void foo(int param1, Integer param3) {\n" + // overriding method, so dont warn |
2012 |
" param1++;\n" + // not a relevant usage |
2013 |
" param3++;\n" + // relevant because unboxing is involved |
2014 |
" }\n" + |
2015 |
"}\n" + |
2016 |
"class A{\n" + |
2017 |
" public void foo(int param1, int param2, Integer param3) {\n" + |
2018 |
" param1 -=1;\n" + // not a relevant usage |
2019 |
" param2--;\n" + // not a relevant usage |
2020 |
" param3--;\n" + // relevant because unboxing is involved |
2021 |
" }\n" + |
2022 |
"}\n" |
2023 |
}, |
2024 |
"----------\n" + |
2025 |
"1. WARNING in X.java (at line 3)\n" + |
2026 |
" boolean b=false;\n" + |
2027 |
" ^\n" + |
2028 |
"The local variable b is never read\n" + |
2029 |
"----------\n" + |
2030 |
"2. WARNING in X.java (at line 9)\n" + |
2031 |
" public void foo(int param1, Integer param3) {\n" + |
2032 |
" ^^^^^^\n" + |
2033 |
"The parameter param1 is never read\n" + |
2034 |
"----------\n" + |
2035 |
"3. WARNING in X.java (at line 15)\n" + |
2036 |
" public void foo(int param1, int param2, Integer param3) {\n" + |
2037 |
" ^^^^^^\n" + |
2038 |
"The parameter param1 is never read\n" + |
2039 |
"----------\n" + |
2040 |
"4. WARNING in X.java (at line 15)\n" + |
2041 |
" public void foo(int param1, int param2, Integer param3) {\n" + |
2042 |
" ^^^^^^\n" + |
2043 |
"The parameter param2 is never read\n" + |
2044 |
"----------\n", |
2045 |
null/*classLibraries*/, |
2046 |
true/*shouldFlushOutputDirectory*/, |
2047 |
customOptions); |
2048 |
} |
2049 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=185682 |
2050 |
// To verify that unused local warning is not shown for locals declared in unreachable code |
2051 |
public void test0050() { |
2052 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) |
2053 |
return; |
2054 |
Map customOptions = getCompilerOptions(); |
2055 |
customOptions.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.WARNING); |
2056 |
this.runNegativeTest( |
2057 |
new String[] { |
2058 |
"X.java", |
2059 |
"class X {\n" + |
2060 |
" int foo() {\n" + |
2061 |
" int i=1;\n" + |
2062 |
" if (false) {\n" + |
2063 |
" boolean b=false;\n" + // don't complain as unused |
2064 |
" b|=true;\n" + |
2065 |
" }\n" + // not a relevant usage |
2066 |
" int k = 2;\n" + |
2067 |
" --k;\n" + // not a relevant usage |
2068 |
" k+=3;\n" + // not a relevant usage |
2069 |
" Integer j = 3;\n" + |
2070 |
" j++;\n" + // relevant because unboxing is involved |
2071 |
" return i++;\n" + // value after increment is used |
2072 |
" }\n" + |
2073 |
"}" |
2074 |
}, |
2075 |
"----------\n" + |
2076 |
"1. WARNING in X.java (at line 4)\n" + |
2077 |
" if (false) {\n" + |
2078 |
" boolean b=false;\n" + |
2079 |
" b|=true;\n" + |
2080 |
" }\n" + |
2081 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
2082 |
"Dead code\n" + |
2083 |
"----------\n" + |
2084 |
"2. WARNING in X.java (at line 8)\n" + |
2085 |
" int k = 2;\n" + |
2086 |
" ^\n" + |
2087 |
"The local variable k is never read\n" + |
2088 |
"----------\n", |
2089 |
null/*classLibraries*/, |
2090 |
true/*shouldFlushOutputDirectory*/, |
2091 |
customOptions); |
2092 |
} |
1686 |
} |
2093 |
} |