[Java:S3516] Need to ignore, if overriding method

If I implement an interface method or override a method, that returns a value, rule S3516 should not hit due to “always the same value”.

Consider this code:

public interface I
{
    /**
     * @return result object or null, if nothing produced.
     */
    Object doSomethingAndReturnResultObject();
}

public class C implements I
{
    @Override
    public Object doSomethingAndReturnResultObject()
    {
        // No result object in this implementation.
        
        if ( exitCondition )
            return null; // FP
        
        doSomethingProductive();
        
        return null; // FP
    }
}

I see, that in most cases always the same value is a good chance for wrong code. But as you can see, if an interface is implemented, this is not always true.

Maybe there’s no solution, but to place NOSONARs or to disable the rule. But these are my 5 cents.

I see your point, but I don’t think we should ignore overridden methods from interface, we will miss true positives for something that seems to hardly ever happen.

I don’t see something smart to do here from a static point of view, I think it’s acceptable to keep it and close it as won’t fix.

I already tried to place some NOSONARs at the return calls. But that doesn’t keep Qube from reporting the issue. Can I do that via NOSONAR? I don’t like any non code driven shut up.

Another way to handle this would be to either scan javadoc @return for tokens like “or null”, “or null” or somethung like that to know, that null is a valid return value. Or place a sonar marker at the method signature to tell.

I guess, that “always the same result” issue is actually all about null, is it? Because there’s really nothing wrong, if the method returns always the same result.

Can I do that via NOSONAR?

I would rather go for @SuppressWarnings("java:S3516"), if you really want to suppress the issue in the code.

I guess, that “always the same result” issue is actually all about null, is it? Because there’s really nothing wrong, if the method returns always the same result.

I don’t think it’s all about null, to me it seems that any method with any return type always returning the same value is the sign of something fishy happening, and I’m glad to be warned when it happens.

Ok, didn’t know about that possibility. I will try it. Thanks.

I tried @SuppressWarnings as you said. Unfortunately it didn’t do anything.

Additionally Eclipse complains about
Unsupported @SuppressWarnings(“java:S3516”)

Did I do anything wrong? Do I have to activate something?

Not sure what went wrong, did you use java.lang.@SuppressWarnings?

I tried the code you initially posted and just added the annotation, and it removed the issue as expected.

  @SuppressWarnings("java:S3516")
  public Object doSomethingAndReturnResultObject() {

Yes, I did.

S3516

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.