-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Enforce use of function return #3914
Comments
|
@borela, I think you misunderstood my post. Let me know if you need anything clearing up! |
|
I've not had issues with Swift forcing me to use a return value, even if it is just throwing it away. Would this not be a good default? |
It's javascript's default behavior, many libraries rely on it and it would suddenly show errors where none existed before. |
Can you point to any code that makes extensive deliberate use of this?
Isn't that the point of flow? 😂 |
All functions have a implicit "return undefined" and many programmers take advantage of that to save 1 line of code, this is more common than I would like.
It would show errors on valid code. |
I see what you mean! I should have been clearer: for all non-void functions, you should use the return value. Flow already knows that in your example, the function still returns void. If anything, it would highlight errors if someone changed |
It would definitely be a handy lint. There are often requests (both on TypeScript and Flow) for a check to make sure you don't forget to |
@jacobp100 I didn't know flow was able to catch that. Warning of the return values could be a good default then. |
Seems nice, but I hope this can be opted out of. I fear this is going to introduce a lot of errors in cases like componentWillMount() {
loadData()
.then(
data => dispatch({ type: 'LOAD_SUCCESS', data }),
err => dispatch({ type: 'LOAD_FAILURE', err })
);
} |
The heuristic described wouldn't give an error there—you're using the return value! |
Ok, I just assumed that since Even if that's not the case, the example could just as well be const loadDataAndDispatch = () =>
loadData().then(
data => dispatch({ type: 'LOAD_SUCCESS', data }),
err => dispatch({ type: 'LOAD_FAILURE', err })
);
...
componentWillMount() {
loadDataAndDispatch();
} Wouldn't that still cause an error? |
That would. My suggestion would be to write |
Many JS functions return values which you usually don't care about. It'd be bothersome to have to write code like this: const arr = [1, 2, 3];
void arr.push(4); // I don't care about the new length
void arr.sort(); // I don't need another reference to the array
void arr.reverse(); // I don't need another reference to the array
void arr.splice(2, 1); // I don't need the removed element You could have some syntax for declaring "This is an important return value" but then you get to have religious wars over whether or not |
Forgetting to return or await a Promise is a really common mistake that I hope can be solved by a change like this, however I agree with the comments that there are too many existing javascript functions, especially in the standard lib, that return values you usually don't care about. I'd love to see behavior like this as an opt-in option, but as the default specifically for Promise values |
Related: #2281 |
there is eslint plugin for this Disallow Unused Expressions (no-unused-expressions) |
@thecotne From the documentation for that rule:
|
@noppa i did not payed much attention Hah but this issue implies that in order to use function with side effects you need to put void in front of function call it can be implemented in eslint with option like i think this is easier to implement in eslint then flow |
Swift and Rust both have the behaviour that if you define a function that returns a value, you must use the value.
In both of these languages, you can disregard the return value by assigning to a special variable,
_
. In JS, we could use thevoid
keyword.Would this be a good addition?
And if so, what would be the best approach? Would something like this work,
and funcalltype = { call_this_t: t; call_args_tlist: call_arg list; call_tout: t; call_closure_t: int; call_strict_arity: bool; + call_return_used: bool; }
And set
call_return_used
to true whenever theCallExpression
is a direct child ofExpressionStatement
. Then finally matchCallT(_ { call_return_used: true })
to add errors?The text was updated successfully, but these errors were encountered: