FP for java:S2583 with conditions involving Micronaut StringUtils.trimToNull

SonarLint with IntelliJ 2021.3.3 (build #IC-213.7172.25) and SonarQube Developer Edition Version 8.9.6 (build 50800).

package com.example;

import io.micronaut.core.util.StringUtils;

public class SonarLintBug {

  private final String internalValue;

  public SonarLintBug(String value) {
    if (StringUtils.trimToNull(value) == null) { // reports java:S2583 against this condition
      internalValue = "";
    else {
      internalValue = value;

SonarLint analysis reports: Change this condition so that it does not always evaluate to “false”, which means SonarLint believes that trimToNull() can never return null.

This is a false positive because the trimToNull() method will return null if the input is blank/empty/null - here’s the source:

     * Trims the supplied string. If the string is empty or null before or after
     * trimming, null is returned.
     * @param string the string to trim
     * @return the trimmed string or null
    public static String trimToNull(@Nullable String string) {
        return Optional.ofNullable(string)

Hello @kdgrant ,

Sorry for getting back to you on this so late. I can not reproduce the issue on my side. It seems to me that micronaut trimToNull() method is using micronaut’s @Nullable annotation, itself using the JSR-305 meta-annotation @Nonnull(when = When.MAYBE). This use case should be well supported by our engine.

Can you tell me if you still suffer from the same FP?


Hi Michael,

Sorry, I left the company where I was working on this Micronaut code some time ago.


1 Like