Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio: disable warning "if statement can be simplified"

Android studio marks warnings in Java code, some of them I consider useless and want to disable them. I know I can configure Inspections that are enabled, but for some of these I can't find where it can be disabled. Then code is marked to have issues, and I want to have clean code so that I see real problems. Example of warning:

'if' statement can be replaced with 'return ...'

enter image description here

And I don't want to put annotations to my code, rather I'd like to switch this off in IDE. Thanks

like image 939
Pointer Null Avatar asked May 11 '16 12:05

Pointer Null


2 Answers

When you click on the lightbulb and then on the suggested action's arrow, you get submenu with options. First one should be "Edit inspection profile setting", which should navigate you to the exact place in Settings, where you can edit given inspection.


enter image description here

like image 190
Vojtech Ruzicka Avatar answered Oct 20 '22 13:10

Vojtech Ruzicka


This is a more general answer for future viewers. These are various ways to suppress the warning.

Statement

Add the following comment above the line with the if statement.

//noinspection SimplifiableIfStatement
if (...)

Method

Add the following line at the beginning of the method.

@SuppressWarnings("SimplifiableIfStatement")
private boolean myIfMethod() {
    if (...) return false;
    return (...);
}

Class

Add the following line at the beginning of the class.

@SuppressWarnings("SimplifiableIfStatement")
public class MyClass { ... }

Current project

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

enter image description here

All projects

Position your cursor on the if statement and press Alt + Enter. Then choose

enter image description here

Reapplying the inspection

If you disabled the inspection by mistake, you can turn it on again.

  1. Go to File > Settings > Editor > Inspections > J2ME issues
  2. Check the line for "if statement may be replaced with && or || expression"

Notes

  • The inspection is there for a purpose. It would generally be better to follow the warnings advice and just simplify the if statement. (However, sometimes I find the "simplification" harder to read. Thus my answer here.)
  • You don't really need answers like this. You can autogenerate the suppression code for any warning by clicking on the code with the warning and pressing Alt + Enter and then expanding the options the lightbulb. You will be given options to suppress the warning for the statement, method, class, etc.
like image 44
Suragch Avatar answered Oct 20 '22 11:10

Suragch