mbien commented on code in PR #7980:
URL: https://github.com/apache/netbeans/pull/7980#discussion_r1854527767


##########
java/java.hints/src/org/netbeans/modules/java/hints/bugs/NPECheck.java:
##########
@@ -227,13 +227,44 @@ public static ErrorDescription 
unboxingConditional(HintContext ctx) {
     }
     
     
-    @TriggerPattern("switch ($select) { case $cases$; }")
+    @TriggerTreeKind({Kind.SWITCH, Kind.SWITCH_EXPRESSION})
     public static ErrorDescription switchExpression(HintContext ctx) {
-        TreePath select = ctx.getVariables().get("$select");
+        ExpressionTree selector;
+        List<? extends CaseTree> cases;
+        Tree swtch = ctx.getPath().getLeaf();
+
+        switch (swtch.getKind()) {
+            case SWITCH -> {
+                SwitchTree st = (SwitchTree) swtch;
+                selector = st.getExpression();
+                cases = st.getCases();
+            }
+            case SWITCH_EXPRESSION -> {
+                SwitchExpressionTree st = (SwitchExpressionTree) swtch;
+                selector = st.getExpression();
+                cases = st.getCases();
+            }
+            default -> throw new IllegalStateException("Unexpected tree kind: 
" + swtch.getKind());
+        }
+
+        TreePath select = new TreePath(ctx.getPath(), selector);
         TypeMirror m = ctx.getInfo().getTrees().getTypeMirror(select);
         if (m == null || m.getKind() != TypeKind.DECLARED) {
             return null;
         }
+
+        boolean hasNullCase = cases.stream()
+                                   .flatMap(ct -> ct.getLabels().stream())
+                                   .filter(clt -> clt.getKind() == 
Kind.CONSTANT_CASE_LABEL)
+                                   .map(ctl -> ((ConstantCaseLabelTree) 
ctl).getConstantExpression())
+                                   .filter(expr -> expr.getKind() == 
Kind.NULL_LITERAL)
+                                   .findAny()
+                                   .isPresent();

Review Comment:
   nitpick: one of my jackpot rules matched here, shorter version which 
combines the last three stages:
   
   ```java
           boolean hasNullCase = cases.stream()
                                      .flatMap(ct -> ct.getLabels().stream())
                                      .filter(clt -> clt.getKind() == 
Kind.CONSTANT_CASE_LABEL)
                                      .map(ctl -> ((ConstantCaseLabelTree) 
ctl).getConstantExpression())
                                      .anyMatch(expr -> expr.getKind() == 
Kind.NULL_LITERAL);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to