Skip to content

NullPointerException when using discard patterns #1168

@MI3Guy

Description

@MI3Guy

This is using NullAway 0.12.4.

If using a switch expression with discard patterns, NullAway fails with a NullPointerException. This can be worked around by changing to something like var _. Interestingly enough, this only occurs when the switch has more than one pattern.

  <snip>/java/nullaway/app/src/main/java/org/example/NullAwayDiscardError.java:10: error: An unhandled exception was thrown by the Error Prone static analysis plugin.
                switch(i) {
                ^
       Please report this at https://github.com/google/error-prone/issues/new and include the following:
    
       error-prone version: 2.36.0
       BugPattern: NullAway
       Stack Trace:
       com.google.common.util.concurrent.UncheckedExecutionException: java.lang.NullPointerException
        at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2087)

Example that reproduces the issue:

package org.example;

public class NullAwayDiscardError {

	public sealed interface IntOrBool {}
	public record WrappedInt(int a) implements IntOrBool {}
	public record WrappedBoolean(boolean b) implements IntOrBool {}

	public static void unwrap(IntOrBool i) {
		switch(i) {
			case WrappedInt(_) -> {}
			case WrappedBoolean(_) -> {}
		}
	}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions