| Summary: | bogus warning about parameter assignment for primitives | ||
|---|---|---|---|
| Product: | [Eclipse Project] JDT | Reporter: | Robert Wenner <robert.wenner> |
| Component: | Core | Assignee: | Srikanth Sankaran <srikanth_sankaran> |
| Status: | CLOSED INVALID | QA Contact: | |
| Severity: | minor | ||
| Priority: | P3 | CC: | srikanth_sankaran |
| Version: | 3.6 | ||
| Target Milestone: | --- | ||
| Hardware: | PC | ||
| OS: | Mac OS X - Carbon (unsup.) | ||
| Whiteboard: | |||
(In reply to comment #0) > Build Identifier: Version: 3.4.2 Build id: M20090211-1700 > > I get a "the parameter bar should not be assigned" for this code: See bug# 53773 which was the original request for this warning. Comment#5 in particular acknowledges a common use pattern that will nevertheless trigger this warning. > public static int foo(int bar) { > bar %= 100; > return bar; > } > > IMHO this should not matter since bar is an integer (i.e., not an Object) so > there are no side effects on the caller. Why does it matter whether the parameter being assigned to is an object or a primitive type ? In both cases any assignment to the parameter doesn't change the actual parameter the warning is warning about this. > This test passes: > > @SuppressWarnings("boxing") > @Test > public void testFoo() { > int bar = 1001; > foo(bar); > assertEquals("Changed bar", 1001, bar); > } > > If the assignment would have any side effect, the caller's value would be > changed. Likewise this program: public class Q { public static void main(String[] args) { String before = "before method call"; new Q().foo (before); if (!"before method call".equals(before)) System.out.println("All hell broke loose"); } void foo(String param) { param = "after method call"; } } The case of primitive needing different treatment vis a vis Objects is simply not there ? I plan to close this as INVALID. If I have misunderstood your point, please let me know. (In reply to comment #1) > (In reply to comment #0) > > Build Identifier: Version: 3.4.2 Build id: M20090211-1700 > > > > I get a "the parameter bar should not be assigned" for this code: > > See bug# 53773 which was the original request for this warning. > Comment#5 in particular acknowledges a common use pattern bug# 53773 comment# 5 that is. Yes, please close the bug. I confused assigning to the parameter vs calling methods on it that do modify it. Sorry. |
Build Identifier: Version: 3.4.2 Build id: M20090211-1700 I get a "the parameter bar should not be assigned" for this code: public static int foo(int bar) { bar %= 100; return bar; } IMHO this should not matter since bar is an integer (i.e., not an Object) so there are no side effects on the caller. This test passes: @SuppressWarnings("boxing") @Test public void testFoo() { int bar = 1001; foo(bar); assertEquals("Changed bar", 1001, bar); } If the assignment would have any side effect, the caller's value would be changed. I can add a suppressWarnings, but I think the warning is bogus and should be fixed for primitive types. I totally agree that warning is a good idea when assigning to an object. Reproducible: Always Steps to Reproduce: See above.