-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Result value must be used
check
#8240
Comments
+1 |
Why? Not using a return value is exactly what a linter is best at doing. Identifying non-syntatical errors that may lead to logic errors in the code. |
but linter does not have type information I suppose. so it will be very noisy about legal cases |
Yes, we must rely on type information here, but |
|
I agree with @kitsonk on this issue. it is perfectly valid to ignore the result of a call. so unless we introduce new syntax that marks these functions, this looks like a style restriction for specific teams and does not apply for the general population. |
This is exactly what I'm asking for. I really don't believe that it's a style restriction, because it breaks logic, not appearance. And this error is quite common and the harm is obvious. Sometimes a lot of time must be spent to find out why object field doesn't contain a value which I've set to it. And this applies at least to all people who uses immutable data structures (not only ImmutableJS) in some way. There are other potential use-cases like |
The example you show doesn't use type information, it uses only syntax analysis: In my example there is no any syntactical markers which can be used to find invalid usage, so we need information of type level. I think it will try to write a formal proposal for this to explain what I'm taking about and why this is useful. |
I still believe this is a style guideline more than a compiler error. i would assume some users want to opt out of the checking, or disable the error for some cases. this puts more in the vein of linting. i would assume something like #2900 to define the metadata on the function, and a linter to impose the rule. |
@mhegazy this can really work with ambient decorators if
Is this possible with |
The compiler API exposes a checker object that can answer these questions, so i do not see why not. |
@mhegazy but doesn't it require a second compilation pass for |
it requires global analysis, yes. |
Reopening for consideration, since we have got multiple requests for this. |
Tracking at #8584 |
This certainly got bounced around! I'm quite happy for this to be "just" a decorator and lint, but the --lib typings should be able to benefit from it too. Would declaration merging support adding design time decorators? |
I think it should be implemented similar to c++
|
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 |
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 <div onClick={() => { log("forward"); onMovementClick("forward")(); } }>Forward</div>
<div onClick={() => { log("backward"); onMovementClick("backward")(); } }>Backward</div> Here, one needed to call If TypeScript had the ability to warn on unused return values, like Haskell and Rust can do with |
What's the current status on this? |
I agree that a "nodiscard" decorator would be very useful. As someone said above, these bugs a hard to find once someone put them in. |
such decorator would also be useful if we want to return errors (similar to |
If the implementation of Also ther is need to be an explicit way to discard values. May be: function discardReturnedValue<T>(valueToDiscard:T):void{} then async closeFile(f:FileHandler):Promise<void>{...}
discardReturnedValue(closeFile(f)) see #13376 |
A It moves control to the writer of the function, instead of giving control over wardnings to the downstream project that uses them. In C++, this makes Further, a callee-side annotation would not fix key issues like my example (#8240 (comment)), as there the thing whose return value is ignored is itself a function call, and it's tough to find a place where you could put the annotation when partially applied functions are involved. So I think a caller-side check that the user can enable as a warning is much better. Even if a |
@nh2 I'm a bit confused. Isn't |
@xuhdev I would say he means that any callee side annotation - and he points out that feature is, in C++ - would be useless unless it gets retrofitted onto every library function that needs it, and chances of that are zero. That's why it is more practical to have a caller-side mechanism so that those of us who care about it can use it when calling all kinds of libraries and be safe. |
I think it would be adopted. Developers of good software look forward to features like this which make their programs more resilient. It's like saying TypeScript also gives you escape hatches:
|
This is exactly what I meant. |
This eslint rule might be useful: https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/no-ignored-return.md And here's the source code if anyone wants to try to modify the rule so that it can be used for your own functions. I think eslint is flexible enough to read the jsdoc comments on a function and look for whatever tag you want to add, e.g. |
Hello.
Quite often while working with
ImmutableJS
data structures and other immutable libraries people forget that values are immutable:They can accidentally write:
Instead of:
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.The text was updated successfully, but these errors were encountered: