Bug 53773 - [plan] [compiler] Warning on assignments to parameters
Summary: [plan] [compiler] Warning on assignments to parameters
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: PC All
: P3 enhancement (vote)
Target Milestone: 3.2 M5   Edit
Assignee: Maxime Daniel CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-03-04 12:25 EST by Adam Kiezun CLA
Modified: 2006-02-14 06:27 EST (History)
2 users (show)

See Also:


Attachments
Suggested patch for compiler (11.61 KB, patch)
2005-08-11 06:02 EDT, Maxime Daniel CLA
no flags Details | Diff
Tests (5.99 KB, patch)
2005-08-11 06:02 EDT, Maxime Daniel CLA
no flags Details | Diff
New patch, integrated Eclipse format, for second review (15.79 KB, patch)
2005-11-23 05:51 EST, Maxime Daniel CLA
no flags Details | Diff
Committed changes (or very close to it) (15.42 KB, patch)
2006-01-04 11:13 EST, Maxime Daniel CLA
no flags Details | Diff
HEAD - suppressed warning token and improved build notes (4.76 KB, patch)
2006-01-09 11:31 EST, Maxime Daniel CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Adam Kiezun CLA 2004-03-04 12:25:15 EST
3.0 m7
assigning to params is a bad smell - but declaring things final is way too much
pain for anybody

it's be cool if the compiler could detect and report these.
e.g.

foo(int i){
  i = 9;  //sign of problems
}


(in the code above, there's actually also another problem - the param is killed
before it's used - another strange smell)
Comment 1 Philipe Mulet CLA 2004-03-25 06:40:55 EST
Will reconsider post 3.0
Comment 2 Philipe Mulet CLA 2005-05-17 05:09:34 EDT
Simple workaround, make your parameters final.
Comment 3 Adam Kiezun CLA 2005-05-17 09:17:10 EDT
i know but like i said above "declaring things final is way too much
pain for anybody" :)
Comment 4 Adam Kiezun CLA 2005-08-04 10:08:27 EDT
an observation: in cases where the original value of the parameter is never
actually used but killed right away with a new value this is even more likely to
be a bug.

Comment 5 Philipe Mulet CLA 2005-08-08 05:30:30 EDT
I personally like reusing parameters as variables to avoid runtime penalty, but
this is a valid option to introduce in the compiler (and easy to implement too).
Comment 6 Maxime Daniel CLA 2005-08-11 06:01:01 EDT
Added a diagnostic for parameter assignment.
This diagnostic can be tuned (error, warning, ignore) and is ignored by default.
Added a warning external name (for use in -warn:+ or @SuppressWarnings), which
is parameter-assignment.
The function won't be active until JDT UI surfaces the new warning.
See attachments for patches (jdt core plus tests).
Comment 7 Maxime Daniel CLA 2005-08-11 06:02:02 EDT
Created attachment 26010 [details]
Suggested patch for compiler

Needs committer review.
Comment 8 Maxime Daniel CLA 2005-08-11 06:02:17 EDT
Created attachment 26011 [details]
Tests
Comment 9 Philipe Mulet CLA 2005-11-15 07:48:56 EST
Review:

Batch command-line argument should be aligned with existing ones
   "parameter-assignment" --> "paramAssign"

> // REVIEW argument would be more homogeneous with existing code, but JLS puts a
> clear emphasis on parameter -- see p.211 for parameter/argument disc.
Where did you find that ? We should always denote a parameter using "parameter".

> REVIEW the 'accidental' seems extraneous here (see assignment in condition)
I would tend to agree, pls file a separate bug for rewording the error msg (and
tune the impacted test cases)
Comment 10 Philipe Mulet CLA 2005-11-15 07:50:04 EST
Also, the implementation is hooked into flow analysis. Isn't it too late ? We
are likely not going to report issues in unreachable code, are we ?
Comment 11 Maxime Daniel CLA 2005-11-22 04:30:52 EST
(In reply to comment #9)
> > // REVIEW argument would be more homogeneous with existing code, but JLS puts a
> > clear emphasis on parameter -- see p.211 for parameter/argument disc.
> Where did you find that ? We should always denote a parameter using "parameter".
We have (at least):
 unusedArgument       unread method parameter
into the messages.properties file for the compiler.
Not checked for others.
Comment 12 Maxime Daniel CLA 2005-11-22 05:39:40 EST
(In reply to comment #9)
> Review:
...
> > REVIEW the 'accidental' seems extraneous here (see assignment in condition)
> I would tend to agree, pls file a separate bug for rewording the error msg (and
> tune the impacted test cases)
Entered Bug 117458.
Comment 13 Maxime Daniel CLA 2005-11-22 06:16:43 EST
(In reply to comment #10)
> Also, the implementation is hooked into flow analysis. Isn't it too late ? We
> are likely not going to report issues in unreachable code, are we ?
Added test cases to AssignmentTest that show that we do report the issue. 
I chose the implementation spot by affinity with other assignment diagnostics, within analyseAssignment. Other code there explicitly checks for reachability before raising errors, which I did not do for the parameter assignment check.
Also, this offered the opportunity not to diagnose assignments to final parameters, which I felt were underlined enough by the 'assignment to final' error. On the latter point, a case of interest would be an assignment to a final parameter within unreachable code. The current implementation raises an 'assignment to final' error, hence I believe there is not much value into raising a supplementary 'parameter assignment' warning.
Comment 14 Maxime Daniel CLA 2005-11-22 06:21:15 EST
(In reply to comment #9)
> Review:
> 
> Batch command-line argument should be aligned with existing ones
>    "parameter-assignment" --> "paramAssign"
Next patch will implement the name 'paramAssign' for the batch command-line argument. Other associated constants (numeric and non numeric) names won't be changed.
Comment 15 Maxime Daniel CLA 2005-11-23 05:51:14 EST
Created attachment 30477 [details]
New patch, integrated Eclipse format, for second review
Comment 16 Maxime Daniel CLA 2006-01-04 09:13:36 EST
Until JDT UI surfaces this option, it can be exerciced by assigning a value
to org.eclipse.jdt.core.compiler.problem.parameterAssignment within the JDT Core preferences.
For example, add the following line:
org.eclipse.jdt.core.compiler.problem.parameterAssignment=warning

into:
.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs
under Linux (relatively to the workspace root).
The same with \ instead of / under Windows.
Comment 17 Maxime Daniel CLA 2006-01-04 11:05:27 EST
Added tests:
AssignmentTest#test040-43
BatchCompilerTest#test037
Comment 18 Maxime Daniel CLA 2006-01-04 11:10:15 EST
Entered bug 122635 to ask for the addition of the new option to compiler preferences in the UI.
Comment 19 Maxime Daniel CLA 2006-01-04 11:13:21 EST
Created attachment 32461 [details]
Committed changes (or very close to it)
Comment 20 Martin Aeschlimann CLA 2006-01-04 11:28:01 EST
Sorry, I'm a bit late. But do we really need this new option? What's wrong with assigning a parameter? And why is setting a parameter to final too much work?
(you can vote for bug 108558 if you want us to automatically make parameters final).

In the end every new compiler warning adds complexity, in the compiler and in the UI.
Comment 21 Maxime Daniel CLA 2006-01-05 06:00:16 EST
Philippe asks that the warning token be suppressed (too specific - would prefer some 'code-style' generic approach).
Comment 22 Maxime Daniel CLA 2006-01-05 10:27:11 EST
Clarification:
- we keep the batch compiler warn option (aka -warn:+paramAssign);
- we remove the warning token (aka @SuppressWarning(paramAssign)).
Comment 23 Maxime Daniel CLA 2006-01-09 11:31:37 EST
Created attachment 32684 [details]
HEAD - suppressed warning token and improved build notes
Comment 24 Maxime Daniel CLA 2006-01-09 11:33:18 EST
Suppressed warning token, improved build notes, added JavaCore constant declaration and improved JavaCore#getDefaultOptions comment.
Comment 25 Jerome Lanneluc CLA 2006-02-14 06:27:51 EST
Verified for 3.2 M5 using build I20060214-0010