[code-quality] Fwd: Spurious useless-else-on-loop warning

Michael Foord fuzzyman at gmail.com
Wed Sep 18 06:33:56 CEST 2013


On 18 September 2013 02:39, Skip Montanaro <skip at pobox.com> wrote:

> Ned,
>
> I still think you're missing the point of my original post. A return
> is as good as a break when considering early exit from a loop. The
> pylint code only checks for the presence of a break statement, but the
> test cases clearly show a case with a return statement. The test
> functions even include docstrings which state that.
>


It's not considering the exit from the loop it's considering the entry to
the else clause. A break would continue past the body of the loop without
entering the else clause (so the else would have a meaning - only enter
this code if we haven't hit break). Without a break the else clause will
always be entered if the loop terminates normally, so the else is useless.
Just putting the return immediately after the loop is functionally
identical.

That you have some early returns inside the body of the loop is irrelevant
- the "else" is still unnecessary / pointless.

Michael


>
> That's all I'm trying to point out. That I discovered the problem in a
> piece of code which could be rewritten to avoid the warning is quite
> beside the point.
>
> Skip
> _______________________________________________
> code-quality mailing list
> code-quality at python.org
> https://mail.python.org/mailman/listinfo/code-quality
>



-- 

http://www.voidspace.org.uk/

May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/code-quality/attachments/20130918/6eeeab34/attachment-0001.html>


More information about the code-quality mailing list