-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--fix does not import sys if needed exit inside functions is not detected
- Loading branch information
1 parent
64e6e44
commit e3cbc0c
Showing
10 changed files
with
152 additions
and
2 deletions.
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,26 @@ | ||
exit(1) | ||
|
||
|
||
import sys | ||
|
||
exit(0) | ||
|
||
|
||
def main(): | ||
exit(3) # Is not detected | ||
|
||
|
||
sys.exit(0) | ||
del sys | ||
|
||
|
||
import sys as sys2 | ||
|
||
exit(2) | ||
|
||
|
||
def exit(e): | ||
pass | ||
|
||
|
||
exit(0) |
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//! Module for Ruff-specific rules. | ||
pub mod checks; | ||
pub mod plugins; |
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,44 @@ | ||
use rustpython_ast::{Expr, ExprKind}; | ||
|
||
use crate::ast::types::{Binding, BindingKind, Range, Scope}; | ||
use crate::autofix::Fix; | ||
use crate::check_ast::Checker; | ||
use crate::checks::{Check, CheckKind}; | ||
|
||
fn get_sys_import(scope: &Scope) -> Option<String> { | ||
let sys_binding_kind = | ||
scope.values.values().map(|v| &v.kind).find( | ||
|b| matches!(b, BindingKind::Importation(_, sys, _) if sys == &"sys".to_string()), | ||
); | ||
if let Some(BindingKind::Importation(name, ..)) = sys_binding_kind { | ||
return Some(name.clone()); | ||
} | ||
None | ||
} | ||
|
||
pub fn convert_sys_to_sys_exit(checker: &mut Checker, func: &Expr) { | ||
if let ExprKind::Name { id, .. } = &func.node { | ||
if id == "exit" { | ||
let scope = checker.current_scope(); | ||
if let Some(Binding { | ||
kind: BindingKind::Builtin, | ||
.. | ||
}) = scope.values.get("exit") | ||
{ | ||
let mut check = | ||
Check::new(CheckKind::ConvertExitToSysExit, Range::from_located(func)); | ||
if checker.patch(check.kind.code()) { | ||
if let Some(mut content) = get_sys_import(scope) { | ||
content.push_str(".exit"); | ||
check.amend(Fix::replacement( | ||
content, | ||
func.location, | ||
func.end_location.unwrap(), | ||
)) | ||
} | ||
} | ||
checker.add_check(check); | ||
} | ||
} | ||
} | ||
} |
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,3 @@ | ||
pub use convert_sys_to_sys_exit::convert_sys_to_sys_exit; | ||
|
||
mod convert_sys_to_sys_exit; |
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,47 @@ | ||
--- | ||
source: src/linter.rs | ||
expression: checks | ||
--- | ||
- kind: ConvertExitToSysExit | ||
location: | ||
row: 1 | ||
column: 0 | ||
end_location: | ||
row: 1 | ||
column: 4 | ||
fix: ~ | ||
- kind: ConvertExitToSysExit | ||
location: | ||
row: 6 | ||
column: 0 | ||
end_location: | ||
row: 6 | ||
column: 4 | ||
fix: | ||
patch: | ||
content: sys.exit | ||
location: | ||
row: 6 | ||
column: 0 | ||
end_location: | ||
row: 6 | ||
column: 4 | ||
applied: false | ||
- kind: ConvertExitToSysExit | ||
location: | ||
row: 19 | ||
column: 0 | ||
end_location: | ||
row: 19 | ||
column: 4 | ||
fix: | ||
patch: | ||
content: sys2.exit | ||
location: | ||
row: 19 | ||
column: 0 | ||
end_location: | ||
row: 19 | ||
column: 4 | ||
applied: false | ||
|