### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: compiler/org/eclipse/jdt/core/compiler/IProblem.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/core/compiler/IProblem.java,v retrieving revision 1.220 diff -u -r1.220 IProblem.java --- compiler/org/eclipse/jdt/core/compiler/IProblem.java 11 Nov 2009 19:28:04 -0000 1.220 +++ compiler/org/eclipse/jdt/core/compiler/IProblem.java 5 Dec 2009 19:33:22 -0000 @@ -115,7 +115,8 @@ * RedundantSuperinterface * Benjamin Muskalla - added the following constants * MissingSynchronizedModifierInInheritedMethod - * + * Stephan Herrmann - added the following constants + * UnusedObjectAllocation *******************************************************************************/ package org.eclipse.jdt.core.compiler; @@ -428,6 +429,8 @@ int UnhandledExceptionInImplicitConstructorCall = TypeRelated + 147; // expressions + /** @since 3.6 */ + int UnusedObjectAllocation = Internal + 148; /** @since 3.5 */ int DeadCode = Internal + 149; int ArrayReferenceRequired = Internal + 150; Index: compiler/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java,v retrieving revision 1.82 diff -u -r1.82 AllocationExpression.java --- compiler/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java 25 Nov 2009 04:56:02 -0000 1.82 +++ compiler/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java 5 Dec 2009 19:33:23 -0000 @@ -7,6 +7,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Stephan Herrmann - Contribution for bug 236385 *******************************************************************************/ package org.eclipse.jdt.internal.compiler.ast; @@ -82,8 +83,11 @@ } public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) { + if (!valueRequired) + currentScope.problemReporter().unusedObjectAllocation(this); + int pc = codeStream.position; - MethodBinding codegenBinding = this.binding.original(); + MethodBinding codegenBinding = this.binding.original(); ReferenceBinding allocatedType = codegenBinding.declaringClass; codeStream.new_(allocatedType); Index: compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java,v retrieving revision 1.225 diff -u -r1.225 CompilerOptions.java --- compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java 26 Nov 2009 16:20:04 -0000 1.225 +++ compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java 5 Dec 2009 19:33:27 -0000 @@ -8,6 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation * Benjamin Muskalla - Contribution for bug 239066 + * Stephan Herrmann - Contribution for bug 236385 *******************************************************************************/ package org.eclipse.jdt.internal.compiler.impl; @@ -127,6 +128,7 @@ public static final String OPTION_ReportDeadCode = "org.eclipse.jdt.core.compiler.problem.deadCode"; //$NON-NLS-1$ public static final String OPTION_ReportDeadCodeInTrivialIfStatement = "org.eclipse.jdt.core.compiler.problem.deadCodeInTrivialIfStatement"; //$NON-NLS-1$ public static final String OPTION_ReportTasks = "org.eclipse.jdt.core.compiler.problem.tasks"; //$NON-NLS-1$ + public static final String OPTION_ReportUnusedObjectAllocation = "org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation"; //$NON-NLS-1$ // Backward compatibility public static final String OPTION_ReportInvalidAnnotation = "org.eclipse.jdt.core.compiler.problem.invalidAnnotation"; //$NON-NLS-1$ @@ -232,6 +234,7 @@ public static final int ShouldImplementHashcode = IrritantSet.GROUP2 | ASTNode.Bit1; public static final int DeadCode = IrritantSet.GROUP2 | ASTNode.Bit2; public static final int Tasks = IrritantSet.GROUP2 | ASTNode.Bit3; + public static final int UnusedObjectAllocation = IrritantSet.GROUP2 | ASTNode.Bit4; // Severity level for handlers /** @@ -515,6 +518,8 @@ return OPTION_ReportMissingHashCodeMethod; case DeadCode : return OPTION_ReportDeadCode; + case UnusedObjectAllocation: + return OPTION_ReportUnusedObjectAllocation; } return null; } @@ -641,6 +646,7 @@ OPTION_ReportUnusedDeclaredThrownException, OPTION_ReportUnusedImport, OPTION_ReportUnusedLocal, + OPTION_ReportUnusedObjectAllocation, OPTION_ReportUnusedParameter, OPTION_ReportUnusedPrivateMember, OPTION_ReportVarargsArgumentNeedCast, @@ -701,6 +707,7 @@ case UnusedPrivateMember : case UnusedDeclaredThrownException : case DeadCode : + case UnusedObjectAllocation : return "unused"; //$NON-NLS-1$ case DiscouragedReference : case ForbiddenReference : @@ -891,6 +898,7 @@ optionsMap.put(OPTION_ReportDeadCode, getSeverityString(DeadCode)); optionsMap.put(OPTION_ReportDeadCodeInTrivialIfStatement, this.reportDeadCodeInTrivialIfStatement ? ENABLED : DISABLED); optionsMap.put(OPTION_ReportTasks, getSeverityString(Tasks)); + optionsMap.put(OPTION_ReportUnusedObjectAllocation, getSeverityString(UnusedObjectAllocation)); return optionsMap; } @@ -1287,6 +1295,7 @@ if ((optionValue = optionsMap.get(OPTION_ReportMissingHashCodeMethod)) != null) updateSeverity(ShouldImplementHashcode, optionValue); if ((optionValue = optionsMap.get(OPTION_ReportDeadCode)) != null) updateSeverity(DeadCode, optionValue); if ((optionValue = optionsMap.get(OPTION_ReportTasks)) != null) updateSeverity(Tasks, optionValue); + if ((optionValue = optionsMap.get(OPTION_ReportUnusedObjectAllocation)) != null) updateSeverity(UnusedObjectAllocation, optionValue); // Javadoc options if ((optionValue = optionsMap.get(OPTION_DocCommentSupport)) != null) { @@ -1488,6 +1497,7 @@ buf.append("\n\t- dead code: ").append(getSeverityString(DeadCode)); //$NON-NLS-1$ buf.append("\n\t- dead code in trivial if statement: ").append(this.reportDeadCodeInTrivialIfStatement ? ENABLED : DISABLED); //$NON-NLS-1$ buf.append("\n\t- tasks severity: ").append(getSeverityString(Tasks)); //$NON-NLS-1$ + buf.append("\n\t- unused object allocation: ").append(getSeverityString(UnusedObjectAllocation)); //$NON-NLS-1$ return buf.toString(); } Index: compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java,v retrieving revision 1.8 diff -u -r1.8 IrritantSet.java --- compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java 6 Oct 2009 19:24:18 -0000 1.8 +++ compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java 5 Dec 2009 19:33:27 -0000 @@ -97,7 +97,8 @@ // group-2 warnings enabled by default .set( CompilerOptions.DeadCode - |CompilerOptions.Tasks); + |CompilerOptions.Tasks + |CompilerOptions.UnusedObjectAllocation); ALL.setAll(); HIDING @@ -117,7 +118,8 @@ .set(CompilerOptions.UnusedImport) .set(CompilerOptions.UnusedTypeArguments) .set(CompilerOptions.RedundantSuperinterface) - .set(CompilerOptions.DeadCode); + .set(CompilerOptions.DeadCode) + .set(CompilerOptions.UnusedObjectAllocation); String suppressRawWhenUnchecked = System.getProperty("suppressRawWhenUnchecked"); //$NON-NLS-1$ if (suppressRawWhenUnchecked != null && "true".equalsIgnoreCase(suppressRawWhenUnchecked)) { //$NON-NLS-1$ UNCHECKED.set(CompilerOptions.RawTypeReference); Index: compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java,v retrieving revision 1.402 diff -u -r1.402 ProblemReporter.java --- compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java 23 Nov 2009 16:45:35 -0000 1.402 +++ compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java 5 Dec 2009 19:33:40 -0000 @@ -414,6 +414,9 @@ case IProblem.Task : return CompilerOptions.Tasks; + + case IProblem.UnusedObjectAllocation: + return CompilerOptions.UnusedObjectAllocation; } return 0; } @@ -464,6 +467,7 @@ case CompilerOptions.MissingSynchronizedModifierInInheritedMethod : case CompilerOptions.ShouldImplementHashcode : case CompilerOptions.DeadCode : + case CompilerOptions.UnusedObjectAllocation : return CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM; case CompilerOptions.OverriddenPackageDefaultMethod : @@ -7124,6 +7128,14 @@ localDecl.sourceStart, localDecl.sourceEnd); } +public void unusedObjectAllocation(AllocationExpression allocationExpression) { + this.handle( + IProblem.UnusedObjectAllocation, + NoArgument, + NoArgument, + allocationExpression.sourceStart, + allocationExpression.sourceEnd); +} public void unusedPrivateConstructor(ConstructorDeclaration constructorDecl) { int severity = computeSeverity(IProblem.UnusedPrivateConstructor); Index: compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties,v retrieving revision 1.255 diff -u -r1.255 messages.properties --- compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties 11 Nov 2009 19:28:04 -0000 1.255 +++ compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties 5 Dec 2009 19:33:42 -0000 @@ -123,6 +123,8 @@ 146 = Default constructor cannot handle exception type {0} thrown by implicit super constructor. Must define an explicit constructor 147 = Unhandled exception type {0} thrown by implicit super constructor + +148 = Allocating an object that is never used 149 = Dead code 150 = The type of the expression must be an array type but it resolved to {0} 151 = Must explicitly convert the char[] to a String Index: model/org/eclipse/jdt/core/JavaCore.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java,v retrieving revision 1.643 diff -u -r1.643 JavaCore.java --- model/org/eclipse/jdt/core/JavaCore.java 26 Oct 2009 17:21:09 -0000 1.643 +++ model/org/eclipse/jdt/core/JavaCore.java 5 Dec 2009 19:33:52 -0000 @@ -81,7 +81,9 @@ * COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION_EXEMPT_EXCEPTION_AND_THROWABLE * IBM Corporation - added getOptionForConfigurableSeverity(int) * Benjamin Muskalla - added COMPILER_PB_MISSING_SYNCHRONIZED_ON_INHERITED_METHOD + * Stephan Herrmann - added COMPILER_PB_UNUSED_OBJECT_ALLOCATION *******************************************************************************/ + package org.eclipse.jdt.core; import java.util.ArrayList; @@ -1541,6 +1543,19 @@ */ public static final String COMPILER_PB_MISSING_SYNCHRONIZED_ON_INHERITED_METHOD = PLUGIN_ID + ".compiler.problem.missingSynchronizedOnInheritedMethod"; //$NON-NLS-1$ /** + * Compiler option ID: Reporting Allocation of an Unused Object. + *

When enabled, the compiler will issue an error or a warning if an object is allocated but never used, + * neither by holding a reference nor by invoking one of the object's methods. + *

+ *
Option id:
"org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation"
+ *
Possible values:
{ "error", "warning", "ignore" }
+ *
Default:
"warning"
+ *
+ * @since 3.6 + * @category CompilerOptionID + */ + public static final String COMPILER_PB_UNUSED_OBJECT_ALLOCATION = PLUGIN_ID + ".compiler.problem.unusedObjectAllocation"; //$NON-NLS-1$ + /** * Core option ID: Computing Project Build Order. *

Indicate whether JavaCore should enforce the project build order to be based on * the classpath prerequisite chain. When requesting to compute, this takes over #P org.eclipse.jdt.core.tests.compiler Index: src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java,v retrieving revision 1.199 diff -u -r1.199 BatchCompilerTest.java --- src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java 7 Oct 2009 14:58:12 -0000 1.199 +++ src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java 5 Dec 2009 19:34:10 -0000 @@ -1856,6 +1856,7 @@ "