Android Studio: suppress lint warning for if statement

I have a this code somewhere in my Android project:

public boolean isLoadInProgress(boolean privateLoad, boolean publicLoad) {
    if (privateLoad && privateLoadInProgress) {
        return true;
    }
    if (publicLoad && publicLoadInProgress) {
        return true;
    }
    return false;
}

I get a lint warning at the second if statement: ‘if’ statement could be simplified. That’s obviously because I could write as well:

  • Working with ViewPager and Fragments
  • How to show imageView full screen on imageView click?
  • AutoCompleteTextView backed by CursorLoader
  • Using attribute resources (?attr/) in layout binding?
  • How to dismiss keyboard in Android SearchView?
  • RatingBar custom Vector Drawables superimposing
  • return publicLoad && publicLoadInProgress;
    

    However, I would like to keep it this way for readability. I know that there is some inline comment annotation for switching off the lint warning at that place, but I can’t find it in the Android Lint documentation. Can you tell me what this annotation/comment was?

  • What is the use of <permission-group> in android?
  • Android : how to open a mail composer view?
  • How to make android EditText smaller than default in height?
  • Drawing Nine Patch onto Canvas (Android)
  • Android Studio is not generating zip aligned apk
  • Genymotion unable to load VirtualBox engine on Windows 10
  • 4 Solutions collect form web for “Android Studio: suppress lint warning for if statement”

    The simple code comment for disabling the warning is:

    //noinspection SimplifiableIfStatement
    

    This on top of the if-statement should switch off the warning only at that place.

    In the example, this would be:

    public boolean isLoadInProgress(boolean privateLoad, boolean publicLoad) {
        if (privateLoad && privateLoadInProgress) {
            return true;
        }
    
        //noinspection SimplifiableIfStatement
        if (publicLoad && publicLoadInProgress) {
            return true;
        }
        return false;
    }
    

    You can add @SuppressWarnings("SimplifiableIfStatement") above your method.

    It’s not an Android Lint error. You can use:

    @SuppressWarnings("RedundantIfStatement")
    public static boolean isLoadInProgress(boolean privateLoad, boolean publicLoad) {
        if (privateLoad && privateLoadInProgress) {
            return true;
        }
        if (publicLoad && publicLoadInProgress) {
            return true;
        }
        return false;
    }
    

    At the highlighted if, you can use the alt-enter shortcut to open the context menu and select Simplify > Suppress for method (keeping the scope as small as possible).

    Sure:

    In .java files, you can suppress issues with the @SuppressLint
    annotations. You supply the lint issue id as the argument to the
    annotations.

    Example:

    @SuppressLint("AndroidWarningId")
    public boolean isLoadInProgress(boolean privateLoad, boolean publicLoad) {
        if (privateLoad && privateLoadInProgress) {
            return true;
        }
        if (publicLoad && publicLoadInProgress) {
            return true;
        }
        return false;
    }
    

    Just replace the AndroidWarningId with the corresponding warning, you can find those in here

    Although I would suggest simplifying it this way:

    public boolean isLoadInProgress(boolean privateLoad, boolean publicLoad) {
        if (privateLoad && privateLoadInProgress
         || publicLoad && publicLoadInProgress) {
            return true;
        }
    
        return false;
    }
    

    Its still readable and uses less space (kind of ugly though, but better than a supresslint).

    You can also suppress more than one issue using a comma separated list:

    @SuppressLint({“NewApi”,”StringFormatInvalid”})

    Cheers!

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.