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

Report errors for duplicate case clauses #854

Closed
DanielRosenwasser opened this issue Oct 8, 2014 · 5 comments
Closed

Report errors for duplicate case clauses #854

DanielRosenwasser opened this issue Oct 8, 2014 · 5 comments
Labels
Duplicate An existing issue was already created Suggestion An idea for TypeScript

Comments

@DanielRosenwasser
Copy link
Member

It is incredibly easy to make a copy/paste error for case clauses in a switch statement.

switch (node.kind) {
    // ...
    case SyntaxKind.TupleType:
        // Do something with tuple types.
    case SyntaxKind.TupleType:
        // Do something with union types.
    // ...
}

While it is easily arguable that non-exhaustive cases can be allowed in TypeScript, there is no reason not to report duplicated cases.

"Obvious" semantics for case clauses in a single switch statement are as follows:

  • Report when named entities with identical symbols are handled.
  • Report when equal string literals are handled.
  • Report when equal numeric literals (even 0.0 and 0) are handled.
  • Report when null is handled more than once.
  • Report when undefined is handled more than once.

One interesting question is whether exhaustive cases for an enum should report an error when there is a default clause.

@DanielRosenwasser DanielRosenwasser added the Suggestion An idea for TypeScript label Oct 8, 2014
@RyanCavanaugh
Copy link
Member

I don't think we can ever assume exhaustiveness in enums -- flags enums, for example, still have a valid default clause when all their masks have been enumerated.

@DanielRosenwasser
Copy link
Member Author

Good point, amending the suggestion.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 9, 2014

This can be covered by flagging unreachable code. possibly part of #274

@RyanCavanaugh RyanCavanaugh added the In Discussion Not yet reached consensus label Oct 24, 2014
@sophiajt
Copy link
Contributor

sophiajt commented Dec 2, 2014

I'd prefer this to be part of #274, too, rather than a separate feature.

@danquirk
Copy link
Member

This will tracked/implemented as part of #2854 now rather than potentially in #274.

@danquirk danquirk added Duplicate An existing issue was already created and removed In Discussion Not yet reached consensus labels Apr 21, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants