Bug 574905 - ECJ has issues with yielding null in switch expressions in a @Nonnull method
Summary: ECJ has issues with yielding null in switch expressions in a @Nonnull method
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.20   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2021-07-18 08:32 EDT by Marko Zajc CLA
Modified: 2023-07-15 08:39 EDT (History)
2 users (show)

See Also:


Attachments
Two examples of problematic code and two examples of accepted workarounds (1.41 KB, text/x-java)
2021-07-18 08:32 EDT, Marko Zajc CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Marko Zajc CLA 2021-07-18 08:32:14 EDT
Created attachment 286800 [details]
Two examples of problematic code and two examples of accepted workarounds

Overview: 
   If  'Annotation-based null analysis'  is enabled in 'Errors/Warnings', and a switch
   expression  assigned  to  a  field  yields  null in a method with a @Nonnull return
   value (even if the assignment does not impact the return value of the method in any
   way),  the  following  will  be  given  (severity depends on the 'Violation of null
   specification' setting):
      "Null type mismatch: required '@Nonnull <method return type>' but the provided
       value is null"

Steps to reproduce:
   1. Enable 'Annotation-based null analysis' in 'Errors/Warnings'
   2. Ensure 'Violation of null specification' in 'Errors/Warnings' is above 'Ignore'
   3. Create or override[1] a method annotated with @Nonnull
   4. Create a Java 14 (JEP 361) switch expression and assign it to a field (the field
      being assigned to and the value being switched are irrelevant)
         var field = switch(switched) {}
   5. In one of the cases (can also be default, yield null
         case "foo" -> null;
         default -> null;

Actual results:
   The  case  will be marked as "Null type mismatch: required '@Nonnull <method return 
   type>' but the provided value is null"

Expected results:
   The case is not marked unless it is returned/causes the return value to be null.

Build Date & Hardware:
       Build id: 20210612-2011
    JDT version: org.eclipse.jdt.feature.group=3.18.800.v20210611-1600
         System: Debian Bullseye/testing
        JRE/JDK: OpenJDK Runtime Environment AdoptOpenJDK-16.0.1+9

Attached  is  a  sample of erroneous code, and workarounds that effectively accomplish
the same thing.

[1] 'Inherit null annotations'  in  'Errors/Warnings'  must also be enabled in case of
    overriding
Comment 1 Eclipse Genie CLA 2023-07-12 14:50:37 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.
Comment 2 Marko Zajc CLA 2023-07-12 15:00:57 EDT
This problem still occurs in 2023-03 (4.27.0)
Comment 3 Marko Zajc CLA 2023-07-15 08:39:48 EDT
Ditto for 2023-06 (4.28.0)