-
Notifications
You must be signed in to change notification settings - Fork 944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement pylint's else-if-used
rule (PLR5501
)
#3231
Changes from 2 commits
decb8bc
5609ab4
b9cc7cb
0a015b7
0eda514
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use ruff_macros::{define_violation, derive_message_formats}; | ||
use rustpython_parser::ast::{Stmt, StmtKind}; | ||
|
||
use crate::ast::helpers::identifier_range; | ||
use crate::registry::Diagnostic; | ||
use crate::source_code::Locator; | ||
use crate::violation::Violation; | ||
|
||
define_violation!( | ||
pub struct ElseIfUsed {} | ||
); | ||
|
||
impl Violation for ElseIfUsed { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Consider using \"elif\" instead of \"else\" then \"if\" to remove one indentation level") | ||
} | ||
} | ||
|
||
/// PLR5501 | ||
pub fn else_if_used( | ||
stmt: &Stmt, | ||
body: &[Stmt], | ||
_orelse: &[Stmt], | ||
locator: &Locator, | ||
) -> Option<Diagnostic> { | ||
if let StmtKind::If { orelse, .. } = &stmt.node { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we not use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes you're right, going to give that a go. |
||
// If the body contains more than just the orelse, can't apply. | ||
if body.len() == 1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, here if ...:
pass
else:
if:
... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I think we do need to structure this such that it looks at this statement: if ...:
pass
else:
if:
... Rather than the nested There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this is more complicated than I thought, namely because you don't know where the else is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this can be done via the text method...is that ok? It feels a bit...dirty... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah it's ok. The alternative would be to do it by lexing and looking at the token stream. But this is effectively equivalent. There's no way to detect this AFAIK from the AST alone. |
||
if let [first, ..] = &orelse[..] { | ||
if matches!(first.node, StmtKind::If { .. }) { | ||
// check the source if this is else then if or elif | ||
// we do this to see if they are on the same line | ||
if stmt.location.row() != first.location.row() { | ||
return Some(Diagnostic::new( | ||
ElseIfUsed {}, | ||
identifier_range(stmt, locator), | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
None | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
source: crates/ruff/src/rules/pylint/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
ElseIfUsed: {} | ||
location: | ||
row: 6 | ||
column: 4 | ||
end_location: | ||
row: 9 | ||
column: 12 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
ElseIfUsed: {} | ||
location: | ||
row: 24 | ||
column: 4 | ||
end_location: | ||
row: 29 | ||
column: 15 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
ElseIfUsed: {} | ||
location: | ||
row: 33 | ||
column: 4 | ||
end_location: | ||
row: 37 | ||
column: 16 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
ElseIfUsed: {} | ||
location: | ||
row: 41 | ||
column: 4 | ||
end_location: | ||
row: 47 | ||
column: 16 | ||
fix: ~ | ||
parent: ~ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this is being flagged as an error in the fixture, when it should be considered ok.