vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.56k stars 660 forks source link

Feature Request: attribute for marking that a function result must be used. #6331

Open morrisonlevi opened 3 years ago

morrisonlevi commented 3 years ago

Other languages have this.

We would need to pin down a name, and ideally co-ordinate with other static analysis tools so it isn't quite so tool specific.

Motivating cases are when you return some sort of status object, which commonly will have error cases. Most recently I had a function that was returning an optional error ?ExportError and callers which ignore this result are inherently buggy.

psalm-github-bot[bot] commented 3 years ago

Hey @morrisonlevi, can you reproduce the issue on https://psalm.dev ?

weirdan commented 3 years ago

For pure functions you already have that out of the box: https://psalm.dev/r/28c64e855b

And impure function could be called for their side-effects.

psalm-github-bot[bot] commented 3 years ago

I found these snippets:

https://psalm.dev/r/28c64e855b ```php
morrisonlevi commented 3 years ago

Just because a function is impure doesn't mean the result should be ignored. This is why an attribute like ones that exist in Rust or GCC are useful.

weirdan commented 3 years ago

Mind providing an example where this would be useful?