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

FII_USE_FUNCTION_IDENTITY false positive #471

Open
nmatt opened this issue Apr 2, 2024 · 2 comments
Open

FII_USE_FUNCTION_IDENTITY false positive #471

nmatt opened this issue Apr 2, 2024 · 2 comments

Comments

@nmatt
Copy link

nmatt commented Apr 2, 2024

FII_USE_FUNCTION_IDENTITY is shown on the STRING constant in the following example:

interface Parser<T>
{
    T parse(String value);

    static final Parser<String> STRING = s -> s;
}

Although specifying

    static final Parser<String> STRING = Function.<String> identity()::apply;

would work, I'd argue that this doesn't make sense here.

@ThrawnCA
Copy link
Contributor

ThrawnCA commented Apr 5, 2024

I'd argue that this doesn't make sense here.

It would create one less object, so...present your argument, I guess?

@nmatt
Copy link
Author

nmatt commented Apr 6, 2024

@ThrawnCA: In both cases, an instance of the (super)class Parser has to be created. So I don't understand why there would be a difference in the number of objects created.

FII_USE_FUNCTION_IDENTITY makes sense for cases like:

    static final Function<String, String> STRING = Function.identity();

i.e. where the existing Function object can be assigned as-is.

To illustrate, the following prints "4":

    Parser<String> a = s -> s;
    Parser<String> b = Function.<String> identity()::apply;
    Function<String, String> c = Function.identity();
    Function<String, String> d = Function.<String> identity()::apply;
    System.out.println(new HashSet<>(asList(a, b, c, d)).size());

This also means that even in d a separate object is created, due to the ::apply indirection. This indirection is necessary in the Parser case, because Function is not assignable to Parser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants