Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] Issue with type inference of nested lambdas #5324

Closed
oowekyala opened this issue Nov 13, 2024 · 0 comments · Fixed by #5325
Closed

[java] Issue with type inference of nested lambdas #5324

oowekyala opened this issue Nov 13, 2024 · 0 comments · Fixed by #5325
Labels
a:bug PMD crashes or fails to analyse a file. in:type-resolution Affects the type resolution code
Milestone

Comments

@oowekyala
Copy link
Member

In the following UnusedPrivateMethod triggers, because type inference infers the wrong type for the result of Stream.map. So reduceBooksAndLenderStatusByLender doesn't apply because of argument type mismatch and is flagged as unused. This is a problem with core type resolution and is not specific to that rule.


Complete Maven project: https://github.com/lolo101/pmd-issue

package org.example.unusedPrivateMethod;

import static java.util.Collections.emptySet;

import java.util.*;
import java.util.stream.*;

public class Main {

    public static void main(String[] args) {
        Library library = new Library(emptySet());
        Map<String, Map<String, String>> map = new Main().run(library);
        System.out.println(map);
    }

    private Map<
        String,
        Map<String, String>
        > run(Library library) {
        return library
            .books()
            .stream()
            .map(
                book ->
                    book
                        .lenders()
                        .stream()
                        .collect(
                            Collectors.toMap(
                                Lender::name,
                                lender ->
                                    Map.of(
                                        book.title(),
                                        lender.status()
                                    )
                            )
                        )
            )
            .reduce(this::reduceBooksAndLenderStatusByLender)
            .orElse(null);
    }

    private Map<
        String,
        Map<String, String>
        > reduceBooksAndLenderStatusByLender(
        Map<String, Map<String, String>> previousMap,
        Map<String, Map<String, String>> nextMap
    ) {
        previousMap.putAll(nextMap);
        return previousMap;
    }
}

record Library(Collection<Book> books) {}
record Book(String title, Collection<Lender> lenders) {}
record Lender(String name, String status) {}

Originally posted by @lolo101 in #5184 (comment)

oowekyala added a commit to oowekyala/pmd that referenced this issue Nov 13, 2024
Reported in pmd#5324

I improved the verbose logging output a bit so
some of the changes are not directly relevant.
@oowekyala oowekyala added the in:type-resolution Affects the type resolution code label Nov 13, 2024
oowekyala added a commit to oowekyala/pmd that referenced this issue Nov 13, 2024
Reported in pmd#5324

I improved the verbose logging output a bit so
some of the changes are not directly relevant.
@oowekyala oowekyala added the a:bug PMD crashes or fails to analyse a file. label Nov 13, 2024
@adangel adangel added this to the 7.8.0 milestone Nov 14, 2024
@adangel adangel changed the title [java] Issue with inference of nested lambdas [java] Issue with type inference of nested lambdas Nov 14, 2024
adangel added a commit that referenced this issue Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:bug PMD crashes or fails to analyse a file. in:type-resolution Affects the type resolution code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants