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:

  • Android, selecting a Cipher suite
  • Bluetooth connection between Android and another phone over the Handsfree profile
  • onConnectionFailed giving SIGN_IN_REQUIRED(4)
  • Couldn't find ionic.config.json file
  • “Android library projects cannot be launched”?
  • Android: adb: copy file to /system (Permission denied)
  • 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?

    Related posts:

    What is the difference between List and ArrayList?
    Android: can I use Google Analytics inside a Service?
    What is RestrictionPolicy and how do I make it shut up?
    Android 4.4 Print to PDF without User Involvement
    android: How to work with CoordinatorLayout and translucent navigation bar?
    Facebook SDK for Android - set Application ID programmatically
  • Storing RSA Private Key Android
  • runOnUiThread(new Runnable(){}) is undefined for the type new Thread(){}
  • Activity class does not exist
  • Retrieve data from website in android app
  • Android.mk - build all source file in a directory
  • Fb app invite with referral code
  • 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.