Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 298022

Summary: bogus warning about parameter assignment for primitives
Product: [Eclipse Project] JDT Reporter: Robert Wenner <robert.wenner>
Component: CoreAssignee: 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:

Description Robert Wenner CLA 2009-12-16 18:35:25 EST
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.
Comment 1 Srikanth Sankaran CLA 2009-12-18 00:06:17 EST
(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.
Comment 2 Srikanth Sankaran CLA 2009-12-18 00:10:45 EST
(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.
Comment 3 Robert Wenner CLA 2009-12-18 11:50:11 EST
Yes, please close the bug. I confused assigning to the parameter vs calling methods on it that do modify it. Sorry.