View | Details | Raw Unified | Return to bug 53773 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jdt/core/tests/compiler/regression/AssignmentTest.java (+98 lines)
Lines 1472-1477 Link Here
1472
		},
1472
		},
1473
		"a=11b=1");
1473
		"a=11b=1");
1474
}
1474
}
1475
// warn upon parameter assignment
1476
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=53773
1477
public void test040() {
1478
	Map options = getCompilerOptions();
1479
	options.put(CompilerOptions.OPTION_ReportParameterAssignment, CompilerOptions.ERROR);
1480
	this.runNegativeTest(
1481
		new String[] {
1482
			"X.java",
1483
			"public class X {\n" + 
1484
			"  void foo(boolean b) {\n" + 
1485
			"    b = false;\n" + 
1486
			"  }\n" + 
1487
			"}\n",
1488
		},
1489
		"----------\n" + 
1490
		"1. ERROR in X.java (at line 3)\n" + 
1491
		"	b = false;\n" + 
1492
		"	^\n" + 
1493
		"The parameter b should not be assigned\n" + 
1494
		"----------\n",
1495
		null, true, options);
1496
}
1497
// warn upon parameter assignment
1498
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=53773
1499
// diagnose within fake reachable code
1500
public void test041() {
1501
	Map options = getCompilerOptions();
1502
	options.put(CompilerOptions.OPTION_ReportParameterAssignment, CompilerOptions.ERROR);
1503
	this.runNegativeTest(
1504
		new String[] {
1505
			"X.java",
1506
			"public class X {\n" + 
1507
			"  void foo(boolean b) {\n" + 
1508
			"    if (false) {\n" + 
1509
			"      b = false;\n" + 
1510
			"    }\n" + 
1511
			"  }\n" + 
1512
			"}\n",
1513
		},
1514
		"----------\n" + 
1515
		"1. ERROR in X.java (at line 4)\n" + 
1516
		"	b = false;\n" + 
1517
		"	^\n" + 
1518
		"The parameter b should not be assigned\n" + 
1519
		"----------\n",
1520
		null, true, options);
1521
}
1522
// warn upon parameter assignment
1523
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=53773
1524
// diagnose within fake reachable code
1525
public void test042() {
1526
	Map options = getCompilerOptions();
1527
	options.put(CompilerOptions.OPTION_ReportParameterAssignment, CompilerOptions.ERROR);
1528
	this.runNegativeTest(
1529
		new String[] {
1530
			"X.java",
1531
			"public class X {\n" + 
1532
			"  void foo(boolean b) {\n" + 
1533
			"    if (true) {\n" + 
1534
			"      return;\n" + 
1535
			"    }\n" + 
1536
			"    b = false;\n" + 
1537
			"  }\n" + 
1538
			"}\n",
1539
		},
1540
		"----------\n" + 
1541
		"1. ERROR in X.java (at line 6)\n" + 
1542
		"	b = false;\n" + 
1543
		"	^\n" + 
1544
		"The parameter b should not be assigned\n" + 
1545
		"----------\n",
1546
		null, true, options);
1547
}
1548
// warn upon parameter assignment
1549
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=53773
1550
// we only show the 'assignment to final' error here
1551
public void test043() {
1552
	Map options = getCompilerOptions();
1553
	options.put(CompilerOptions.OPTION_ReportParameterAssignment, CompilerOptions.ERROR);
1554
	this.runNegativeTest(
1555
		new String[] {
1556
			"X.java",
1557
			"public class X {\n" + 
1558
			"  void foo(final boolean b) {\n" + 
1559
			"    if (false) {\n" + 
1560
			"      b = false;\n" + 
1561
			"    }\n" + 
1562
			"  }\n" + 
1563
			"}\n",
1564
		},
1565
		"----------\n" + 
1566
		"1. ERROR in X.java (at line 4)\n" + 
1567
		"	b = false;\n" + 
1568
		"	^\n" + 
1569
		"The final local variable b cannot be assigned. It must be blank and not using a compound assignment\n" + 
1570
		"----------\n",
1571
		null, true, options);
1572
}
1475
public static Class testClass() {
1573
public static Class testClass() {
1476
	return AssignmentTest.class;
1574
	return AssignmentTest.class;
1477
}
1575
}
(-)src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java (+37 lines)
Lines 2082-2087 Link Here
2082
        false);
2082
        false);
2083
}
2083
}
2084
2084
2085
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=53773
2086
// complain on assignment to parameters
2087
public void test037() {
2088
	this.runNegativeTest(
2089
		new String[] {
2090
			"X.java",
2091
			"public class X {\n" +
2092
			"  void foo(int i, final int j) {\n" +
2093
			"    i =  0; // warning\n" +
2094
			"    j =  0; // error\n" +
2095
			"  }\n" +
2096
			"}\n"},
2097
		"\"" + OUTPUT_DIR +  File.separator + "X.java\""
2098
		+ " -1.5 "
2099
		+ " -cp \"" + OUTPUT_DIR + "\""
2100
		+ " -warn:+paramAssign"
2101
		+ " -proceedOnError"
2102
		+ " -d \"" + OUTPUT_DIR + "\"",
2103
		"",
2104
		"----------\n" +
2105
		"1. WARNING in ---OUTPUT_DIR_PLACEHOLDER---" +
2106
		File.separator + "X.java\n" +
2107
		" (at line 3)\n" +
2108
		"	i =  0; // warning\n" +
2109
		"	^\n" +
2110
		"The parameter i should not be assigned\n" +
2111
		"----------\n" +
2112
		"2. ERROR in ---OUTPUT_DIR_PLACEHOLDER---" +  File.separator + "X.java\n" +
2113
		" (at line 4)\n" +
2114
		"	j =  0; // error\n" +
2115
		"	^\n" +
2116
		"The final local variable j cannot be assigned. It must be blank and not using a compound assignment\n" +
2117
		"----------\n" +
2118
		"2 problems (1 error, 1 warning)",
2119
		true);
2120
}
2121
2085
public static Class testClass() {
2122
public static Class testClass() {
2086
	return BatchCompilerTest.class;
2123
	return BatchCompilerTest.class;
2087
}
2124
}
(-)compiler/org/eclipse/jdt/core/compiler/IProblem.java (-1 / +5 lines)
Lines 72-77 Link Here
72
 *     IBM Corporation - added the following constants
72
 *     IBM Corporation - added the following constants
73
 *								   IllegalUsageOfQualifiedTypeReference
73
 *								   IllegalUsageOfQualifiedTypeReference
74
 *								   InvalidDigit
74
 *								   InvalidDigit
75
 *     IBM Corporation - added the following constants
76
 *								   ParameterAssignment
75
 *******************************************************************************/
77
 *******************************************************************************/
76
package org.eclipse.jdt.core.compiler;
78
package org.eclipse.jdt.core.compiler;
77
 
79
 
Lines 286-292 Link Here
286
	int TooManyArrayDimensions = Internal + 68;
288
	int TooManyArrayDimensions = Internal + 68;
287
	/** @since 2.1 */
289
	/** @since 2.1 */
288
	int BytecodeExceeds64KLimitForConstructor = Internal + 69;
290
	int BytecodeExceeds64KLimitForConstructor = Internal + 69;
289
291
	/** @since 3.2 */
292
	int ParameterAssignment = Internal + 860;
293
	
290
	// fields
294
	// fields
291
	int UndefinedField = FieldRelated + 70;
295
	int UndefinedField = FieldRelated + 70;
292
	int NotVisibleField = FieldRelated + 71;
296
	int NotVisibleField = FieldRelated + 71;
(-)batch/org/eclipse/jdt/internal/compiler/batch/messages.properties (+1 lines)
Lines 153-158 Link Here
153
\      noEffectAssign     + assignment without effect\n\
153
\      noEffectAssign     + assignment without effect\n\
154
\      null                 missing or redundant null check\n\
154
\      null                 missing or redundant null check\n\
155
\      over-ann             missing @Override annotation\n\
155
\      over-ann             missing @Override annotation\n\
156
\      paramAssign          assignment to a parameter\n\
156
\      pkgDefaultMethod   + attempt to override package-default method\n\
157
\      pkgDefaultMethod   + attempt to override package-default method\n\
157
\      raw                  usage of raw type\n\
158
\      raw                  usage of raw type\n\
158
\      semicolon            unnecessary semicolon, empty statement\n\
159
\      semicolon            unnecessary semicolon, empty statement\n\
(-)batch/org/eclipse/jdt/internal/compiler/batch/Main.java (+4 lines)
Lines 1912-1917 Link Here
1912
						this.options.put(
1912
						this.options.put(
1913
								CompilerOptions.OPTION_ReportUnusedLabel,
1913
								CompilerOptions.OPTION_ReportUnusedLabel,
1914
								isEnabling ? CompilerOptions.WARNING : CompilerOptions.IGNORE);
1914
								isEnabling ? CompilerOptions.WARNING : CompilerOptions.IGNORE);
1915
					} else if (token.equals("paramAssign")) { //$NON-NLS-1$
1916
						this.options.put(
1917
							CompilerOptions.OPTION_ReportParameterAssignment,
1918
							isEnabling ? CompilerOptions.WARNING : CompilerOptions.IGNORE);
1915
					} else {
1919
					} else {
1916
						throw new InvalidInputException(Main.bind("configure.invalidWarning", token)); //$NON-NLS-1$
1920
						throw new InvalidInputException(Main.bind("configure.invalidWarning", token)); //$NON-NLS-1$
1917
					}
1921
					}
(-)compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties (+2 lines)
Lines 558-560 Link Here
558
857 = Incorrect number of type arguments for generic constructor <{3}>{0}({1}) of type {2}; it cannot be parameterized with arguments <{4}>
558
857 = Incorrect number of type arguments for generic constructor <{3}>{0}({1}) of type {2}; it cannot be parameterized with arguments <{4}>
559
858 = The parameterized constructor <{3}>{0}({1}) of type {2} is not applicable for the arguments ({4})
559
858 = The parameterized constructor <{3}>{0}({1}) of type {2} is not applicable for the arguments ({4})
560
859 = The constructor {0}({1}) of raw type {2} is no longer generic; it cannot be parameterized with arguments <{3}>
560
859 = The constructor {0}({1}) of raw type {2} is no longer generic; it cannot be parameterized with arguments <{3}>
561
562
860 = The parameter {0} should not be assigned
(-)compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java (+12 lines)
Lines 1469-1474 Link Here
1469
1469
1470
		case IProblem.JavadocMissing:
1470
		case IProblem.JavadocMissing:
1471
			return CompilerOptions.MissingJavadocComments;
1471
			return CompilerOptions.MissingJavadocComments;
1472
1473
		case IProblem.ParameterAssignment:
1474
			return CompilerOptions.ParameterAssignment;
1472
	}
1475
	}
1473
	return 0;
1476
	return 0;
1474
}
1477
}
Lines 4484-4489 Link Here
4484
		compUnitDecl.currentPackage == null ? 0 : compUnitDecl.currentPackage.sourceStart,
4487
		compUnitDecl.currentPackage == null ? 0 : compUnitDecl.currentPackage.sourceStart,
4485
		compUnitDecl.currentPackage == null ? 0 : compUnitDecl.currentPackage.sourceEnd);
4488
		compUnitDecl.currentPackage == null ? 0 : compUnitDecl.currentPackage.sourceEnd);
4486
}
4489
}
4490
public void parameterAssignment(LocalVariableBinding local, ASTNode location) {
4491
	String[] arguments = new String[] { new String(local.readableName())};
4492
	this.handle(
4493
		IProblem.ParameterAssignment,
4494
		arguments,
4495
		arguments,
4496
		location.sourceStart,
4497
		location.sourceEnd);
4498
}
4487
private String parameterBoundAsString(TypeVariableBinding typeVariable, boolean makeShort) {
4499
private String parameterBoundAsString(TypeVariableBinding typeVariable, boolean makeShort) {
4488
    StringBuffer nameBuffer = new StringBuffer(10);
4500
    StringBuffer nameBuffer = new StringBuffer(10);
4489
    if (typeVariable.firstBound == typeVariable.superclass) {
4501
    if (typeVariable.firstBound == typeVariable.superclass) {
(-)compiler/org/eclipse/jdt/internal/compiler/ast/SingleNameReference.java (+3 lines)
Lines 119-124 Link Here
119
						currentScope.problemReporter().cannotAssignToFinalOuterLocal(localBinding, this);
119
						currentScope.problemReporter().cannotAssignToFinalOuterLocal(localBinding, this);
120
					}
120
					}
121
				}
121
				}
122
				else /* avoid double diagnostic */ if (localBinding.isArgument) {
123
					currentScope.problemReporter().parameterAssignment(localBinding, this);
124
				}
122
				flowInfo.markAsDefinitelyAssigned(localBinding);
125
				flowInfo.markAsDefinitelyAssigned(localBinding);
123
		}
126
		}
124
		manageEnclosingInstanceAccessIfNecessary(currentScope, flowInfo);
127
		manageEnclosingInstanceAccessIfNecessary(currentScope, flowInfo);
(-)compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java (-1 / +13 lines)
Lines 103-108 Link Here
103
	public static final String OPTION_ReportUnhandledWarningToken =  "org.eclipse.jdt.core.compiler.problem.unhandledWarningToken"; //$NON-NLS-1$
103
	public static final String OPTION_ReportUnhandledWarningToken =  "org.eclipse.jdt.core.compiler.problem.unhandledWarningToken"; //$NON-NLS-1$
104
	public static final String OPTION_ReportUnusedLabel =  "org.eclipse.jdt.core.compiler.problem.unusedLabel"; //$NON-NLS-1$
104
	public static final String OPTION_ReportUnusedLabel =  "org.eclipse.jdt.core.compiler.problem.unusedLabel"; //$NON-NLS-1$
105
	public static final String OPTION_FatalOptionalError =  "org.eclipse.jdt.core.compiler.problem.fatalOptionalError"; //$NON-NLS-1$
105
	public static final String OPTION_FatalOptionalError =  "org.eclipse.jdt.core.compiler.problem.fatalOptionalError"; //$NON-NLS-1$
106
	public static final String OPTION_ReportParameterAssignment =  "org.eclipse.jdt.core.compiler.problem.parameterAssignment"; //$NON-NLS-1$
106
	
107
	
107
	// Backward compatibility
108
	// Backward compatibility
108
	public static final String OPTION_ReportInvalidAnnotation = "org.eclipse.jdt.core.compiler.problem.invalidAnnotation"; //$NON-NLS-1$
109
	public static final String OPTION_ReportInvalidAnnotation = "org.eclipse.jdt.core.compiler.problem.invalidAnnotation"; //$NON-NLS-1$
Lines 185-190 Link Here
185
	public static final long UnhandledWarningToken = ASTNode.Bit45L;
186
	public static final long UnhandledWarningToken = ASTNode.Bit45L;
186
	public static final long RawTypeReference = ASTNode.Bit46L;
187
	public static final long RawTypeReference = ASTNode.Bit46L;
187
	public static final long UnusedLabel = ASTNode.Bit47L;
188
	public static final long UnusedLabel = ASTNode.Bit47L;
189
	public static final long ParameterAssignment = ASTNode.Bit48L;
188
	
190
	
189
	// Default severity level for handlers
191
	// Default severity level for handlers
190
	public long errorThreshold = 0;
192
	public long errorThreshold = 0;
Lines 393-398 Link Here
393
		optionsMap.put(OPTION_ReportNullReference, getSeverityString(NullReference));
395
		optionsMap.put(OPTION_ReportNullReference, getSeverityString(NullReference));
394
		optionsMap.put(OPTION_SuppressWarnings, this.suppressWarnings ? ENABLED : DISABLED); 
396
		optionsMap.put(OPTION_SuppressWarnings, this.suppressWarnings ? ENABLED : DISABLED); 
395
		optionsMap.put(OPTION_ReportUnhandledWarningToken, getSeverityString(UnhandledWarningToken));
397
		optionsMap.put(OPTION_ReportUnhandledWarningToken, getSeverityString(UnhandledWarningToken));
398
		optionsMap.put(OPTION_ReportParameterAssignment, getSeverityString(ParameterAssignment));
396
		return optionsMap;		
399
		return optionsMap;		
397
	}
400
	}
398
	
401
	
Lines 633-639 Link Here
633
		if ((optionValue = optionsMap.get(OPTION_ReportIncompleteEnumSwitch)) != null) updateSeverity(IncompleteEnumSwitch, optionValue);
636
		if ((optionValue = optionsMap.get(OPTION_ReportIncompleteEnumSwitch)) != null) updateSeverity(IncompleteEnumSwitch, optionValue);
634
		if ((optionValue = optionsMap.get(OPTION_ReportUnhandledWarningToken)) != null) updateSeverity(UnhandledWarningToken, optionValue);
637
		if ((optionValue = optionsMap.get(OPTION_ReportUnhandledWarningToken)) != null) updateSeverity(UnhandledWarningToken, optionValue);
635
		if ((optionValue = optionsMap.get(OPTION_ReportUnusedLabel)) != null) updateSeverity(UnusedLabel, optionValue);
638
		if ((optionValue = optionsMap.get(OPTION_ReportUnusedLabel)) != null) updateSeverity(UnusedLabel, optionValue);
636
				
639
		if ((optionValue = optionsMap.get(OPTION_ReportParameterAssignment)) != null) updateSeverity(ParameterAssignment, optionValue);
640
637
		// Javadoc options
641
		// Javadoc options
638
		if ((optionValue = optionsMap.get(OPTION_DocCommentSupport)) != null) {
642
		if ((optionValue = optionsMap.get(OPTION_DocCommentSupport)) != null) {
639
			if (ENABLED.equals(optionValue)) {
643
			if (ENABLED.equals(optionValue)) {
Lines 798-803 Link Here
798
		buf.append("\n\t- unhandled warning token: ").append(getSeverityString(UnhandledWarningToken)); //$NON-NLS-1$
802
		buf.append("\n\t- unhandled warning token: ").append(getSeverityString(UnhandledWarningToken)); //$NON-NLS-1$
799
		buf.append("\n\t- unused label: ").append(getSeverityString(UnusedLabel)); //$NON-NLS-1$
803
		buf.append("\n\t- unused label: ").append(getSeverityString(UnusedLabel)); //$NON-NLS-1$
800
		buf.append("\n\t- treat optional error as fatal: ").append(this.treatOptionalErrorAsFatal ? ENABLED : DISABLED); //$NON-NLS-1$
804
		buf.append("\n\t- treat optional error as fatal: ").append(this.treatOptionalErrorAsFatal ? ENABLED : DISABLED); //$NON-NLS-1$
805
		buf.append("\n\t- parameter assignment: ").append(getSeverityString(ParameterAssignment)); //$NON-NLS-1$
801
		return buf.toString();
806
		return buf.toString();
802
	}
807
	}
803
808
Lines 898-903 Link Here
898
			OPTION_ReportUnusedPrivateMember,
903
			OPTION_ReportUnusedPrivateMember,
899
			OPTION_ReportVarargsArgumentNeedCast,
904
			OPTION_ReportVarargsArgumentNeedCast,
900
			OPTION_ReportUnhandledWarningToken,
905
			OPTION_ReportUnhandledWarningToken,
906
			OPTION_ReportParameterAssignment,
901
		};
907
		};
902
		return result;
908
		return result;
903
	}
909
	}
Lines 949-954 Link Here
949
					return "unchecked"; //$NON-NLS-1$
955
					return "unchecked"; //$NON-NLS-1$
950
				case (int) UnusedLabel:
956
				case (int) UnusedLabel:
951
					return "unused"; //$NON-NLS-1$
957
					return "unused"; //$NON-NLS-1$
958
				case (int) (ParameterAssignment >>> 32) :
959
					return "paramAssign"; //$NON-NLS-1$
952
			}
960
			}
953
		}
961
		}
954
		return null;
962
		return null;
Lines 985-990 Link Here
985
				if ("nls".equals(warningToken)) //$NON-NLS-1$
993
				if ("nls".equals(warningToken)) //$NON-NLS-1$
986
					return NonExternalizedString;
994
					return NonExternalizedString;
987
				break;
995
				break;
996
			case 'p' :
997
				if ("paramAssign".equals(warningToken)) //$NON-NLS-1$
998
					return ParameterAssignment;
999
				break;
988
			case 's' :
1000
			case 's' :
989
				if ("serial".equals(warningToken)) //$NON-NLS-1$
1001
				if ("serial".equals(warningToken)) //$NON-NLS-1$
990
					return MissingSerialVersion;
1002
					return MissingSerialVersion;

Return to bug 53773