Menu

#1423 Findbugs fails to detect passing nullable field to nonnull in loops

3.x
closed-rejected
nobody
None
5
2017-10-22
2015-10-13
No
@Nonnull
public static <T extends Enum<?>> List<String> enumToList(
        @CheckForNull final Object context,
        @Nonnull final String label,
        @Nonnull final List<Object> items) {

        final List<String> values = new ArrayList<>(items.size());
        for (final Object item : items) {
            values.add(getMagic(context, label, item));
        }
        return values;
    }
@Nonnull
    public static String getMagic(
        @Nonnull final Context context,
        @CheckForNull final String label,
        @CheckForNull final Enum<?> item) {
        /// magic happens.
}

I encountred the above issue with my current project.
This should be detectable however it seems to never catch that context could be null before it gets passed to getMagic as a nonnull paramter.

Discussion

  • Tagir Valeev

    Tagir Valeev - 2015-12-26

    Likely the same as [bugs:#1351]: when multiple parameters are annotated with Nonnull/CheckForNull, FindBugs may fail to track them. Probably the problem would persist even without the loop.

     

    Related

    Bugs: #1351

  • Jonathan W Schneider

    Possibly,
    However I did try it with an initilal call to getMagic before the loop and it complained there.

    But as to the related bug... the fact that it is allowed to produce false positives makes this check useless to me. There are alternatives out there that do a better job.

    Thanks

     
  • Andrey Loskutov

    Andrey Loskutov - 2017-10-22
    • Status: open --> closed-rejected
     

Log in to post a comment.