View | Details | Raw Unified | Return to bug 106478
Collapse All | Expand All

(-)batch/org/eclipse/jdt/internal/compiler/batch/Main.java (+5 lines)
Lines 3474-3479 Link Here
3474
					CompilerOptions.OPTION_ReportUnnecessaryElse,
3474
					CompilerOptions.OPTION_ReportUnnecessaryElse,
3475
					isEnabling ? CompilerOptions.WARNING : CompilerOptions.IGNORE);
3475
					isEnabling ? CompilerOptions.WARNING : CompilerOptions.IGNORE);
3476
				return;
3476
				return;
3477
			} else if (token.equals("unnecessaryOperator")) {//$NON-NLS-1$
3478
				this.options.put(
3479
						CompilerOptions.OPTION_ReportUnnecessaryOperator,
3480
						isEnabling ? CompilerOptions.WARNING : CompilerOptions.IGNORE);
3481
					return;
3477
			} else if (token.equals("unusedThrown")) { //$NON-NLS-1$
3482
			} else if (token.equals("unusedThrown")) { //$NON-NLS-1$
3478
				this.options.put(
3483
				this.options.put(
3479
					CompilerOptions.OPTION_ReportUnusedDeclaredThrownException,
3484
					CompilerOptions.OPTION_ReportUnusedDeclaredThrownException,
(-)batch/org/eclipse/jdt/internal/compiler/batch/messages.properties (+1 lines)
Lines 287-292 Link Here
287
\      typeHiding         + type parameter hiding another type\n\
287
\      typeHiding         + type parameter hiding another type\n\
288
\      unchecked          + unchecked type operation\n\
288
\      unchecked          + unchecked type operation\n\
289
\      unnecessaryElse      unnecessary else clause\n\
289
\      unnecessaryElse      unnecessary else clause\n\
290
\      unnecessaryOperator  unnecessary ''+'' operator for unary expression\n\
290
\      unqualifiedField     unqualified reference to field\n\
291
\      unqualifiedField     unqualified reference to field\n\
291
\      unused               macro for unusedArgument, unusedImport, unusedLabel,\n\
292
\      unused               macro for unusedArgument, unusedImport, unusedLabel,\n\
292
\                               unusedLocal, unusedPrivate, unusedThrown,\n\
293
\                               unusedLocal, unusedPrivate, unusedThrown,\n\
(-)compiler/org/eclipse/jdt/core/compiler/IProblem.java (+2 lines)
Lines 591-596 Link Here
591
	int InvalidHighSurrogate = Syntax + Internal + 264;
591
	int InvalidHighSurrogate = Syntax + Internal + 264;
592
	/** @since 3.2 */
592
	/** @since 3.2 */
593
	int UnnecessaryNLSTag = Internal + 265;
593
	int UnnecessaryNLSTag = Internal + 265;
594
	/** @since 3.6 */
595
	int UnnecessaryOperator = Internal + 266;
594
596
595
	// type related problems
597
	// type related problems
596
	/** @since 3.1 */
598
	/** @since 3.1 */
(-)compiler/org/eclipse/jdt/internal/compiler/ast/UnaryExpression.java (-1 / +6 lines)
Lines 235-243 Link Here
235
			case TWIDDLE :
235
			case TWIDDLE :
236
				tableId = LEFT_SHIFT;
236
				tableId = LEFT_SHIFT;
237
				break;
237
				break;
238
			case PLUS :
239
				tableId = PLUS;
240
				scope.problemReporter().unnecessaryOperator(this);
241
				break;
238
			default :
242
			default :
243
				// minus case
239
				tableId = MINUS;
244
				tableId = MINUS;
240
		} //+ and - cases
245
		}
241
246
242
		// the code is an int
247
		// the code is an int
243
		// (cast)  left   Op (cast)  rigth --> result
248
		// (cast)  left   Op (cast)  rigth --> result
(-)compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java (-1 / +8 lines)
Lines 125-131 Link Here
125
	public static final String OPTION_ReportMissingSynchronizedOnInheritedMethod =  "org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod"; //$NON-NLS-1$
125
	public static final String OPTION_ReportMissingSynchronizedOnInheritedMethod =  "org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod"; //$NON-NLS-1$
126
	public static final String OPTION_ReportMissingHashCodeMethod =  "org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod"; //$NON-NLS-1$
126
	public static final String OPTION_ReportMissingHashCodeMethod =  "org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod"; //$NON-NLS-1$
127
	public static final String OPTION_ReportDeadCode =  "org.eclipse.jdt.core.compiler.problem.deadCode"; //$NON-NLS-1$
127
	public static final String OPTION_ReportDeadCode =  "org.eclipse.jdt.core.compiler.problem.deadCode"; //$NON-NLS-1$
128
	public static final String OPTION_ReportDeadCodeInTrivialIfStatement =  "org.eclipse.jdt.core.compiler.problem.deadCodeInTrivialIfStatement"; //$NON-NLS-1$
128
	public static final String OPTION_ReportDeadCodeInTrivialIfStatement = "org.eclipse.jdt.core.compiler.problem.deadCodeInTrivialIfStatement"; //$NON-NLS-1$
129
	public static final String OPTION_ReportUnnecessaryOperator = "org.eclipse.jdt.core.compiler.problem.unnecessaryOperator"; //$NON-NLS-1$
129
130
130
	// Backward compatibility
131
	// Backward compatibility
131
	public static final String OPTION_ReportInvalidAnnotation = "org.eclipse.jdt.core.compiler.problem.invalidAnnotation"; //$NON-NLS-1$
132
	public static final String OPTION_ReportInvalidAnnotation = "org.eclipse.jdt.core.compiler.problem.invalidAnnotation"; //$NON-NLS-1$
Lines 230-235 Link Here
230
	// group 2
231
	// group 2
231
	public static final int ShouldImplementHashcode = IrritantSet.GROUP2 | ASTNode.Bit1;
232
	public static final int ShouldImplementHashcode = IrritantSet.GROUP2 | ASTNode.Bit1;
232
	public static final int DeadCode = IrritantSet.GROUP2 | ASTNode.Bit2;
233
	public static final int DeadCode = IrritantSet.GROUP2 | ASTNode.Bit2;
234
	public static final int UnnecessaryOperator = IrritantSet.GROUP2 | ASTNode.Bit3;
233
235
234
	// Severity level for handlers
236
	// Severity level for handlers
235
	/** 
237
	/** 
Lines 512-517 Link Here
512
				return OPTION_ReportMissingHashCodeMethod;
514
				return OPTION_ReportMissingHashCodeMethod;
513
			case DeadCode :
515
			case DeadCode :
514
				return OPTION_ReportDeadCode;
516
				return OPTION_ReportDeadCode;
517
			case UnnecessaryOperator :
518
				return OPTION_ReportUnnecessaryOperator;
515
		}
519
		}
516
		return null;
520
		return null;
517
	}
521
	}
Lines 595-600 Link Here
595
			OPTION_ReportAssertIdentifier,
599
			OPTION_ReportAssertIdentifier,
596
			OPTION_ReportAutoboxing,
600
			OPTION_ReportAutoboxing,
597
			OPTION_ReportDeadCode,
601
			OPTION_ReportDeadCode,
602
			OPTION_ReportUnnecessaryOperator,
598
			OPTION_ReportDeprecation,
603
			OPTION_ReportDeprecation,
599
			OPTION_ReportDiscouragedReference,
604
			OPTION_ReportDiscouragedReference,
600
			OPTION_ReportEmptyStatement,
605
			OPTION_ReportEmptyStatement,
Lines 887-892 Link Here
887
		optionsMap.put(OPTION_ReportMissingHashCodeMethod, getSeverityString(ShouldImplementHashcode));
892
		optionsMap.put(OPTION_ReportMissingHashCodeMethod, getSeverityString(ShouldImplementHashcode));
888
		optionsMap.put(OPTION_ReportDeadCode, getSeverityString(DeadCode));
893
		optionsMap.put(OPTION_ReportDeadCode, getSeverityString(DeadCode));
889
		optionsMap.put(OPTION_ReportDeadCodeInTrivialIfStatement, this.reportDeadCodeInTrivialIfStatement ? ENABLED : DISABLED);
894
		optionsMap.put(OPTION_ReportDeadCodeInTrivialIfStatement, this.reportDeadCodeInTrivialIfStatement ? ENABLED : DISABLED);
895
		optionsMap.put(OPTION_ReportUnnecessaryOperator, getSeverityString(UnnecessaryOperator));
890
		return optionsMap;
896
		return optionsMap;
891
	}
897
	}
892
898
Lines 1280-1285 Link Here
1280
		if ((optionValue = optionsMap.get(OPTION_ReportMissingSynchronizedOnInheritedMethod)) != null) updateSeverity(MissingSynchronizedModifierInInheritedMethod, optionValue);
1286
		if ((optionValue = optionsMap.get(OPTION_ReportMissingSynchronizedOnInheritedMethod)) != null) updateSeverity(MissingSynchronizedModifierInInheritedMethod, optionValue);
1281
		if ((optionValue = optionsMap.get(OPTION_ReportMissingHashCodeMethod)) != null) updateSeverity(ShouldImplementHashcode, optionValue);
1287
		if ((optionValue = optionsMap.get(OPTION_ReportMissingHashCodeMethod)) != null) updateSeverity(ShouldImplementHashcode, optionValue);
1282
		if ((optionValue = optionsMap.get(OPTION_ReportDeadCode)) != null) updateSeverity(DeadCode, optionValue);
1288
		if ((optionValue = optionsMap.get(OPTION_ReportDeadCode)) != null) updateSeverity(DeadCode, optionValue);
1289
		if ((optionValue = optionsMap.get(OPTION_ReportUnnecessaryOperator)) != null) updateSeverity(UnnecessaryOperator, optionValue);
1283
1290
1284
		// Javadoc options
1291
		// Javadoc options
1285
		if ((optionValue = optionsMap.get(OPTION_DocCommentSupport)) != null) {
1292
		if ((optionValue = optionsMap.get(OPTION_DocCommentSupport)) != null) {
(-)compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java (-6 / +3 lines)
Lines 96-102 Link Here
96
				| CompilerOptions.ComparingIdentical)
96
				| CompilerOptions.ComparingIdentical)
97
			// group-2 warnings enabled by default
97
			// group-2 warnings enabled by default
98
			.set(CompilerOptions.DeadCode);
98
			.set(CompilerOptions.DeadCode);
99
			
99
100
		ALL.setAll();
100
		ALL.setAll();
101
		HIDING
101
		HIDING
102
			.set(CompilerOptions.FieldHiding)
102
			.set(CompilerOptions.FieldHiding)
Lines 212-220 Link Here
212
			return this;
212
			return this;
213
		boolean wasNoOp = true;
213
		boolean wasNoOp = true;
214
		for (int i = 0; i < GROUP_MAX; i++) {
214
		for (int i = 0; i < GROUP_MAX; i++) {
215
			int otherIrritant = other.bits[i] & ~GROUP_MASK; // erase the
215
			int otherIrritant = other.bits[i] & ~GROUP_MASK; // erase the group bits
216
																	// group
217
																	// bits
218
			if ((this.bits[i] & otherIrritant) != otherIrritant) {
216
			if ((this.bits[i] & otherIrritant) != otherIrritant) {
219
				wasNoOp = false;
217
				wasNoOp = false;
220
				this.bits[i] |= otherIrritant;
218
				this.bits[i] |= otherIrritant;
Lines 225-232 Link Here
225
	
223
	
226
	public IrritantSet setAll() {
224
	public IrritantSet setAll() {
227
		for (int i = 0; i < GROUP_MAX; i++) {
225
		for (int i = 0; i < GROUP_MAX; i++) {
228
			this.bits[i] |= 0xFFFFFFFF & ~GROUP_MASK; // erase the group
226
			this.bits[i] |= 0xFFFFFFFF & ~GROUP_MASK; // erase the group bits;
229
															// bits;
230
		}
227
		}
231
		return this;
228
		return this;
232
	}
229
	}
(-)compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java (+14 lines)
Lines 411-416 Link Here
411
			
411
			
412
		case IProblem.DeadCode:
412
		case IProblem.DeadCode:
413
			return CompilerOptions.DeadCode;
413
			return CompilerOptions.DeadCode;
414
			
415
		case IProblem.UnnecessaryOperator :
416
			return CompilerOptions.UnnecessaryOperator;
414
	}
417
	}
415
	return 0;
418
	return 0;
416
}
419
}
Lines 481-486 Link Here
481
			case CompilerOptions.UnusedWarningToken :
484
			case CompilerOptions.UnusedWarningToken :
482
			case CompilerOptions.UnusedLabel :
485
			case CompilerOptions.UnusedLabel :
483
			case CompilerOptions.RedundantSuperinterface :	
486
			case CompilerOptions.RedundantSuperinterface :	
487
			case CompilerOptions.UnnecessaryOperator :
484
				return CategorizedProblem.CAT_UNNECESSARY_CODE;
488
				return CategorizedProblem.CAT_UNNECESSARY_CODE;
485
489
486
			case CompilerOptions.UsingDeprecatedAPI :
490
			case CompilerOptions.UsingDeprecatedAPI :
Lines 6731-6736 Link Here
6731
		sourceStart,
6735
		sourceStart,
6732
		sourceEnd);
6736
		sourceEnd);
6733
}
6737
}
6738
public void unnecessaryOperator(UnaryExpression expression) {
6739
	int severity = computeSeverity(IProblem.UnnecessaryOperator);
6740
	if (severity == ProblemSeverities.Ignore) return;
6741
	this.handle(
6742
			IProblem.UnnecessaryOperator,
6743
			NoArgument,
6744
			NoArgument,
6745
			expression.sourceStart,
6746
			expression.sourceEnd);
6747
}
6734
public void unnecessaryTypeArgumentsForMethodInvocation(MethodBinding method, TypeBinding[] genericTypeArguments, TypeReference[] typeArguments) {
6748
public void unnecessaryTypeArgumentsForMethodInvocation(MethodBinding method, TypeBinding[] genericTypeArguments, TypeReference[] typeArguments) {
6735
	String methodName = method.isConstructor()
6749
	String methodName = method.isConstructor()
6736
		? new String(method.declaringClass.shortReadableName())
6750
		? new String(method.declaringClass.shortReadableName())
(-)compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties (+1 lines)
Lines 223-228 Link Here
223
263 = Invalid low surrogate: must be within 0xDC00 and 0xDFFF
223
263 = Invalid low surrogate: must be within 0xDC00 and 0xDFFF
224
264 = Invalid high surrogate: must be within 0xD800 and 0xDBFF
224
264 = Invalid high surrogate: must be within 0xD800 and 0xDBFF
225
265 = Unnecessary $NON-NLS$ tag
225
265 = Unnecessary $NON-NLS$ tag
226
266 = Unnecessary operator
226
227
227
280 = Discouraged access: {0}
228
280 = Discouraged access: {0}
228
229
(-)model/org/eclipse/jdt/core/JavaCore.java (+12 lines)
Lines 742-747 Link Here
742
	 */
742
	 */
743
	public static final String COMPILER_PB_UNNECESSARY_ELSE = PLUGIN_ID + ".compiler.problem.unnecessaryElse"; //$NON-NLS-1$
743
	public static final String COMPILER_PB_UNNECESSARY_ELSE = PLUGIN_ID + ".compiler.problem.unnecessaryElse"; //$NON-NLS-1$
744
	/**
744
	/**
745
	 * Compiler option ID: Reporting Unnecessary Operator.
746
	 * <p>When enabled, the compiler will issue an error or a warning when an unary expression is using a '+' operator.</p>
747
	 * <dl>
748
	 * <dt>Option id:</dt><dd><code>"org.eclipse.jdt.core.compiler.problem.unnecessaryOperator"</code></dd>
749
	 * <dt>Possible values:</dt><dd><code>{ "error", "warning", "ignore" }</code></dd>
750
	 * <dt>Default:</dt><dd><code>"ignore"</code></dd>
751
	 * </dl>
752
	 * @since 3.6
753
	 * @category CompilerOptionID
754
	 */
755
	public static final String COMPILER_PB_UNNECESSARY_OPERATOR = PLUGIN_ID + ".compiler.problem.unnecessaryOperator"; //$NON-NLS-1$
756
	/**
745
	 * Compiler option ID: Reporting Undocumented Empty Block.
757
	 * Compiler option ID: Reporting Undocumented Empty Block.
746
	 * <p>When enabled, the compiler will issue an error or a warning when an empty block is detected and it is not
758
	 * <p>When enabled, the compiler will issue an error or a warning when an empty block is detected and it is not
747
	 *    documented with any comment.
759
	 *    documented with any comment.
(-)src/org/eclipse/jdt/core/tests/compiler/regression/AssignmentTest.java (-3 / +54 lines)
Lines 33-39 Link Here
33
// All specified tests which does not belong to the class are skipped...
33
// All specified tests which does not belong to the class are skipped...
34
static {
34
static {
35
//	TESTS_NAMES = new String[] { "test000" };
35
//	TESTS_NAMES = new String[] { "test000" };
36
//	TESTS_NUMBERS = new int[] { 67 };
36
//	TESTS_NUMBERS = new int[] { 69 };
37
//	TESTS_RANGE = new int[] { 11, -1 };
37
//	TESTS_RANGE = new int[] { 11, -1 };
38
}
38
}
39
public static Test suite() {
39
public static Test suite() {
Lines 45-52 Link Here
45
 * http://bugs.eclipse.org/bugs/show_bug.cgi?id=27235
45
 * http://bugs.eclipse.org/bugs/show_bug.cgi?id=27235
46
 */
46
 */
47
public void test001() {
47
public void test001() {
48
	this.runConformTest(
48
	this.runConformTest(		new String[] {
49
		new String[] {
50
			"X.java",
49
			"X.java",
51
			"public class X {	\n" +
50
			"public class X {	\n" +
52
			"    int i;	\n" +
51
			"    int i;	\n" +
Lines 1954-1959 Link Here
1954
		options
1953
		options
1955
	);
1954
	);
1956
}
1955
}
1956
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=106478
1957
public void test068() {
1958
	Map options = getCompilerOptions();
1959
	options.put(CompilerOptions.OPTION_ReportUnnecessaryOperator, CompilerOptions.ERROR);
1960
	this.runNegativeTest(
1961
		new String[] {
1962
			"X.java",
1963
			"public class X {\n" +
1964
			"	public boolean test() {\n" +
1965
			"		int i = 0;\n" + 
1966
			"		i =+ 1;\n" + 
1967
			"		System.out.println(i);\n" + 
1968
			"	}\n" +
1969
			"}"
1970
		},
1971
		"----------\n" + 
1972
		"1. ERROR in X.java (at line 4)\n" + 
1973
		"	i =+ 1;\n" + 
1974
		"	   ^^^\n" + 
1975
		"Unnecessary operator\n" + 
1976
		"----------\n",
1977
		null,
1978
		true,
1979
		options
1980
	);
1981
}
1982
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=106478
1983
public void test069() {
1984
	Map options = getCompilerOptions();
1985
	options.put(CompilerOptions.OPTION_ReportUnnecessaryOperator, CompilerOptions.ERROR);
1986
	this.runNegativeTest(
1987
		new String[] {
1988
			"X.java",
1989
			"public class X {\n" +
1990
			"	public boolean test() {\n" +
1991
			"		int i = 0;\n" + 
1992
			"		i = +1;\n" + 
1993
			"		System.out.println(i);\n" + 
1994
			"	}\n" +
1995
			"}"
1996
		},
1997
		"----------\n" + 
1998
		"1. ERROR in X.java (at line 4)\n" + 
1999
		"	i = +1;\n" + 
2000
		"	    ^^\n" + 
2001
		"Unnecessary operator\n" + 
2002
		"----------\n",
2003
		null,
2004
		true,
2005
		options
2006
	);
2007
}
1957
public static Class testClass() {
2008
public static Class testClass() {
1958
	return AssignmentTest.class;
2009
	return AssignmentTest.class;
1959
}
2010
}
(-)src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java (+2 lines)
Lines 1692-1697 Link Here
1692
        "      typeHiding         + type parameter hiding another type\n" +
1692
        "      typeHiding         + type parameter hiding another type\n" +
1693
        "      unchecked          + unchecked type operation\n" +
1693
        "      unchecked          + unchecked type operation\n" +
1694
        "      unnecessaryElse      unnecessary else clause\n" +
1694
        "      unnecessaryElse      unnecessary else clause\n" +
1695
        "      unnecessaryOperator  unnecessary ''+'' operator for unary expression\n" + 
1695
        "      unqualifiedField     unqualified reference to field\n" +
1696
        "      unqualifiedField     unqualified reference to field\n" +
1696
        "      unused               macro for unusedArgument, unusedImport, unusedLabel,\n" +
1697
        "      unused               macro for unusedArgument, unusedImport, unusedLabel,\n" +
1697
        "                               unusedLocal, unusedPrivate, unusedThrown,\n" +
1698
        "                               unusedLocal, unusedPrivate, unusedThrown,\n" +
Lines 1838-1843 Link Here
1838
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock\" value=\"ignore\"/>\n" +
1839
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock\" value=\"ignore\"/>\n" +
1839
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unhandledWarningToken\" value=\"warning\"/>\n" +
1840
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unhandledWarningToken\" value=\"warning\"/>\n" +
1840
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unnecessaryElse\" value=\"ignore\"/>\n" +
1841
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unnecessaryElse\" value=\"ignore\"/>\n" +
1842
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unnecessaryOperator\" value=\"ignore\"/>\n" + 
1841
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck\" value=\"ignore\"/>\n" +
1843
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck\" value=\"ignore\"/>\n" +
1842
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess\" value=\"ignore\"/>\n" +
1844
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess\" value=\"ignore\"/>\n" +
1843
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException\" value=\"ignore\"/>\n" +
1845
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException\" value=\"ignore\"/>\n" +

Return to bug 106478