Skip to content

Commit d8d26d1

Browse files
committed
Python: Port ShouldUseWithStatement.ql
Only trivial test changes.
1 parent 55ed8e2 commit d8d26d1

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

python/ql/src/Statements/ShouldUseWithStatement.ql

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414

1515
import python
16-
private import LegacyPointsTo
16+
private import semmle.python.dataflow.new.internal.DataFlowDispatch
1717

1818
predicate calls_close(Call c) { exists(Attribute a | c.getFunc() = a and a.getName() = "close") }
1919

@@ -23,18 +23,12 @@ predicate only_stmt_in_finally(Try t, Call c) {
2323
)
2424
}
2525

26-
predicate points_to_context_manager(ControlFlowNodeWithPointsTo f, ClassValue cls) {
27-
forex(Value v | f.pointsTo(v) | v.getClass() = cls) and
28-
cls.isContextManager()
29-
}
30-
31-
from Call close, Try t, ClassValue cls
26+
from Call close, Try t, Class cls
3227
where
3328
only_stmt_in_finally(t, close) and
3429
calls_close(close) and
35-
exists(ControlFlowNode f | f = close.getFunc().getAFlowNode().(AttrNode).getObject() |
36-
points_to_context_manager(f, cls)
37-
)
30+
classInstanceTracker(cls).asExpr() = close.getFunc().(Attribute).getObject() and
31+
DuckTyping::isContextManager(cls)
3832
select close,
3933
"Instance of context-manager class $@ is closed in a finally block. Consider using 'with' statement.",
4034
cls, cls.getName()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| test.py:168:9:168:17 | Attribute() | Instance of context-manager class $@ is closed in a finally block. Consider using 'with' statement. | test.py:151:1:151:17 | class CM | CM |
1+
| test.py:168:9:168:17 | Attribute() | Instance of context-manager class $@ is closed in a finally block. Consider using 'with' statement. | test.py:151:1:151:17 | Class CM | CM |

0 commit comments

Comments
 (0)