This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 252172 - Missing hint to remove unused assignment
Summary: Missing hint to remove unused assignment
Status: REOPENED
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.1
Hardware: PC Windows 7
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-04-30 06:55 UTC by cezariusz
Modified: 2015-10-02 14:44 UTC (History)
0 users

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments
Only one hint available for the unnecessary assignment (6.26 KB, image/png)
2015-04-30 06:57 UTC, cezariusz
Details

Note You need to log in before you can comment on or make changes to this bug.
Description cezariusz 2015-04-30 06:55:28 UTC
Product Version: NetBeans IDE Dev (Build 201504280001)
Java: 1.8.0_45; Java HotSpot(TM) 64-Bit Server VM 25.45-b02
Runtime: Java(TM) SE Runtime Environment 1.8.0_45-b14
System: Windows 7 version 6.1 running on amd64; UTF-8; pl_PL (nb)

There is a warning "The assigned value is never used", but there is no hint to remove the unnecessary assignment.

Test case:

    public String unused(boolean xyx) {
        String s = "abc";
        if (xyx) {
            s = "xyx";
        } else {
            s = "cde";
        }
        return s;
    }

The first assignment to "s" gives the warning, and hint offers only to "Configure Unused Assignment Hint". It should also offer to remove the assignment, leading to the following result:

    public String unused(boolean xyx) {
        String s;
        if (xyx) {
            s = "xyx";
        } else {
            s = "cde";
        }
        return s;
    }
Comment 1 cezariusz 2015-04-30 06:57:14 UTC
Created attachment 153470 [details]
Only one hint available for the unnecessary assignment
Comment 2 Svata Dedic 2015-05-12 15:52:55 UTC
I don't think that removing code need to be automated this way; deleting a portion of text is trivial - and it's rather unlikely that the code is actually useless, but rather some code path which should (was intended to) use the value does not.
Comment 3 cezariusz 2015-05-14 05:45:22 UTC
(In reply to Svata Dedic from comment #2)
> I don't think that removing code need to be automated this way; deleting a
> portion of text is trivial - and it's rather unlikely that the code is
> actually useless, but rather some code path which should (was intended to)
> use the value does not.

I do not agree with INVALID.
1. It is a warning with an exclamation mark. I don't want to have a yellow status of the file and I want the IDE to help me with that.
2. It's trivial, but requires several keystrokes (End, Left, 5 x Shift+Ctrl+Left, Backspace). Alt-Enter, Enter would be much faster.
3. There are hints for even more trivial actions like removing an unused import, though it's just a Ctrl+D to delete a line.
4. It's very often a useless code, because programmers assign a null value "just in case". A common example:

    public String unused() throws SQLException {
        Connection connection = null;
        String value = null;
        try {
            connection = getCommonConnection();
            value = getDatabaseName(connection);
        } catch (SQLException e) {
            logError(e);
            resetCommonConnection();
            throw e;
        }
        return value;
    }

BTW: The "value = null" assignment is not detected as unused. Should I report a bug for it.
Comment 4 Svata Dedic 2015-05-14 06:51:02 UTC
Whatever. In that case, let's make it an enhancement.

Hm, null assignment COULD be important, sometimes, even though the value is not used: to clear a reference which would otherwise prevent GC of an object (consider call to a blocking method). Optimizing compiler would limit the scope of the variable, but there's no guarantee such optimization happens at runtime.

But not in the case in your example: in variable initializer - this one should be reported. Specifically in this case I would agree with an automated action: the expression has no potential side effects and the result is not used.