Bug 195757 - Missing redundant null check in if condition through ternary operator
Summary: Missing redundant null check in if condition through ternary operator
Status: REOPENED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.3   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT Core Triaged CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords: helpwanted
Depends on:
Blocks:
 
Reported: 2007-07-08 07:20 EDT by NoName CLA
Modified: 2023-05-18 09:00 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description NoName CLA 2007-07-08 07:20:13 EDT
Build ID: I20070625-1500

Steps To Reproduce:
If you type in a java file:

public class X {

  boolean test() {
    return true;
  }

  int foo() {
    if ((test() ? null : null) == null)
      return 1;
    return 0;
  }

  // more complex

  int bar() {
    String s = null;
    if ((test() ? s : null) == null)
      return 1;
    return 0;
  }
}

In both case, a "redundant null check" should be reported.


More information:
Comment 1 Maxime Daniel CLA 2007-07-09 02:50:04 EDT
While the test cases are different (and should be used for additional regression tests), the cure would be the same as for bug 133125, namely compute and use the null status of the ternary operator in due time. Hence I will consider this bug as a duplicate (and we'll leverage the test cases as needed if/when we go after bug 133125).

*** This bug has been marked as a duplicate of bug 133125 ***
Comment 2 Eclipse Webmaster CLA 2007-07-29 09:23:21 EDT
Changing OS from Mac OS to Mac OS X as per bug 185991
Comment 3 Satyam Kandula CLA 2010-09-14 07:40:12 EDT
The test case given here is not fixed by bug 133125. Hence reopening this.
Comment 4 Stephan Herrmann CLA 2010-09-14 08:45:53 EDT
(In reply to comment #3)
> The test case given here is not fixed by bug 133125. Hence reopening this.

While part of bug 133125 has moved to bug 324178, which is still open,
the test cases from this bug are different as analysis of the condition 
is not required here.

To start narrow down the issue, consider the following variant, where
the ternaries have been extracted into local variables:

public class Bug_195757 {
  boolean test() {
    return true;
  }

  int foo() {
    Object ternary1 = test() ? null : null;
    if (ternary1 == null)
      return 1;
    return 0;
  }

  // more complex

  int bar() {
    String s = null;
    String ternary2 = test() ? s : null;
    if (ternary2 == null)
      return 1;
    return 0;
  }
}

Already in 3.6.0 this produces the expected warnings:

Description	Resource	Path	Location	Type
Dead code	Bug_195757.java	/Bug/src	line 11	Java Problem
Dead code	Bug_195757.java	/Bug/src	line 21	Java Problem
Redundant null check: The variable ternary1 can only be null at this location	Bug_195757.java	/Bug/src	line 9	Java Problem
Redundant null check: The variable ternary2 can only be null at this location	Bug_195757.java	/Bug/src	line 19	Java Problem

This seems to support Maxime's analysis that this is a matter of when the
required information is available.
Comment 5 Eclipse Genie CLA 2019-01-28 16:19:04 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 6 Stephan Herrmann CLA 2019-01-31 14:52:14 EST
still relevant, but not on our plan, currently.
Comment 7 Eclipse Genie CLA 2021-01-21 15:56:14 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 8 Eclipse Genie CLA 2023-05-18 09:00:00 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.