Skip to content
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

"The call to ::getIterator is not used" reported right where it's used #3625

Closed
orklah opened this issue Jun 20, 2020 · 8 comments
Closed

"The call to ::getIterator is not used" reported right where it's used #3625

orklah opened this issue Jun 20, 2020 · 8 comments
Labels

Comments

@orklah
Copy link
Collaborator

orklah commented Jun 20, 2020

Hi,

We got a strange error on phpDocumentor CI:
https://github.com/phpDocumentor/phpDocumentor/runs/790360908?check_suite_focus=true#step:9:18
The error warn of an issue with a getIterator function being unused here:
phpDocumentor/phpDocumentor@337b40d#annotation_257518989

But that's exactly the line where it's actually used, I don't understand what psalm is trying to report here.

I couldn't reproduce the issue locally, but phpDocumentor CI is complex so I don't have the same setup.

The issue was "solved" by changing "findUnusedVariablesAndParams" to false, but it'd be cool to be able to enable this check again.

Do you have an idea what could be the issue here?

@psalm-github-bot
Copy link

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

@weirdan
Copy link
Collaborator

weirdan commented Jun 23, 2020

Call to ... unused actually refers to the return value of a pure function, as calling a pure function without using its result is pointless. It doesn't seem appropriate here though.

@weirdan weirdan added the bug label Jun 23, 2020
@weirdan
Copy link
Collaborator

weirdan commented Jun 23, 2020

Reproduced with https://psalm.dev/r/6287fdb60c
@orklah is your code similar to that?

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/6287fdb60c
<?php
/** @psalm-immutable */
class C implements IteratorAggregate {
    public function getIterator(): Iterator {
        return new ArrayIterator([]);
    }
}

$d = [];
/** @psalm-suppress MixedAssignment */
foreach (new C as $elt) {
    $d[(string)$elt] = $elt;
}
Psalm output (using commit e569f08):

ERROR: UnusedMethodCall - 11:10 - The call to C::getIterator is not used

@orklah
Copy link
Collaborator Author

orklah commented Jun 24, 2020

@orklah is your code similar to that?

Kinda, but there isn't a psalm-immutable annotation in my case. And without it I can't reproduce the issue at all...

@enumag
Copy link
Contributor

enumag commented Oct 20, 2020

Here are my reproducers from #4373 - slightly cleaner as they don't have other problems:

https://psalm.dev/r/dd77299d18
https://psalm.dev/r/ab4bb38b1e

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/dd77299d18
<?php

/**
 * @psalm-immutable
 */
class A implements IteratorAggregate
{
    /**
     * @return Iterator<string>
     */
	public function getIterator() {
    	yield from [];
    }
}

$a = new A;

foreach ($a as $v) {
    echo $v;
}
Psalm output (using commit b904b1d):

ERROR: UnusedMethodCall - 18:10 - The call to A::getIterator is not used
https://psalm.dev/r/ab4bb38b1e
<?php

class A implements IteratorAggregate
{
    /**
     * @return Iterator<string>
     * @psalm-mutation-free
     */
	public function getIterator() {
    	yield from [];
    }
}

$a = new A;

foreach ($a as $v) {
    echo $v;
}
Psalm output (using commit b904b1d):

ERROR: UnusedMethodCall - 16:10 - The call to A::getIterator is not used

@muglug muglug closed this as completed in 6678071 Oct 20, 2020
@muglug
Copy link
Collaborator

muglug commented Oct 20, 2020

Thanks @enumag!

danog pushed a commit to danog/psalm that referenced this issue Jan 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants