Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-unreachable-code: error on default clause of exhaustive switch #740

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ switch (Boolean()) {
'bar';
break;
default:
~~~~~~~ [error no-unreachable-code: 'default' clause is unreachable in exhaustive 'switch' statements.]
'baz';
break;
'bas';
Expand Down
1 change: 1 addition & 0 deletions packages/mimir/docs/no-unreachable-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,5 @@ function foo() {

## Related Rules

* [`no-fallthrough`](no-fallthrough.md)
* [`return-never-call`](return-never-call.md)
28 changes: 24 additions & 4 deletions packages/mimir/src/rules/no-unreachable-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getControlFlowEnd,
endsControlFlow,
isLabeledStatement,
hasExhaustiveCaseClauses,
} from 'tsutils';

type ForDoWhileStatement = ts.ForStatement | ts.WhileStatement | ts.DoStatement;
Expand All @@ -17,10 +18,13 @@ export class Rule extends AbstractRule {
for (const node of this.context.getFlatAst()) {
switch (node.kind) {
case ts.SyntaxKind.Block:
case ts.SyntaxKind.CaseClause:
case ts.SyntaxKind.DefaultClause:
case ts.SyntaxKind.SourceFile:
case ts.SyntaxKind.ModuleBlock:
this.checkBlock(<ts.BlockLike>node);
break;
case ts.SyntaxKind.SwitchStatement:
this.checkSwitch(<ts.SwitchStatement>node);
break;
case ts.SyntaxKind.IfStatement:
this.checkIfStatement(<ts.IfStatement>node);
break;
Expand All @@ -31,9 +35,23 @@ export class Rule extends AbstractRule {
}
}

private checkSwitch(node: ts.SwitchStatement) {
for (const clause of node.caseBlock.clauses) {
this.checkBlock(clause);
if (clause.kind === ts.SyntaxKind.DefaultClause) {
const checker = this.program?.getTypeChecker();
if (checker !== undefined && hasExhaustiveCaseClauses(node, checker))
this.addFindingAtNode(
clause.getFirstToken(this.sourceFile)!,
"'default' clause is unreachable in exhaustive 'switch' statements.",
);
}
}
}

private checkBlock(node: ts.BlockLike) {
let i = node.statements.findIndex(this.nextStatementIsUnreachable, this);
if (i === -1 || i === node.statements.length - 1)
if (i === -1)
return;
for (i += 1; i < node.statements.length; ++i)
if (isExecutableStatement(node.statements[i]))
Expand Down Expand Up @@ -71,7 +89,9 @@ export class Rule extends AbstractRule {
this.addFindingAtNode(node.getFirstToken(this.sourceFile)!, 'Unreachable code detected.');
}

private nextStatementIsUnreachable(statement: ts.Statement): boolean {
private nextStatementIsUnreachable(statement: ts.Statement, index: number, statements: readonly ts.Statement[]): boolean {
if (index === statements.length - 1)
return false; // no need to check the last statement in a block
if (endsControlFlow(statement, this.program?.getTypeChecker()))
return true;
const labels: string[] = [];
Expand Down
6 changes: 0 additions & 6 deletions packages/wotan/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ export function format<T = any>(value: T, fmt = Format.Yaml): string {
schema: yaml.JSON_SCHEMA,
sortKeys: true,
});
default:
return assertNever(fmt);
}
}

Expand Down Expand Up @@ -81,10 +79,6 @@ function convertToPrintable(value: any): any {
return added ? newValue : undefined;
}

export function assertNever(v: never): never {
throw new Error(`unexpected value '${v}'`);
}

export function calculateChangeRange(original: string, changed: string): ts.TextChangeRange {
const diff = changed.length - original.length;
let start = 0;
Expand Down
5 changes: 0 additions & 5 deletions packages/wotan/test/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import test from 'ava';
import {
calculateChangeRange,
assertNever,
resolveCachedResult,
} from '../src/utils';

Expand Down Expand Up @@ -46,10 +45,6 @@ test('calculateChangeRange', (t) => {
}
});

test('assertNever', (t) => {
t.throws(() => assertNever(<never>'a'));
});

test('resolveCachedResult', (t) => {
const cache = new Map<string, string | undefined>();
t.is(
Expand Down