FP S3516 Methods should not be invariant

  • What language is this for? Java
  • Which rule? S3516
  • Why do you believe it’s a false-positive/false-negative? ‘id’ is not invariant because is comes from the call to super
  • SonarQube for IDE - which IDE/version? Eclipse 2025-06, Sonar 11.13
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
Resource Date Description
FPTestS3516.java 7 minutes ago Refactor this method to not always return the same value. [+4 locations]
class Parent {
	String getName(String input) {
		return input.equals("one") ? "A" :"B";
	}
}

public class FPTestS3516 extends Parent {

	@Override
	public String getName(String input) {
		String id = super.getName(input);
		
		if (id == null || id.isEmpty()) {
			System.out.println("Name is not set, returning default value.");
			return id;
		}
		
		if (id.length() < 5) {
			System.out.println("Name is too short, returning default value.");
			return id;
		}
		if (id.length() > 20) {
			System.out.println("Name is too long, returning default value.");
			return id;
		}
		System.out.println("Name is set to: " + id);
		return id;
	}

	public static void main(String[] args) {
		FPTestS3516 test = new FPTestS3516();
		System.out.println(test.getName(args[0]));
	}
}

Hi,

If you remove the System.out.println calls in you method it does always return id. The value of id is never changed during the execution. No “default value” is ever returned.

Does the following code produce the same error ?

@Override
	public String getName(String input) {
		String id = super.getName(input);
		
		if (id == null || id.isEmpty()) {
			System.out.println("Name is not set, returning default value.");
		} else if (id.length() < 5) {
			System.out.println("Name is too short, returning default value.");
		} else if (id.length() > 20) {
			System.out.println("Name is too long, returning default value.");
		} else {
		        System.out.println("Name is set to: " + id);
                }

		return id;
	}

I do not think this is a FP as your method does always return id as a result. Despite your code suggesting that it should in certain conditions return a different value.

Daniel

1 Like

Since the super method does return different values, then this method also returns different values. The code example is not real code it is an example (mostly generated by AI) to highlight the FP.

Yes I understand that the value from the super method will have different values. However, this method always returns the value from the super method. This method has multiple returns but they all return the same value (id from the super method). What is the point of a separate return statement if it returns the same value as elsewhere?

The entire method has no effect on the value of id, it always has the value from the super method. So it should probably only have a single return.

In general, if a method has multiple returns it is likely to return different values. That is not the case here. Therefore, SonarQube suspects there may be an error in the programming of this method. And I would expect SonarQube to bring this to my attention if this was in my code.

The method may do exactly what you intend. But it is still unusual. Which, to me, does not signify that this is a false positive.

1 Like

Hi @dandoy,

I tend to agree with @nelkahn here. All possible paths of the method under analysis return the same value. Transforming it to have single return statement also makes it easier to maintain in the future. Feel free to mark this specific issue as a false positive and move on.

But if you have another example of an FP on the same rule that you would like to share, feel free to do so in this thread or a new one.

Cheers,

Dorian