Bug 18320 - Compiler Warning/Error/Ignore when Assigning to a Parameter
Summary: Compiler Warning/Error/Ignore when Assigning to a Parameter
Status: RESOLVED DUPLICATE of bug 25092
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.0   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: 2.1 M3   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-05-30 10:53 EDT by Simon Archer CLA
Modified: 2002-11-12 10:32 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Simon Archer CLA 2002-05-30 10:53:06 EDT
In many cases, assigning back to a parameter is a mistake.  For example:

	public void foo(String name) {
		name = "Eclipse";
	}

Or perhaps even:
	public void setName(String name) {
		name = name;  // Should have said, this.name = name
	}

Please could this be added as a compiler error/warning/ignore preference.
Comment 1 Erich Gamma CLA 2002-05-30 11:42:22 EDT
moving to JDT Core
Comment 2 Philipe Mulet CLA 2002-05-30 11:53:07 EDT
I actually tend to like this code pattern. 
An optional warning may make sense however.
Will queue post 2.0.
Comment 3 Simon Archer CLA 2002-05-30 13:13:02 EDT
To avoid any confusion, I wish to clarify that the following code SHOULD NOT 
cause a warning:

	public void setName(String name) {
		this.name = name; // This is OK.
	}
Comment 4 Philipe Mulet CLA 2002-07-25 06:59:39 EDT
Reopening
Comment 5 Philipe Mulet CLA 2002-07-25 07:00:57 EDT
Clearing resolution
Comment 6 Philipe Mulet CLA 2002-10-08 12:36:25 EDT
Simon, would you actually want to see no-op assignments flagged or just 
assignments to arguments ?

It seems you were mostly annoyed by "name = name" assignments. I could imagine 
similar annoyance with field/local variable.

Or would you rather want to see flagged any assignment on arguments:
e.g.
void iterate(int count){
  while (count-- > 0) doSomething();
}
Comment 7 Simon Archer CLA 2002-10-08 12:54:59 EDT
Philippe, I think there are two issues here.

Firstly there should be the ability to optionally flag assignments to 
parameters.  While assigning to parameters is perfectly legal in Java, some 
people choose not to do this as a personal style.  Typically people learn not 
to do this having used languages that disallow it, such as Smalltalk.  I could 
imagine a corporate coding standard that states that assigning back to a 
parameter is not allowed since it contributes to more confusing code and errir 
prone code.  Granted, Java does allow the programmer to create "final" 
parameters (that cannot be assigned back to), but I have never seen anyone use 
this in real code.

Secondly, my PR was originally trying to find a way to flag a common mistake 
where the a parameter/local hides a field and forgetting to qualify the field 
reference with "this." results in devestating results at runtime.  On a number 
of occassions I've made this mistake myself and been suprised that a field is 
still null even after it seems to have been initialized.

The botton line here is to have optional compiler warnings that make it easier 
to write accurate code.  In my experience you rarely need to assign back to 
parameters and extra care is needed when creating parameters/locals that hide 
fields.

Comment 8 Adam Kiezun CLA 2002-10-22 09:13:26 EDT
+1 here (for a warning on assignments to params)
Comment 9 Philipe Mulet CLA 2002-10-24 18:24:53 EDT
Let's try to sneak it for M4
Comment 10 Philipe Mulet CLA 2002-11-12 09:17:31 EST
Given we know detect assignments having no effect (see bug 25092) from build 
20021112 on (2.1 stream), is it ok to close this one ?

You can always force a variable to not be assigned by using the final modifier 
(even on arguments).
Comment 11 Simon Archer CLA 2002-11-12 09:46:47 EST
Sounds good to me.  Yes, final can, but rarely is, used on parameters.  With 
the functionaly provided by bug 25092 I think this bug can be considered 
RESOLVED/FIXED.
Comment 12 Philipe Mulet CLA 2002-11-12 10:32:58 EST
Closing as duplicate of bug 25092

*** This bug has been marked as a duplicate of 25092 ***