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 210520 - Hint "assigned value not used" appears wrong
Summary: Hint "assigned value not used" appears wrong
Status: RESOLVED FIXED
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 7.1.1
Hardware: PC Windows 7
: P3 normal (vote)
Assignee: Jan Lahoda
URL:
Keywords:
: 212401 (view as bug list)
Depends on:
Blocks:
 
Reported: 2012-04-02 10:39 UTC by tfleischmann
Modified: 2012-11-23 17:36 UTC (History)
2 users (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
Screen shot from NetBeans 7.3 Beta 2 (14.32 KB, image/png)
2012-11-23 00:36 UTC, pacific202
Details

Note You need to log in before you can comment on or make changes to this bug.
Description tfleischmann 2012-04-02 10:39:22 UTC
I have this code:

public void f() {
	boolean test1_ok = false;
	try {
		test1();  // may throw an Exception
		test1_ok = true;   // ***
		test2();  // may throw an Exception
	} catch (Exception e) {
		if (test1_ok) {
			System.out.println("test2 failed");
		} else {
			System.out.println("test1 failed");
		}
	}
}

In line ***, Netbeans shows a hint "The assigned value is never used" which is not true, because it is used in the catch-block.
Comment 1 Jan Lahoda 2012-04-10 16:41:14 UTC
Yes, that is incorrect. Fixed in the development builds:
http://hg.netbeans.org/jet-main/rev/bfc42636e50f
Comment 2 Quality Engineering 2012-04-12 09:57:12 UTC
Integrated into 'main-golden', will be available in build *201204120400* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main-golden/rev/bfc42636e50f
User: Jan Lahoda <jlahoda@netbeans.org>
Log: #210520: correcting handling of thrown and caught exceptions in Flow.
Comment 3 Jan Lahoda 2012-05-14 07:11:00 UTC
*** Bug 212401 has been marked as a duplicate of this bug. ***
Comment 4 pacific202 2012-11-23 00:34:34 UTC
I am having this issue in 7.3 Beta 2.
Comment 5 pacific202 2012-11-23 00:36:53 UTC
Created attachment 128288 [details]
Screen shot from NetBeans 7.3 Beta 2
Comment 6 Jan Lahoda 2012-11-23 08:05:22 UTC
(In reply to comment #4)
> I am having this issue in 7.3 Beta 2.

I apologize, but from the screenshot it is not clear if the warning is valid or not. For example, for code like this:
{
  int s = 0;

  try {
    s = 1;
    System.err.println(s);
  } finally {
  }
}

the warning is valid, as noone can ever read the "0" value from the variable. On the other hand, in a case like this:
{
  int s = 0;

  try {
    s = 1;
  } finally {
    System.err.println(s);
  }
}

the warning would not be valid (as removing the initializer from the variable would lead to an uncompilable source code). But, for me, the warning is not produced in this case.

A standalone test case would help greatly in evaluating the problem.

As a separate matter, I am considering disabling the warning when "null" is assigned into a variable (as that is often used to allow GC of an object), and possibly also when a variable is initialized to a simple value (like null or 0 - not sure about booleans, though). Especially in the latter case, I am not yet convinced it is a correct thing to do, though.
Comment 7 pacific202 2012-11-23 16:22:28 UTC
To directly answer your question, now that I understand the EXACT requirement to get this warning then it was valid.  I was not using startTime in the finally block.

However, the zero was purely an initialization value for a long variable and was never intended to be read again.  I would like to have the option to have this turned off as my style is as follows and this message is shown everwhere.

public String myMethod() {

  [declare all variables here with default values of null/0/false]

  try {

    main code here

  } catch (Exception e) {
    ...
  } finally {
    ...
  }

}

I'll do some research now on best practices for variable declaration outside of the try/catch/finally block and if there's anything definitive I'll post it back to the thread.  I've been doing it this way for so long that I can't remember the reason I do it in the first place!
Comment 8 pacific202 2012-11-23 17:36:07 UTC
Oracle doesn't really say anything about this in their style guide, but Apache does say this:

  5.5 Variables

  44. Variables should be initialized where they are declared and they should be declared in the smallest scope possible.

  48. Variables should be kept alive for as short a time as possible.


I believe that because my objects and methods are defined properly and relatively small in scope by design, it's better to keep all my variable declarations up front where I can view all of the variables in one place.  I don't like having to weave through the code to see where all my variables are.  As such, I don't believe that I am violating either of the two guidelines above, except to the smallest possible (trivial/inconsequential) degree.

I would like to propose that the warning in question be disabled if the following are true:

a) The variable declarations exist at the start of a method and before any code block defined by a "{"

b) The initialization value is null, zero, or false