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

findbugs:NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE does not consider null validation #651

Closed
alixwar opened this issue Jun 5, 2018 · 12 comments

Comments

@alixwar
Copy link

alixwar commented Jun 5, 2018

Problem: A violation is triggered at the last line, saying that args may be null. This is clearly validated the line before (using org.apache.commons.lang3.Validate)

Affected version: 3.1.2 (SpotBugs)
Seen in: SonarQube (FindBugs plugin 3.7)

Reproducible code example (from an Android app):

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   final Bundle args = getArguments();
   Validate.notNull(args);
   email = args.getString(ARG_EMAIL);
}
@npfistner
Copy link

Problem: Also if validated using java assert spotbugs still complains NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE

Affected version: 3.1.5 (SpotBugs)

        assert entity.getId() != null;
        final long id = entity.getId().longValue();

@ThrawnCA
Copy link
Contributor

I'm not sure that the assert keyword should be relied on, since assertions can be disabled. But the Commons Lang one is likely worth including; thanks for pointing it out.

@marcelmore
Copy link

Problem: Same problem with Google Guava

Namely on the two checks:

com.google.common.base.Verify.verifyNotNull
com.google.common.base.Preconditions.checkNotNull

Seen in: Gradle build with SpotBugs plugin:

id "com.github.spotbugs" version "1.6.9"

Affected Version: 3.1.12

@alixwar
Copy link
Author

alixwar commented Jun 10, 2019

Same issue with java.util.Objects.requireNonNull()

@nmatt
Copy link

nmatt commented Jul 30, 2019

+1 for supporting requireNonNull().

@gmbett
Copy link

gmbett commented Jul 31, 2019

Same issue with custom method that is annotated with @contract("null -> false")

@alixwar
Copy link
Author

alixwar commented Aug 7, 2019

I'm trying to add a reproducer in the SpotBugs project but I keep getting:

The following classes needed for analysis were missing:
  org.apache.commons.lang.Validate

even after adding the dependency to the spotbugs-tests module 😕

@gmbett
Copy link

gmbett commented Jan 10, 2020

Any updates on this issue?

@victornoel
Copy link

@ThrawnCA any news on that?

@C-Otto
Copy link

C-Otto commented May 1, 2020

There's a database of hardcoded method parameter annotations in https://github.com/spotbugs/spotbugs/blob/master/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/DefaultNullnessAnnotations.java#L406

At the given line, the first argument of Preconditions.checkNotNull is assumed to be "non-null". As such, the analysis complains if null is passed to checkNotNull.

The same holds true for other implementations, for example Objects.requireNonNull.

@ByeBug
Copy link

ByeBug commented Dec 19, 2022

any news on that and #456 ?

prdoyle added a commit to prdoyle/bosk-old that referenced this issue May 29, 2023
SpotBugs has a bug: spotbugs/spotbugs#651

IntelliJ has better built-in null warnings anyway.
prdoyle added a commit to prdoyle/bosk-old that referenced this issue Jun 1, 2023
SpotBugs has a bug: spotbugs/spotbugs#651

IntelliJ has better built-in null warnings anyway.
prdoyle added a commit to prdoyle/bosk that referenced this issue Jun 1, 2023
SpotBugs has a bug: spotbugs/spotbugs#651

IntelliJ has better built-in null warnings anyway.
prdoyle added a commit to prdoyle/bosk that referenced this issue Jun 1, 2023
SpotBugs has a bug: spotbugs/spotbugs#651

IntelliJ has better built-in null warnings anyway.
@hazendaz
Copy link
Member

Fixed by #2709

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