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.
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; }
Created attachment 153470 [details] Only one hint available for the unnecessary assignment
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.
(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.
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.