Terry
Terry

Reputation: 14907

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:

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?

Upvotes: 16

Views: 8475

Answers (4)

epool
epool

Reputation: 6819

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

Upvotes: 11

Terry
Terry

Reputation: 14907

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;
}

Upvotes: 26

ataulm
ataulm

Reputation: 15334

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).

Upvotes: 10

Victor
Victor

Reputation: 941

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!

Upvotes: 0

Related Questions