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

UnnecessaryOptionalGet gives false-positive #2101

Closed
wendigo opened this issue Jan 14, 2021 · 1 comment · Fixed by #2110
Closed

UnnecessaryOptionalGet gives false-positive #2101

wendigo opened this issue Jan 14, 2021 · 1 comment · Fixed by #2110

Comments

@wendigo
Copy link

wendigo commented Jan 14, 2021

Description of the problem / feature request:

UnnecessaryOptionalGet gives a false-positive when two, different instances of the same type holding Optional are used.

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

package test;

import java.util.Optional;

import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Verify.verify;
import static java.util.Objects.requireNonNull;

public class ErrorProneTest
{
    private ErrorProneTest()
    {
    }

    public static class T
    {
        private final String name;

        public T(String name)
        {
            this.name = requireNonNull(name, "name is null");
        }

        public Optional<String> getValue()
        {
            return Optional.of(name);
        }
    }

    public static void main(String[] args)
    {
        T actual = new T("hello");
        T expected = new T("world");

        test(actual, expected);
    }

    private static void test(T actual, T expected)
    {
        checkState(expected.getValue().isPresent(), actual.getValue().isPresent());
        actual.getValue().ifPresent(actualValue -> {
            String expectedValue = expected.getValue().get();
            verify(actualValue.equals(expectedValue), "values are different");
        });
    }
}

Gives false-positive:

ErrorProneTest.java:[54,36] [UnnecessaryOptionalGet] This code can be simplified by directly using the lambda parameters instead of calling get..() on optional.
(see https://errorprone.info/bugpattern/UnnecessaryOptionalGet)
Did you mean 'String expectedValue = actualValue;'?

which is not correct obviously.

What version of Error Prone are you using?

2.5.0

Have you found anything relevant by searching the web?

Nope :)

copybara-service bot pushed a commit that referenced this issue Jan 14, 2021
…entical

don't assume that a particular receiver symbol already corresponds to the
same `Optional`, which results in false positives when the same method is
called on two different instances.

Fixes #2101

PiperOrigin-RevId: 351873201
copybara-service bot pushed a commit that referenced this issue Jan 14, 2021
…entical

don't assume that a particular receiver symbol already corresponds to the
same `Optional`, which results in false positives when the same method is
called on two different instances.

Fixes #2101

PiperOrigin-RevId: 351873201
copybara-service bot pushed a commit that referenced this issue Jan 14, 2021
…entical

don't assume that a particular receiver symbol already corresponds to the
same `Optional`, which results in false positives when the same method is
called on two different instances.

Fixes #2101

PiperOrigin-RevId: 351873201
@findepi
Copy link
Contributor

findepi commented Jan 15, 2021

Thanks @cushon that was quick!

stevie400 pushed a commit to HubSpot/error-prone that referenced this issue Jan 15, 2021
…entical

don't assume that a particular receiver symbol already corresponds to the
same `Optional`, which results in false positives when the same method is
called on two different instances.

Fixes google#2101

PiperOrigin-RevId: 351898477
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants