twigcs icon indicating copy to clipboard operation
twigcs copied to clipboard

Variables set and used in loop should not be marked as unused

Open danog opened this issue 2 years ago • 2 comments

Steps required to reproduce the problem

{% set first = true %}
{% for part in content %}
	{% if not first %}
		<div style="page-break-after: always"></div>
	{% else %}
		{% set first = false %}
	{% endif %}
	{{ part|raw }}
{% endfor %}

Expected Result

  • No warning

Actual Result

  • l.6 c.9 : WARNING Unused variable "first".

danog avatar Nov 15 '22 09:11 danog

Hi,

You need to use loop instead https://twig.symfony.com/doc/2.x/tags/for.html

Like this :

{% for part in content %}
	{% if not loop.first %}
		<div style="page-break-after: always"></div>
	{% endif %}
	{{ part|raw }}
{% endfor %}

But i think it's a bug for the cs. Thanks to report !

Ciloe avatar Nov 15 '22 09:11 Ciloe

Thank you, will use loop.first instead! Leaving this open as IMO this is still a valid twigcs issue ;)

danog avatar Nov 15 '22 09:11 danog