Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 938
Terry Avatar asked Sep 01 '15 07:09

Terry


People also ask

How do I stop a lint warning?

To suppress a lint warning with an annotation, add a @SuppressLint("id") annotation on the class, method or variable declaration closest to the warning instance you want to disable.

How do I get rid of warnings on Android?

Current projectPosition your cursor on the if statement and press Alt + Enter . Then choose Simplify > Disable inspection.

What is suppress lint in Android?

android.annotation.SuppressLint. Indicates that Lint should ignore the specified warnings for the annotated element.

How do you make lint baseline?

If you want to create a new baseline, manually delete the file and run lint again to recreate it. Then, run lint from the IDE (Analyze > Inspect Code) or from the command line as follows. The output prints the location of the lint-baseline. xml file.


3 Answers

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;
}
like image 99
Terry Avatar answered Oct 20 '22 13:10

Terry


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

like image 42
ataulm Avatar answered Oct 20 '22 12:10

ataulm


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

like image 40
epool Avatar answered Oct 20 '22 12:10

epool