question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Detect unreachable code in more scenarios

See original GitHub issue

Search Terms

logical assignment binary expression dead never truthy falsy

Suggestion

Raise compiler errors when the left side of an expression using a logical operator (&& or ||) is unrechable.

Also detect unreachable branches of ternary expressions.

Use Cases

Code that is statically verifiable to be unreahable is almost always a programmer error so TypeScript should detect unreachable code in more scenarios.

Examples

declare const truthy: true
declare const falsy: false;
declare function doSomething(): number

const a = true ? 5 : doSomething(); // error: doSomething is unreachable
const b = truthy ? 5 : doSomething(); // error: doSomething is unreachable

const c = false ? doSomething() : 5; // error: doSomething is unreachable
const d = falsy ? doSomething() : 5; // error: doSomething is unreachable

if (!truthy) {
    doSomething(); // error: doSomething is unreachable
}

if (falsy) {
    doSomething(); // error: doSomething is unreachable
}

Playground link.


It would also be nice to expand unreachable code checks to other expressions that are known to be truthy or falsy eg. object/array literals, NaN eg.

declare function doSomething(): number

const a = [1,2,3] || doSomething() // error: doSomething is unreachable
const b = { count: 1 } && doSomething() // error: doSomething is unreachable

if ([1,2,3]) {
    doSomething() // error: doSomething is unreachable
}

if ({ count: 1 }) {
    doSomething() // error: doSomething is unreachable
}

Playground link.


Finally, detecting unreachable branches of a ternary expression

declare const truthy: true
declare const falsy: false;
declare function doSomething(): number

const a = true ? 5 : doSomething(); // error: doSomething is unreachable
const b = truthy ? 5 : doSomething(); // error: doSomething is unreachable

const c = false ? doSomething() : 5; // error: doSomething is unreachable
const d = falsy ? doSomething() : 5; // error: doSomething is unreachable

Playground link

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

This wouldn’t be a breaking change in existing TypeScript/JavaScript code

There would be new type errors. The extra safety could be opted into by extending the allowUnreachableCode flag or creating a new flag.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
tom-shermancommented, Sep 3, 2020

@RyanCavanaugh I agree that some of these may not be too useful, but others where TypeScript knows that the value is truthy (objects, mostly) I think are helpful.

[] || doSomething() I think it’s really common for TypeScript to infer the LHS is truthy.

1reaction
tom-shermancommented, Aug 12, 2020

I would say that these unreachable branches of logical operators fall under the category of, quote from the docs

unreachable due to the use of JavaScript syntax

Read more comments on GitHub >

github_iconTop Results From Across the Web

Detecting Unreachable Code and Dead Code
The "Unused code" section of MISRA C contains seven rules that deal with detecting both unreachable code and dead code. The two most...
Read more >
Warning: unreachable code after return statement - JavaScript
The JavaScript warning "unreachable code after return statement" occurs when using an expression after a return statement, or when using a semicolon-less ...
Read more >
Unreachable code - Wikipedia
Analysis. Detection of unreachable code is a form of control flow analysis to find code that can never be reached in any possible...
Read more >
Why isn't unreachable code detected when an if condition is a ...
It should be possible to switch DEBUG between false and true without having to change anything else in the code (due to compilation...
Read more >
Unreachable Code Error in Java - GeeksforGeeks
Have a return statement before them: When a return statement gets executed, then that function execution gets stopped right there. Therefore any ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found