Files
codeql/python/ql/src/Statements/UnnecessaryElseClause.ql
Taus Brock-Nannestad f07a7bf8cf Python: Autoformat everything using qlformat.
Will need subsequent PRs fixing up test failures (due to deprecated
methods moving around), but other than that everything should be
straight-forward.
2020-07-07 15:43:52 +02:00

25 lines
767 B
Plaintext

/**
* @name Unnecessary 'else' clause in loop
* @description An 'else' clause in a 'for' or 'while' statement that does not contain a 'break' is redundant.
* @kind problem
* @tags maintainability
* useless-code
* @problem.severity warning
* @sub-severity low
* @precision very-high
* @id py/redundant-else
*/
import python
from Stmt loop, StmtList body, StmtList clause, string kind
where
(
exists(For f | f = loop | clause = f.getOrelse() and body = f.getBody() and kind = "for")
or
exists(While w | w = loop | clause = w.getOrelse() and body = w.getBody() and kind = "while")
) and
not exists(Break b | body.contains(b))
select loop,
"This '" + kind + "' statement has a redundant 'else' as no 'break' is present in the body."