-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
flake8-simplify
] Add Rule for SIM115
(Use context handler for op…
…ening files) (#1782) ref: #998 Co-authored-by: Charlie Marsh <[email protected]>
- Loading branch information
1 parent
4fce296
commit de81b0c
Showing
10 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
f = open('foo.txt') # SIM115 | ||
data = f.read() | ||
f.close() | ||
|
||
with open('foo.txt') as f: # OK | ||
data = f.read() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1520,6 +1520,7 @@ | |
"SIM110", | ||
"SIM111", | ||
"SIM112", | ||
"SIM115", | ||
"SIM117", | ||
"SIM118", | ||
"SIM2", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/flake8_simplify/rules/open_file_with_context_handler.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use rustpython_ast::Expr; | ||
use rustpython_parser::ast::StmtKind; | ||
|
||
use crate::ast::helpers::{collect_call_paths, dealias_call_path, match_call_path}; | ||
use crate::ast::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::registry::Diagnostic; | ||
use crate::violations; | ||
|
||
/// SIM115 | ||
pub fn open_file_with_context_handler(checker: &mut Checker, func: &Expr) { | ||
if match_call_path( | ||
&dealias_call_path(collect_call_paths(func), &checker.import_aliases), | ||
"", | ||
"open", | ||
&checker.from_imports, | ||
) { | ||
if checker.is_builtin("open") { | ||
match checker.current_stmt().node { | ||
StmtKind::With { .. } => (), | ||
_ => { | ||
checker.diagnostics.push(Diagnostic::new( | ||
violations::OpenFileWithContextHandler, | ||
Range::from_located(func), | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/flake8_simplify/snapshots/ruff__flake8_simplify__tests__SIM115_SIM115.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
source: src/flake8_simplify/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
OpenFileWithContextHandler: ~ | ||
location: | ||
row: 1 | ||
column: 4 | ||
end_location: | ||
row: 1 | ||
column: 8 | ||
fix: ~ | ||
parent: ~ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters