mirror of
https://github.com/github/codeql.git
synced 2026-07-04 02:55:30 +02:00
By limiting the results to the class that actually defines the `__del__` method, we eliminate a bunch of FPs where a _subclass_ of such a class would also get flagged.
26 lines
755 B
Plaintext
26 lines
755 B
Plaintext
/**
|
|
* @name Class should be a context manager
|
|
* @description Making a class a context manager allows instances to be used in a 'with' statement.
|
|
* This improves resource handling and code readability.
|
|
* @kind problem
|
|
* @tags quality
|
|
* maintainability
|
|
* readability
|
|
* performance
|
|
* @problem.severity recommendation
|
|
* @sub-severity high
|
|
* @precision medium
|
|
* @id py/should-be-context-manager
|
|
*/
|
|
|
|
import python
|
|
private import semmle.python.dataflow.new.internal.DataFlowDispatch
|
|
|
|
from Class c
|
|
where
|
|
not DuckTyping::isContextManager(c) and
|
|
exists(c.getMethod("__del__"))
|
|
select c,
|
|
"Class " + c.getName() +
|
|
" implements __del__ (presumably to release some resource). Consider making it a context manager."
|