Skip to content

Commit 71a122f

Browse files
Allow open without context manager in return statement (#14066)
## Summary Closes #13862.
1 parent 3ca2478 commit 71a122f

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM115.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,11 @@ def aliased():
256256
# SIM115
257257
f = dbm.sqlite3.open("foo.db")
258258
f.close()
259+
260+
# OK
261+
def func(filepath, encoding):
262+
return open(filepath, mode="rt", encoding=encoding)
263+
264+
# OK
265+
def func(filepath, encoding):
266+
return f(open(filepath, mode="rt", encoding=encoding))

crates/ruff_linter/src/rules/flake8_simplify/rules/open_file_with_context_handler.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ pub(crate) fn open_file_with_context_handler(checker: &mut Checker, call: &ast::
214214
return;
215215
}
216216

217+
// Ex) `return open("foo.txt")`
218+
if semantic.current_statement().is_return_stmt() {
219+
return;
220+
}
221+
217222
// Ex) `with contextlib.ExitStack() as exit_stack: ...`
218223
if match_exit_stack(semantic) {
219224
return;

0 commit comments

Comments
 (0)