An else block holds a single if statement

tx0/ts:lonely-if-in-elsestrictwarningstyle

What it finds

else { if (b) { ... } } with nothing else in the block is an else if written with an extra level of nesting. Writing else if (b) keeps the chain flat.

The fix replaces the block by the if statement inside it. It is offered only when the block holds no comment, which would otherwise be lost.

From the rule's source, lonely_if_in_else.rs.

Example from the rule's tests
function check(a: boolean, b: boolean) {  if (a) {    return 1;  } else {    if (b) {tx0 reports: write else if      return 2;    }  }  if (a) {    return 3;  } else {    if (b) {      return 4;    }    return 5;  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:lonely-if-in-else" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:lonely-if-in-else] reason="..."

tx0 explain tx0/ts:lonely-if-in-else shows the same in the terminal. Found a false positive? Report it.