How to follow SonarLint rule "parameters should be base types (java:S3242)" without breaking code

  • Operating system: win10
  • SonarLint plugin version: v4.2.2
  • Programming language you’re coding in: Java
  • Is connected mode used: no

As shown in the picture, SonarLint suggests me to change JsonNode to use its interface TreeNode; however, if I do so, I need to change the method body to return (JsonNode) params.get(0); and add type checking.

Screenshot 2 provides a more general way to reproduce the problem.

My questions are:

  1. Is there a way to improve my code with minimal cost to avoid the warning?
  2. Or is this a scenario suitable for SuppressWarnings?
  3. Is it possible that java:S3242 will change its judgment logic in the future?

Hello there

I could not reproduce the issue with SonarLint 10.2.1 on this snippet:

package org.example;

import com.fasterxml.jackson.databind.JsonNode;

public class App1 {
  public JsonNode getFirst(JsonNode params){
    return params.get(0);
  }
}

You are using SonarLint version 4.2.2 on VSCode?

demo.zip (181.2 KB)

Thank you for your attention to this post. I upload a demo project in the zip file.

1 Like

Thank you, I managed to reproduce it, and I consider it a false positive.
You should not be forced by a rule to change the logic of your code when it makes sense.
In my opinion, the rule here is failing to understand that whatever your method will return, is going to be the same type as the parameter that you pass to it. So changing the parameter’s type will affect the return type of the method or force you to add logic to ensure you still return the correct type.

I created a ticket to fix this behavior
https://sonarsource.atlassian.net/browse/SONARJAVA-4835