Bug 135684 - Suggested Compiler Warning - 'Assigned value is discarded'
Summary: Suggested Compiler Warning - 'Assigned value is discarded'
Status: RESOLVED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.3   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-04-07 23:22 EDT by Josh Micich CLA
Modified: 2010-07-07 14:19 EDT (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 Josh Micich CLA 2006-04-07 23:22:22 EDT
The proposed compiler warning I am talking about is very closely related to 'Local variable is never read'.  The main deficiency with that warning is that it only checks that every variable gets read at least once.  It doesn't check if an assigned value is never used.

I have written an algorithm which does precisely that (finds locations of variable assignments that get discarded).  The utility was written in about 55K of plain java code.  As input, it requires the list of variable read & write locations, as well as branch and flow control information.  Currently this is being directly derived from the compiled bytecode.

I'd like to contribute this functionality to Eclipse, but of course that depends on whether it is considered valuable, and also whether there is a straightforward way to fit it into the existing compiler warnings framework.

--  --  --  --
Listed below is an example method showing the sorts of problems this algorithm can identify.  As of version 3.2M5a, Eclipse does not identify any of these.


public static void loopWithComplexMiddleExit() {
    int wellUsed = getAnInt();
    int x = getAnInt();
    int y = getAnInt();
    int z = getAnInt();
    use(z);
    while(true) {
        y = getAnInt();  // warning: value overwritten next line
        y = getAnInt();
        if(pred()) {
            if(pred()) {
                break;
            }
            x = getAnInt(); // ok: this value is used below
        }
        wellUsed++;
        z = getAnInt(); // warning: value never read again
    }
    use(x);
    use(y);
}

In this example, 'getAnInt()' represents any assignment RHS expression. 'pred()' is some boolean condition expression, and 'use()' 'reads' or 'uses' a variable.

Only one variable- 'wellUsed', is well used.  'y' and 'z' are assigned values that get overwritten or discarded.  In contrast, through the same complex flow, 'x' is assigned a value that does have a chance of being subsequently used.
Comment 1 Philipe Mulet CLA 2006-04-10 07:08:06 EDT
Good suggestion, but post 3.2, since we are already feature complete.
Comment 2 Denis Roy CLA 2009-08-30 02:12:56 EDT
As of now 'LATER' and 'REMIND' resolutions are no longer supported.
Please reopen this bug if it is still valid for you.
Comment 3 Olivier Thomann CLA 2010-07-07 14:19:05 EDT
Josh,
Feel free to reopen this one to provide your contribution if you want.
The only condition is that the overhead on the compilation time must be very small.