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.

`Result value must be used` check

See original GitHub issue

Hello.

Quite often while working with ImmutableJS data structures and other immutable libraries people forget that values are immutable:

They can accidentally write:

obj.set('name', 'newName');

Instead of:

let newObj = obj.set('name', 'newName');

These errors are hard to discover and they are very annoying. I think that this problem can’t be solved by tslint, so I propose to add some sort of "you must use the result" check into the compiler. But right now I have no idea how I want it to be expressed in the language, so I create this issue mainly to start a discussion.

Rust lang, for example, solves a similar problem with #[must_use] annotation. This is not exact what we want, but it’s a good example and shows that the problem is quite common.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:15
  • Comments:22 (8 by maintainers)

github_iconTop GitHub Comments

7reactions
nh2commented, Sep 28, 2021

Here is another example of a refactoring error that this type of check would prevent:

Consider having an onclick handler:

const onMovementClick = (direction: "forward" | "backward") => () => { ... }

used like:

<div onClick={onMovementClick("forward") }>Forward</div>
<div onClick={onMovementClick("backward")}>Backward</div>

Now you refactor, adding some logging:

<div onClick={() => { log("forward");  onMovementClick("forward");  } }>Forward</div>
<div onClick={() => { log("backward"); onMovementClick("backward"); } }>Backward</div>

This refactor is wrong; your buttons now no longer have any effect and your web app is broken due to what’s logically a type error that TypeScript did not help prevent. The correct code has some added () function calls:

<div onClick={() => { log("forward");  onMovementClick("forward")();  } }>Forward</div>
<div onClick={() => { log("backward"); onMovementClick("backward")(); } }>Backward</div>

Here, one needed to call f(someArg)() instead of f(someArg); the latter returns a function that is then unused.

If TypeScript had the ability to warn on unused return values, like Haskell and Rust can do with -Wall, this mistake would be impossible to make.

2reactions
TimWollacommented, Aug 31, 2021

After having been burnt by a library updating a method to be const/pure in a new major version, whereas it modified state previously: +1

Calling pure methods without using the return value almost certainly is a bug. So if the library author was able to indicate this in some standardized way (e.g. the nodiscard as suggested above), TypeScript might’ve been able to prevent this issue from happening to me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Value Check - Oracle
The Value Check processor compares the data in an attribute against a single value. There are a number of options for performing the...
Read more >
How to correct a #VALUE! error - Microsoft Support
Excel shows the #VALUE! error when your formula includes cells that have different data types (text and numeric values). The #VALUE! error is...
Read more >
P-Value: What It Is, How to Calculate It, and Why It Matters
In statistics, the p-value is the probability of obtaining results at least as extreme as ... The p-value hypothesis test does not necessarily...
Read more >
How can I test that a value is "greater than or equal to" in ...
addMatchers( { toBeAtLeast: function () { return { compare: function ... You can use the function least to check if a value is...
Read more >
S.3.1 Hypothesis Testing (Critical Value Approach)
If the test statistic is more extreme than the critical value, then the null ... To conduct the hypothesis test for the population...
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