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

Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/ast/SingleNameReference.java (-4 / +25 lines)
Lines 77-86 Link Here
77
					currentScope.problemReporter().uninitializedLocalVariable(localBinding, this);
77
					currentScope.problemReporter().uninitializedLocalVariable(localBinding, this);
78
					// we could improve error msg here telling "cannot use compound assignment on final local variable"
78
					// we could improve error msg here telling "cannot use compound assignment on final local variable"
79
				}
79
				}
80
				if (isReachable) {
80
				if (localBinding.useFlag != LocalVariableBinding.USED) {
81
					localBinding.useFlag = LocalVariableBinding.USED;
81
					// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318571
82
				} else if (localBinding.useFlag == LocalVariableBinding.UNUSED) {
82
					// access from compound assignment does not prevent "unused" warning, unless unboxing is involved:
83
					localBinding.useFlag = LocalVariableBinding.FAKE_USED;
83
					if (isReachable && (this.implicitConversion & TypeIds.UNBOXING) != 0) {
84
						localBinding.useFlag = LocalVariableBinding.USED;
85
					} else {
86
						localBinding.useFlag = LocalVariableBinding.FAKE_USED;
87
					}
84
				}
88
				}
85
		}
89
		}
86
	}
90
	}
Lines 461-466 Link Here
461
 * are optimized in one access: e.g "a = a + 1" optimized into "a++".
465
 * are optimized in one access: e.g "a = a + 1" optimized into "a++".
462
 */
466
 */
463
public void generateCompoundAssignment(BlockScope currentScope, CodeStream codeStream, Expression expression, int operator, int assignmentImplicitConversion, boolean valueRequired) {
467
public void generateCompoundAssignment(BlockScope currentScope, CodeStream codeStream, Expression expression, int operator, int assignmentImplicitConversion, boolean valueRequired) {
468
	if (!valueRequired && ((this.bits & ASTNode.RestrictiveFlagMASK) == Binding.LOCAL)) {
469
		LocalVariableBinding localBinding = (LocalVariableBinding) this.binding;
470
		if (localBinding.useFlag == LocalVariableBinding.FAKE_USED) {
471
			// report the case of a local variable that is unread except for this compound assignment
472
			currentScope.problemReporter().unusedLocalVariable(localBinding.declaration);
473
			localBinding.useFlag = LocalVariableBinding.USED; // don't report again
474
		}
475
	}
464
	this.generateCompoundAssignment(
476
	this.generateCompoundAssignment(
465
		currentScope,
477
		currentScope,
466
		codeStream,
478
		codeStream,
Lines 661-666 Link Here
661
			return;
673
			return;
662
		case Binding.LOCAL : // assigning to a local variable
674
		case Binding.LOCAL : // assigning to a local variable
663
			LocalVariableBinding localBinding = (LocalVariableBinding) this.binding;
675
			LocalVariableBinding localBinding = (LocalVariableBinding) this.binding;
676
677
			if (!valueRequired) {
678
				if (localBinding.useFlag == LocalVariableBinding.FAKE_USED) {
679
					// report the case of a local variable that is unread except for postIncrement expressions
680
					currentScope.problemReporter().unusedLocalVariable(localBinding.declaration);
681
					localBinding.useFlag = LocalVariableBinding.USED; // don't report again
682
				}
683
			}
684
664
			// using incr bytecode if possible
685
			// using incr bytecode if possible
665
			if (localBinding.type == TypeBinding.INT) {
686
			if (localBinding.type == TypeBinding.INT) {
666
				if (valueRequired) {
687
				if (valueRequired) {
(-)src/org/eclipse/jdt/core/tests/compiler/regression/ProgrammingProblemsTest.java (+38 lines)
Lines 1683-1686 Link Here
1683
		"The assignment to variable nvx has no effect\n" + 
1683
		"The assignment to variable nvx has no effect\n" + 
1684
		"----------\n");
1684
		"----------\n");
1685
}
1685
}
1686
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=185682
1687
public void test0046() {
1688
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
1689
		return;
1690
	Map customOptions = getCompilerOptions();
1691
	customOptions.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.WARNING);
1692
	this.runNegativeTest(
1693
			new String[] {
1694
				"X.java",
1695
				"class X {\n" + 
1696
				"    int foo() {\n" + 
1697
				"        int i=1;\n" + 
1698
				"        boolean b=false;\n" + 
1699
				"        b|=true;\n" + 			// not a relevant usage
1700
				"        int k = 2;\n" + 
1701
				"        --k;\n" + 				// not a relevant usage
1702
				"        k+=3;\n" + 			// not a relevant usage
1703
				"        Integer j = 3;\n" + 
1704
				"        j++;\n" + 				// relevant because unboxing is involved
1705
				"        return i++;\n" + 		// value after increment is used
1706
				"    }\n" + 
1707
				"}"
1708
			},
1709
			"----------\n" + 
1710
			"1. WARNING in X.java (at line 4)\n" + 
1711
			"	boolean b=false;\n" + 
1712
			"	        ^\n" + 
1713
			"The local variable b is never read\n" + 
1714
			"----------\n" + 
1715
			"2. WARNING in X.java (at line 6)\n" + 
1716
			"	int k = 2;\n" + 
1717
			"	    ^\n" + 
1718
			"The local variable k is never read\n" + 
1719
			"----------\n",
1720
			null/*classLibraries*/,
1721
			true/*shouldFlushOutputDirectory*/,
1722
			customOptions);
1723
}
1686
}
1724
}

Return to bug 185682