redhat-developer / redhat-developer/vscode-java

Return null through ternary operator does not give Type mismatch error

Open
#436 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug compile upstream
Dominant language
TypeScript
Stars
2.3k
Forks
546
Avg merge
20h 1m
Merged PRs (30d)
11

Description

Returning null through a ternary operator does not give Type mismatch error. It should.

Code 1:

public double getValue() {
    return null;
}

null is error-underlined with the error [Java] Type mismatch: cannot convert from null to double.
image

Code 2:

public double getValue() {
    return value == null ? null : Double.parseDouble(value);
}

No errors shown on the returned null.
image

Code 3:

public double getValue() {
    if (value == null)
        return null;
    else
        return Double.parseDouble(value);
}

Same error:
image

Environment
  • Operating System: Windows 10 x64
  • JDK version: 8
  • Visual Studio Code version: 1.20.0
  • Java extension version: 0.18.1
Steps To Reproduce
  1. Make code shown.
  2. Look for error.
Current Result

Code 1 & 3 shows error. Code 2 doesn't.

Expected Result

All Code blocks should show error.

Additional Information

Please let me know if you need any logs or anything.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing Code 1, Code 2, and Code 3 in the Java extension and compare the diagnostics for the ternary and if/else forms. Trace how the Java language support analyzes return expressions, then verify that the ternary case reports the same type mismatch as the other examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers, devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.