10000 Add a rule which disallows calling a non-void/non-never function without using its return value · Issue #1314 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
dko-slapdash opened this issue Dec 6, 2019 · 4 comments
Labels
enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on

Comments

@dko-slapdash
Copy link
dko-slapdash commented Dec 6, 2019

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?

// eslint-enable-next-line @typescript-eslint/no_unused_result <-- ???
function f() {
  return 10;
}

f();

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.

@dko-slapdash dko-slapdash added package: eslint-plugin-tslint Issues related to @typescript-eslint/eslint-plugin-tslint triage Waiting for team members to take a look labels Dec 6, 2019
@bradzacher bradzacher added enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin and removed package: eslint-plugin-tslint Issues related to @typescript-eslint/eslint-plugin-tslint triage Waiting for team members to take a look labels Dec 7, 2019
@bradzacher
Copy link
Member
bradzacher commented Dec 7, 2019

Surprisingly, no-unused-expressions doesn't help here, it doesn't process function returns.

Function return values are not expressions. Function calls are expressions.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Expressions

An expression is any valid unit of code that resolves to a value


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();

@bradzacher bradzacher changed the title Is there a rule which disallows swallowing a particular function return value? Add a rule which disallows calling a non-void/non-never function without using its return value Dec 7, 2019
@robyoder
Copy link
robyoder commented Nov 13, 2020

This would be lovely. I was searching for this because I'm refactoring some code that used to return void or Promise<void> to return real data. I'll add it to my list to potentially cut my teeth on here if I can. :)

I'd disagree on await f(); being valid, but I suppose that could be an option in the rule. Personally, I would only want that to be valid if the return type was Promise<void> because many of the functions I'm working with are returning Promises, so it would defeat the purpose if you awaited and then did nothing with the awaited value.

@JoshuaKGoldberg
Copy link
Member

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 Array's .push returning the new length of the array.

const items = [];
items.push('a', 'b', 'c'); // 3

cc @bradzacher - I'm inclined to close this issue as one that should be implemented externally.

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Feb 23, 2022
@bradzacher
Copy link
Member

yeah definitely - it's really common to have functions which return ignorable values.
another example is the "builder" pattern for which methods all return this.

I don't think this is a good candidate for a lint rule as it would be too noisy.

@bradzacher bradzacher added wontfix This will not be worked on and removed awaiting response Issues waiting for a reply from the OP or another party labels Feb 23, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

4 participants
0