Skip to content

US_USELESS_SUPPRESSION_ON_METHOD broken when used with IT_NO_SUCH_ELEMENT #3351

@uhafner

Description

@uhafner

As mentioned in #3307 (comment) the new useless suppressions finder does not always work correctly.

When you use SpotBugs for the following class

package edu.hm.hafner.util;

import java.util.Iterator;
import java.util.NoSuchElementException;

class DummyIterator implements Iterator<Integer> {
    private int value;

    DummyIterator(final int number) {
        value = number;
    }

    @Override
    public boolean hasNext() {
        return false;
    }

    @Override
    @SuppressFBWarnings(value = "IT_NO_SUCH_ELEMENT", justification = "thrown in ex()")
    public Integer next() {
        ex();

        return value++;
    }

    private void ex() throws NoSuchElementException {
        if (value > 100) {
            throw new NoSuchElementException("Nothing");
        }
    }
}

it detects the following warnings:

[ERROR] Medium: Suppressing annotation on the method edu.hm.hafner.util.DummyIterator.next() is unnecessary [edu.hm.hafner.util.DummyIterator] At DummyIterator.java:[line 8] US_USELESS_SUPPRESSION_ON_METHOD

The interesting thing is that SpotBugs logs that the annotation should be on line 8 but there is no such annotation.

When I remove the annotation then SpotBugs finds the warning:

[ERROR] Low: edu.hm.hafner.util.DummyIterator.next() cannot throw NoSuchElementException [edu.hm.hafner.util.DummyIterator] At DummyIterator.java:[lines 20-22] IT_NO_SUCH_ELEMENT

So I always get a warning which breaks my CI builds.

As a side node: actually the detector of IT_NO_SUCH_ELEMENT should be a little bit smarter and check the called methods as well but this is a different issue...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions