Bug 165084

Summary: [compiler] "Parameter is never read" could be customized for constructors
Product: [Eclipse Project] JDT Reporter: Christophe Cornu <christophe.cornu+eclipse>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: NEW --- QA Contact:
Severity: enhancement    
Priority: P3 CC: daniel_megert, jerome_lanneluc, markus.kell.r, martinae, philippe_mulet
Version: 3.2   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Christophe Cornu CLA 2006-11-18 16:40:34 EST
Here is a mistake I do too often. Wondering if you will find this case interesting enough to add it to JDT UI.

class MyClass {

    Foo foo;
    Bar bar;
    String msg;

    public MyClass(Foo foo, Bar bar, String msg) {
        this.foo = foo;
        this.arg3 = arg3;
        // more work
    }

I forgot to assign bar to this.bar . This happens in particular when refactoring a class, declaring extra arguments, passing extra arguments and fixing javadoc (till compile errors are gone). Then start running but things still don't work. Then after some debugging, find out the constructor isn't doing its work. With eclipse 3.2 M3 JRE1.5 I don't see any warning inside the constructor.

Suggestion: detect this case and report it. Add the yellow warning decoration under the bar variable in the constructor.

"Warning: Argument bar is not assigned in constructor"
Comment 1 Christophe Cornu CLA 2006-11-18 16:41:52 EST
Typo on arg3, I meant:

class MyClass {

    Foo foo;
    Bar bar;
    String msg;

    public MyClass(Foo foo, Bar bar, String msg) {
        this.foo = foo;
        this.msg = msg;
        // more work
    }
Comment 2 Markus Keller CLA 2006-11-19 12:51:34 EST
A common practise to prevent such bugs is to make these fields final. A warning is problematic, since it will inevitably lead to false positives.
Comment 3 Dani Megert CLA 2006-11-20 03:33:14 EST
You can enable the "Parameter is never read" compiler problem.
Comment 4 Martin Aeschlimann CLA 2006-11-21 04:26:30 EST
Moving to jdt.core to decide.
Comment 5 Philipe Mulet CLA 2006-11-21 08:16:58 EST
Comment 3 is the actual feature you are looking for. 
We could special case constructors though.
Comment 6 Christophe Cornu CLA 2006-11-24 13:57:58 EST
Thanks for suggestion in comment 3.