Suppressing unchecked warnings creates false positive

  • What language is this for? => Java
  • Which rule? => S1612
  • Why do you believe it’s a false-positive/false-negative? => removing warning suppression for unchecked should not change the evaluation of specified rule in this case.
  • Are you using
    • SonarCloud? => no
    • SonarQube - which version? => Community edition 10.0.0.68432
    • SonarLint - which IDE/version? => no
      • in connected mode with SonarQube or SonarCloud?
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)

No issues:

import java.util.stream.Stream;

public class Main {
    public <T> Stream<T> example(Stream<Object> objects) {
        return objects
                .map(metric -> (T) metric);
    }
}

“Replace this lambda with method reference ‘T.class::cast’.”:

import java.util.stream.Stream;

public class Main {
    @SuppressWarnings("unchecked")
    public <T> Stream<T> example(Stream<Object> objects) {
        return objects
                .map(metric -> (T) metric);
    }
}

“Replace this lambda with method reference ‘T.class::cast’.”:

import java.util.stream.Stream;

public class Main {
    public <T> Stream<T> example(Stream<Object> objects) {
        //noinspection unchecked
        return objects
                .map(metric -> (T) metric);
    }
}

Hi @Geert,

I analyzed the examples that you provided and both Sonar Qube and Sonar Lint are behaving consistently not raising any issues. Please check the following screenshot to make sure I didn’t miss anything:

Don’t hesitate to reach out and provide any information that may help me reproduce the issue you’re experiencing.

I must have done something wrong when recreating the issue.
Right now I can’t really determine the cause of the false positive.
This is the original code raising the issue. This code fragment also raises the issue in isolation.

public <T> Stream<T> example2(Stream<Object> objects) {
        //noinspection unchecked
        final Stream<T> metrics = getTypeTemplates(containerTemplate)
                .map(TypeTemplate::getData)
                .map(data -> data.getData(typeMetricKey))
                .map(metric -> (T) metric);
        return metrics;
    }

Hi @Geert ,

unfortunately, the latest code snipped you provided doesn’t add more context and it doesn’t help in reproducing the eventual false positive. If you can share screenshots from the sonar qube analysis, then I might be able to help you more.

Cheers

After copy only the relevant classes to an empty project, no issues where raised. Yet the original project still raises the issue.
I’m not sure what to try next.

Capture_cut

I have observed some very strange behavior.
Removing an unused formal parameter changed whether the issue was raised.

no_issue_raised

Another instance of strange behavior:

No issues raised in an isolated empty project.
no_issue_raised2

Issue raised in original project.

Hello @Geert,

The only thing I can think of is the presence of an annotation that may interfere with the rule. Can you check if, in your original project, there are annotations at the class level?