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);
}
}
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:
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.
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.
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.
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?