-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add a rule which disallows calling a non-void/non-never function without using its return value #13 8000 14
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
Comments
Function return values are not expressions. Function calls are expressions. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Expressions
There is no rule to check if a function with a non-void return value is being used without storing its return value. Happy to accept a PR to add one. declare function f(): Promise<number> | number;
declare function g(): void;
declare function h(): never;
declare function i(x: unknown): asserts x;
// valid
const x = f();
function z() {
return f();
}
void f();
await f();
typeof f() === 'number';
g();
h();
i(x);
// etc
// invalid
f(); |
This would be lovely. I was searching for this because I'm refactoring some code that used to return I'd disagree on |
This rule would have a ton of false negatives. Many functions return a value that indicates some result or state change even if it's not relevant to the caller. I'm not sold that we want this as part of typescript-eslint core. One example is const items = [];
items.push('a', 'b', 'c'); // 3 cc @bradzacher - I'm inclined to close this issue as one that should be implemented externally. |
yeah definitely - it's really common to have functions which return ignorable values. I don't think this is a good candidate for a lint rule as it would be too noisy. |
Uh oh!
There was an error while loading. Please reload this page.
In C++/gcc, there is a useful attribute which can be assigned to a function, and then swallowing the returned value of this function will be considered as an error:
__attribute__((warn_unused_result))
.Is there something like that for eslint/typescript?
What code were you trying to parse?
What did you expect to happen?
It should warn that the function's return value is not consumed and is swallowed. There is also no eslint-enable-next-line (but maybe there is something instead?).
What actually happened?
Nothing, I couldn't find such a rule. Surprisingly, no-unused-expressions doesn't help here, it doesn't process function returns.
The text was updated successfully, but these errors were encountered: