-
Notifications
You must be signed in to change notification settings - Fork 885
"Result value must be used" rule #1154
Comments
Result value must be used
rule
See also #1135, which is a similar suggestion for core JS functions |
@jkillian yes, this looks similar. I want to know what I'm an author of docscript and awesome-typescript-loader projects, so I know TS internals quite well to implement this. |
My two cents: this doesn't belong in core In any case, to implement this properly you're going to find yourself blocked on #680 - you will need full type information, which you don't have in |
I'm looking to implement this lint check for core JS functions on a blacklisting basis. The idea is that a function would fail the check if:
(1) is now available through type checking. (2) is up for discussion. An easy solution that is compatible with rule options and does not require extra code is to have an array of blacklisted function names. For example: Some other possibilities are a generic type or a JSDoc annotation, which would be more robust but require changing code and .d.ts files. Ambient decorators would probably be the best option, but it doesn't look like they are in TypeScript yet. (3) can be determined by walking along parent nodes until a certain node type is reached. Otherwise if an Thoughts? |
For context, this is how we did it on my Java static checker project: API maintainers mark the functions whose return values must not be ignored. As Scott says, we don't have microsoft/TypeScript#2900 yet, and we'd also need to change TS syntax to allow Decorators on functions. So I think this is out. We should explore a generic wrapper type, eg.
We could force users to JSDoc is a crappy last choice, IMHO. Separately, we need to maintain our own list of APIs from the core/standard libraries whose return value must not be ignored. In Java we assumed that we'll never get a change upstream into those libraries to mark them. We might have better luck with TypeScript, but it would still take a long time to get agreement about how we mark these, discussion of how the type system should represent them instead ( cc @rkirov ) and then actually add them to |
Scott has a WIP for this, see ScottSWu@21bed19 @jkillian any thoughts about the design questions? |
Hmm, I'm curious as to why you find JSDoc a bad choice? /** @MustUse */
function trim(s: string): {
return s.trim();
}
// disallowed:
trim(" string ");
// allowed?
trim(" string ") || someFunc();
// allowed
const newStr = trim(" string "); If we required a |
JSDoc has a few drawbacks:
Like you say, the drawback of a real type is that the user must either declare that awkward Having laid out the reasoning, I'd be fine to go either way. |
Given what you said, I'm still leaning towards JSDoc. It feels less invasive to me than adding a That being said, I'd guess that you have more experience and perspective here, if you still think the type is a better answer, I can probably be convinced
I think we could accept any capitalization of |
Oh, one other thing, we'll have to lookup (and probably memoize) the declaration of every function or method called in the program to see if the JSDoc contains the special bit. We'll have to see how it affects performance. |
Why isn't that a core rule for all functions? (we have tslint ignore for corner cases) Example: https://gist.github.com/arackaf/7826ff661db492c7b5d3ef95dd2afef4 A requirement to add a "must use" declaration (in any shape: change return type or add a comment) requires changing code you don't own hence cannot or should not change: typings for standard objects (Array.concat), async function return types (must be Promise), typings for libraries (immutable.js), etc. |
We can totally change the typings for standard objects and libraries. We already do this as TypeScript adds features to allow libraries to be typed more precisely. |
@alexeagle The fact we can, in general, doesn't mean we should for this usecase, on a per-app basis (that's how tslint is configured). Overriding the standard library or any other library type definitions is a workaround to update an app while the upstream libraries are lagging behind on the type definitions. Unless MustUse is proposed to be a TypeScript compiler feature (which AFAIK is not the case, we're talking tslint now). |
💀 It's time! 💀TSLint is being deprecated and no longer accepting pull requests for major new changes or features. See #4534. 😱 If you'd like to see this change implemented, you have two choices:
👋 It was a pleasure open sourcing with you! If you believe this message was posted here in error, please comment so we can re-open the issue! |
🤖 Beep boop! 👉 TSLint is deprecated 👈 (#4534) and you should switch to typescript-eslint! 🤖 🔒 This issue is being locked to prevent further unnecessary discussions. Thank you! 👋 |
Original issue and discussion microsoft/TypeScript#8240
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.@mhegazy in the original discussion proposed to use ambient decorators. But this requires to perform not only syntactical analysis, but global type analysis too (using typescript checker API). I'm interested is this in scope of tslint? Or we need to create another linter tool which will work with types to resolve this issue?
The text was updated successfully, but these errors were encountered: