[Python-ideas] SyntaxWarning for for/while/else without break or return?
Masklinn
masklinn at masklinn.net
Thu Oct 8 16:06:40 CEST 2009
On 8 Oct 2009, at 15:44 , Gerald Britton wrote:
> re warnings: The compiler is simply not smart enough to know if a
> program is doing what a programmer wants.
There are cases in which what's written doesn't make sense or serve
any purpose (even if it isn't actively harmful). This includes
variables which are created but never used, side-effect free functions
whose results are not collected or branches which cannot be taken.
Compilers or runtimes typically warn about such cases, this allows the
programmer to spot potential bugs and/or simplify their code.
And having a `for: else:` statement without a `break` is one such case
which, according to Nick Coghlan, can be recognized. So why not warn
about it?
> If a given program is
> working quietly according to its specifications, we should do nothing
> to disturb the peace.
>
If the program is running quietly on its current runtime, it'll keep
doing so as long as it isn't switched to a runtime implementing that
warning won't it?
> re syntax: There are at least three ways to exit a for-loop early:
> break, return and raise (explicit or implicit). Would this code
> generate a warning? (I hope not)
>
Both would, because in both cases the `else:` clause serves no purpose
whatsoever. It's noise.
More information about the Python-ideas
mailing list