Bug 325342 - Add new option for null analysis based on assert result.
Summary: Add new option for null analysis based on assert result.
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.7 M3   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 198044 (view as bug list)
Depends on:
Blocks: 326116
  Show dependency tree
 
Reported: 2010-09-15 08:16 EDT by Olivier Thomann CLA
Modified: 2010-10-26 03:59 EDT (History)
6 users (show)

See Also:
Olivier_Thomann: review+
daniel_megert: review+


Attachments
proposed fix v1.0 + tests (40.82 KB, patch)
2010-09-20 05:58 EDT, Ayushman Jain CLA
no flags Details | Diff
proposed fix v1.1 + regression tests (42.66 KB, patch)
2010-09-23 12:18 EDT, Ayushman Jain CLA
no flags Details | Diff
same fix with suggested changes (37.71 KB, patch)
2010-09-28 08:08 EDT, Ayushman Jain CLA
no flags Details | Diff
patch with doc changes (37.90 KB, patch)
2010-09-29 08:17 EDT, Ayushman Jain CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Olivier Thomann CLA 2010-09-15 08:16:32 EDT
We should add a new option to let the user choose if he/she wants to see the null analysis of the assert statement being used for further diagnosis.
For example:

int foo(String s) {
    assert s != null;
    if (s != null) {
        return s.length();
    }
    return -1;
}

right now we would flag the if condition as a redundant null check.
Comment 1 Ayushman Jain CLA 2010-09-20 05:58:32 EDT
Created attachment 179233 [details]
proposed fix v1.0 + tests

This patch adds the new option
org.eclipse.jdt.core.compiler.problem.suppressNullInfoFromAsserts

It'll be good to discuss where best to put this option on the Java>Compiler>Errors/Warnings page
The option itself is a kind of suboption for 3 different options - redundant null check, null pointer access, and potential null ptr access. 
I would suggest adding this option just after "treat optional errors like fatal compile errors.." which currently appears at the end of the errors/warnings page. Olivier, whats your take on this? Also, can you please review the patch? Thanks!
Comment 2 Olivier Thomann CLA 2010-09-22 10:17:36 EDT
Looks good, but you must also add the new option inside the JavaCore class so that it can be referenced from the UI.
Comment 3 Olivier Thomann CLA 2010-09-22 10:18:16 EDT
The UI will have to be reorganized a little to put all null warnings together.
Comment 4 Olivier Thomann CLA 2010-09-22 10:19:40 EDT
Markus,

I believe this option is required as we got request to make this diagnosis optional.
Comment 5 Ayushman Jain CLA 2010-09-23 12:18:12 EDT
Created attachment 179470 [details]
proposed fix v1.1 + regression tests

same fix with the option added to JavaCore.
Comment 6 Ayushman Jain CLA 2010-09-24 02:32:02 EDT
Released in HEAD for 3.7M3
Comment 7 Dani Megert CLA 2010-09-27 05:37:47 EDT
That we issue a redundant null checks has been reported previously. I think if we're going to fix that then we should also change the default given that assertions are also disabled by default. Also, the other options must get additional Javadoc and mention the impact of the newly added option.
Comment 8 Dani Megert CLA 2010-09-27 05:43:31 EDT
>	 * Compiler option ID: Suppress null related warnings arising because >of assert statements.
>	 * <p>When enabled, the compiler will not flag null warnings on >variables that got marked as maybe or definitely

The Javadoc should talk about "warning or error" or "problem".

Given my previous comment and given that -ea is off by default we should invert the preference and name it
COMPILER_PB_INCLUDE_ASSERT_IN_NULL_ANALYSIS [default: Disabled]
Comment 9 Ayushman Jain CLA 2010-09-27 06:16:26 EDT
(In reply to comment #7)
> That we issue a redundant null checks has been reported previously. I think if
> we're going to fix that then we should also change the default given that
> assertions are also disabled by default. Also, the other options must get
> additional Javadoc and mention the impact of the newly added option.


Its true that asserts are disabled by default, but a code such as:

assert (foo != null);

if (foo != null) {
}

means that the user is maybe just using it as a marker in the code to tell someone reading it that at this point, foo cannot be null. In this kind of scenario, not giving off the null warning on the if statement can be contradictory to what the user may expect. 
Till now, we had always concurred on the strategy to always report null problems arising out of asserts. It is just for some special cases, when the user doesnt want to see this warnings, that we wanted an option to turn them off. So renaming it to COMPILER_PB_INCLUDE_ASSERT_IN_NULL_ANALYSIS [default: Disabled] will reverse the logic behind introducing the new option - which was - to let the user suppress a warning in some cases in which it irks him.

I agree with your point about fixing the javadoc though.
Comment 10 Dani Megert CLA 2010-09-27 06:23:40 EDT
(In reply to comment #9)
> (In reply to comment #7)
> > That we issue a redundant null checks has been reported previously. I think if
> > we're going to fix that then we should also change the default given that
> > assertions are also disabled by default. Also, the other options must get
> > additional Javadoc and mention the impact of the newly added option.
> 
> 
> Its true that asserts are disabled by default, but a code such as:
> 
> assert (foo != null);
> 
> if (foo != null) {
> }
> 
> means that the user is maybe just using it as a marker in the code to tell
> someone reading it that at this point, foo cannot be null. In this kind of
> scenario, not giving off the null warning on the if statement can be
> contradictory to what the user may expect. 
If he wanted to add a comment then add a comment, not an assert. Also, in that case he could enable to also check 'assert'.

> Till now, we had always concurred on the strategy to always report null
> problems arising out of asserts. It is just for some special cases, when the
> user doesnt want to see this warnings,
My point: out of the box we should not see the warning. The default behavior should be close to what the code does by default when executed. Hence the null check is not redundant. Once a user decides he uses asserts and enables them on launching (via -ea), he can also enable it for the 'null' analysis and then clean up his code accordingly.
Comment 11 Olivier Thomann CLA 2010-09-27 14:47:56 EDT
Ayushman, please describe the new constant in JavaCore inside the buildnotes.
Comment 12 Olivier Thomann CLA 2010-09-27 14:50:07 EDT
This will need to be described inside the N&N for 3.7M3 and the doc should also be updated to reflect the new constant (org.eclipse.jdt.doc.isv\guide\jdt_api_options.htm).
Comment 13 Ayushman Jain CLA 2010-09-28 08:08:40 EDT
Created attachment 179730 [details]
same fix with suggested changes

This patch:
-> Modifies javadoc of org.eclipse.jdt.core.JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK, org.eclipse.jdt.core.JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE, org.eclipse.jdt.core.JavaCore.COMPILER_PB_NULL_REFERENCE
to reflect the new option.
-> Changes the name of the new option to org.eclipse.jdt.core.JavaCore.COMPILER_PB_INCLUDE_ASSERTS_IN_NULL_ANALYSIS.
-> Small modifications to the tests since now by default we dont show warnings arising out of asserts.
-> Changes the name of batch compiler option to includeAssertNull
-> Updates the jdt doc guide to include the new option and update the doc for the 3 null warning options.

Dani, can you please have a quick glance to see if the documentation, etc is ok? Thanks!
Comment 14 Dani Megert CLA 2010-09-29 07:14:03 EDT
Initial tests show that the new option seems to work. Here some Javadoc correction:

"errors/warnings": all other options use "error or a warning". I see no reason to introduce a new pattern here. This needs to be fixed in the html doc too.


 * Compiler option ID: Suppress null related errors/warnings arising because of assert statements.
The initial descriptions is inverse compared to the preference name. I would describe what it does when it gets enabled.


 * <p>This diagnostic is further tuned in the presence of assert statements
 *    by the option {@link #COMPILER_PB_INCLUDE_ASSERTS_IN_NULL_ANALYSIS}
This is missing a '.' and a </p> at the end (see also bug 326514). The sentence could be more user friendly, e.g.
Assert statements are ignored unless 
{@link #COMPILER_PB_INCLUDE_ASSERTS_IN_NULL_ANALYSIS} is enabled.
Comment 15 Ayushman Jain CLA 2010-09-29 08:17:05 EDT
Created attachment 179836 [details]
patch with doc changes
Comment 16 Ayushman Jain CLA 2010-09-29 12:44:24 EDT
Released in HEAD for 3.7M3.
Comment 17 Dani Megert CLA 2010-09-30 02:34:37 EDT
*** Bug 198044 has been marked as a duplicate of this bug. ***
Comment 18 Rajesh CLA 2010-10-26 02:53:48 EDT
Verified in I20101025-1800
Comment 19 Srikanth Sankaran CLA 2010-10-26 03:59:45 EDT
Thanks Rajesh. Verified for 3.7 M3 using I20101025-1800.