Thanks for reaching out. I think your problem is coming from the fact that our engine assumes that you can’t pass null to the method trimWhitespaces and thus guarantees that the result is not null; in your case, that’s, of course, not true. However, if you look at the documentation of StringUtils some of the parameters are annotated as Nullable, while others are not. Thus it’s not trivial to detect the right contract between the values.
As a side note:
this method is deprecated in 6.0
IntelliJ IDEA shows the same issue + reports on null as an argument
In fact I’ve simplified the example a bit.
Originally I had someting like
// request of type jakarta.servlet.http.HttpServletRequest
String myParam = request.getParameter("xxx");
String s1 = org.springframework.util.StringUtils.trimWhitespace(input);
But I don’t think it changes much things.
What I don’t understand, is if I create my own class MyOwnStringUtils with the same methods (trimWhitespace() and hasLength()), SonarLint doesn’t show this warning.
And with that version, I have error S2583 (Conditionally executed code should be reachable) :
public static void foo(String input) {
String s = StringUtils.trimWhitespace(input);
if (s == null) {
System.out.println("null");
}
System.out.println(s);
}
I think our SE engine (and IntelliJ Data flow analysis) make the same assumption that the function from StringUtils.trimWhitespace can’t return null. This is probably caused by the fact that it’s argument isn’t marked as nullable. While arguments of other functions in the class could be marked. This causes a mess in understanding what are the nullability constraints.
So what should be done is to suggest Spring FWK team to add annotation @Nullable on that parameter. And maybe also on the method itself as it can return null ?