-
Notifications
You must be signed in to change notification settings - Fork 944
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt to implement else-if-used https://pylint.pycqa.org/en/latest/user_guide/messages/refactor/else-if-used.html Issue #970
- Loading branch information
1 parent
994e2e0
commit 0b7d6b9
Showing
9 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
crates/ruff/resources/test/fixtures/pylint/collapsible_else_if.py
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,49 @@ | ||
""" | ||
Test for else-if-used | ||
""" | ||
|
||
|
||
def ok0(): | ||
"""Should not trigger on elif""" | ||
if 1: | ||
pass | ||
elif 2: | ||
pass | ||
|
||
|
||
def ok1(): | ||
"""If the orelse has more than 1 item in it, shouldn't trigger""" | ||
if 1: | ||
pass | ||
else: | ||
print() | ||
if 1: | ||
pass | ||
|
||
|
||
def ok2(): | ||
"""If the orelse has more than 1 item in it, shouldn't trigger""" | ||
if 1: | ||
pass | ||
else: | ||
if 1: | ||
pass | ||
print() | ||
|
||
|
||
def not_ok0(): | ||
if 1: | ||
pass | ||
else: | ||
if 2: | ||
pass | ||
|
||
|
||
def not_ok1(): | ||
if 1: | ||
pass | ||
else: | ||
if 2: | ||
pass | ||
else: | ||
pass |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use rustpython_parser::ast::{Stmt, StmtKind}; | ||
|
||
use ruff_macros::{define_violation, derive_message_formats}; | ||
|
||
use crate::ast::types::Range; | ||
use crate::registry::Diagnostic; | ||
use crate::source_code::Locator; | ||
use crate::violation::Violation; | ||
|
||
define_violation!( | ||
pub struct CollapsibleElseIf; | ||
); | ||
|
||
impl Violation for CollapsibleElseIf { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Consider using `elif` instead of `else` then `if` to remove one indentation level") | ||
} | ||
} | ||
|
||
/// PLR5501 | ||
pub fn collapsible_else_if(orelse: &[Stmt], locator: &Locator) -> Option<Diagnostic> { | ||
if orelse.len() == 1 { | ||
let first = &orelse[0]; | ||
if matches!(first.node, StmtKind::If { .. }) { | ||
// Determine whether this is an `elif`, or an `if` in an `else` block. | ||
if locator | ||
.slice(&Range { | ||
location: first.location, | ||
end_location: first.end_location.unwrap(), | ||
}) | ||
.starts_with("if") | ||
{ | ||
return Some(Diagnostic::new( | ||
CollapsibleElseIf, | ||
Range::from_located(first), | ||
)); | ||
} | ||
} | ||
} | ||
None | ||
} |
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
25 changes: 25 additions & 0 deletions
25
...rc/rules/pylint/snapshots/ruff__rules__pylint__tests__PLR5501_collapsible_else_if.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,25 @@ | ||
--- | ||
source: crates/ruff/src/rules/pylint/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
CollapsibleElseIf: ~ | ||
location: | ||
row: 38 | ||
column: 8 | ||
end_location: | ||
row: 39 | ||
column: 16 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
CollapsibleElseIf: ~ | ||
location: | ||
row: 46 | ||
column: 8 | ||
end_location: | ||
row: 49 | ||
column: 16 | ||
fix: ~ | ||
parent: ~ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.