pitest icon indicating copy to clipboard operation
pitest copied to clipboard

NakedReceiverMutator demands removal of map(MyClass.class::cast) but java compiler requires it

Open romani opened this issue 2 years ago • 0 comments

Detected at https://github.com/checkstyle/checkstyle/pull/13928

code:

    public Set<String> getExternalResourceLocations() {
        return Stream.concat(filters.stream(),
                Stream.concat(ordinaryChecks.stream(), commentChecks.stream()))
            .filter(ExternalResourceHolder.class::isInstance)
            .map(ExternalResourceHolder.class::cast)
            .flatMap(resource -> resource.getExternalResourceLocations().stream())
            .collect(Collectors.toSet());
    }

mutation is: org.pitest.mutationtest.engine.gregor.mutators.experimental.NakedReceiverMutator replaced call to java/util/stream/Stream::map with receiver on line .map(ExternalResourceHolder.class::cast)

Workaround to avoid mutation is usage of old style cast in lambda:

    public Set<String> getExternalResourceLocations() {
        return Stream.concat(filters.stream(),
                Stream.concat(ordinaryChecks.stream(), commentChecks.stream()))
            .filter(ExternalResourceHolder.class::isInstance)
            .flatMap(resource -> {
                return ((ExternalResourceHolder) resource)
                        .getExternalResourceLocations().stream();
            })
            .collect(Collectors.toSet());
    }

but code does not looks good in this case. We do know that pitest operates on bytecode so cast chain method is actually not required especially in code where it is close to impossible to put wrong type in collection.

Is it possible to improve pitest to skip such mutations ?

romani avatar Oct 30 '23 12:10 romani