-
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.
Implement compound-statements (E701, E702, E703, E704) (#2680)
- Loading branch information
1 parent
5a07c9f
commit 739a92e
Showing
14 changed files
with
406 additions
and
30 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,25 @@ | ||
#: E701:1:5 | ||
if a: a = False | ||
#: E701:1:40 | ||
if not header or header[:6] != 'bytes=': return | ||
#: E702:1:10 | ||
a = False; b = True | ||
#: E702:1:17 | ||
import bdist_egg; bdist_egg.write_safety_flag(cmd.egg_info, safe) | ||
#: E703:1:13 | ||
import shlex; | ||
#: E702:1:9 E703:1:23 | ||
del a[:]; a.append(42); | ||
#: E704:1:1 | ||
def f(x): return 2 | ||
#: E704:1:1 | ||
async def f(x): return 2 | ||
#: E704:1:1 E271:1:6 | ||
async def f(x): return 2 | ||
#: E704:1:1 E226:1:19 | ||
def f(x): return 2*x | ||
#: E704:2:5 E226:2:23 | ||
while all is round: | ||
def f(x): return 2*x | ||
#: | ||
if True: x; y; |
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
143 changes: 143 additions & 0 deletions
143
crates/ruff/src/rules/pycodestyle/rules/compound_statements.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,143 @@ | ||
use rustpython_parser::lexer::{LexResult, Tok}; | ||
|
||
use ruff_macros::{define_violation, derive_message_formats}; | ||
|
||
use crate::ast::types::Range; | ||
use crate::registry::Diagnostic; | ||
use crate::violation::Violation; | ||
|
||
define_violation!( | ||
pub struct MultipleStatementsOnOneLineColon; | ||
); | ||
impl Violation for MultipleStatementsOnOneLineColon { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Multiple statements on one line (colon)") | ||
} | ||
} | ||
|
||
define_violation!( | ||
pub struct MultipleStatementsOnOneLineSemicolon; | ||
); | ||
impl Violation for MultipleStatementsOnOneLineSemicolon { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Multiple statements on one line (semicolon)") | ||
} | ||
} | ||
|
||
define_violation!( | ||
pub struct UselessSemicolon; | ||
); | ||
impl Violation for UselessSemicolon { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Statement ends with an unnecessary semicolon") | ||
} | ||
} | ||
|
||
define_violation!( | ||
pub struct MultipleStatementsOnOneLineDef; | ||
); | ||
impl Violation for MultipleStatementsOnOneLineDef { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Multiple statements on one line (def)") | ||
} | ||
} | ||
|
||
pub fn compound_statements(lxr: &[LexResult]) -> Vec<Diagnostic> { | ||
let mut diagnostics = vec![]; | ||
|
||
// Track the last seen instance of a variety of tokens. | ||
let mut def = None; | ||
let mut colon = None; | ||
let mut semi = None; | ||
|
||
// Track the bracket depth. | ||
let mut par_count = 0; | ||
let mut sqb_count = 0; | ||
let mut brace_count = 0; | ||
|
||
for &(start, ref tok, end) in lxr.iter().flatten() { | ||
match tok { | ||
Tok::Lpar => { | ||
par_count += 1; | ||
} | ||
Tok::Rpar => { | ||
par_count -= 1; | ||
} | ||
Tok::Lsqb => { | ||
sqb_count += 1; | ||
} | ||
Tok::Rsqb => { | ||
sqb_count -= 1; | ||
} | ||
Tok::Lbrace => { | ||
brace_count += 1; | ||
} | ||
Tok::Rbrace => { | ||
brace_count -= 1; | ||
} | ||
_ => {} | ||
} | ||
|
||
if par_count > 0 || sqb_count > 0 || brace_count > 0 { | ||
continue; | ||
} | ||
|
||
match tok { | ||
Tok::Newline => { | ||
if let Some((start, end)) = semi { | ||
diagnostics.push(Diagnostic::new(UselessSemicolon, Range::new(start, end))); | ||
} | ||
|
||
// Reset. | ||
def = None; | ||
colon = None; | ||
semi = None; | ||
} | ||
Tok::Def => { | ||
def = Some((start, end)); | ||
} | ||
Tok::Colon => { | ||
colon = Some((start, end)); | ||
} | ||
Tok::Semi => { | ||
semi = Some((start, end)); | ||
} | ||
Tok::Comment(..) | Tok::Indent | Tok::Dedent | Tok::NonLogicalNewline => {} | ||
_ => { | ||
if let Some((start, end)) = semi { | ||
diagnostics.push(Diagnostic::new( | ||
MultipleStatementsOnOneLineSemicolon, | ||
Range::new(start, end), | ||
)); | ||
|
||
// Reset. | ||
semi = None; | ||
} | ||
|
||
if let Some((start, end)) = colon { | ||
if let Some((start, end)) = def { | ||
diagnostics.push(Diagnostic::new( | ||
MultipleStatementsOnOneLineDef, | ||
Range::new(start, end), | ||
)); | ||
} else { | ||
diagnostics.push(Diagnostic::new( | ||
MultipleStatementsOnOneLineColon, | ||
Range::new(start, end), | ||
)); | ||
} | ||
|
||
// Reset. | ||
def = None; | ||
colon = None; | ||
} | ||
} | ||
} | ||
} | ||
|
||
diagnostics | ||
} |
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
35 changes: 35 additions & 0 deletions
35
...es/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E701_E70.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,35 @@ | ||
--- | ||
source: crates/ruff/src/rules/pycodestyle/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
MultipleStatementsOnOneLineColon: ~ | ||
location: | ||
row: 2 | ||
column: 4 | ||
end_location: | ||
row: 2 | ||
column: 5 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
MultipleStatementsOnOneLineColon: ~ | ||
location: | ||
row: 4 | ||
column: 39 | ||
end_location: | ||
row: 4 | ||
column: 40 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
MultipleStatementsOnOneLineColon: ~ | ||
location: | ||
row: 25 | ||
column: 7 | ||
end_location: | ||
row: 25 | ||
column: 8 | ||
fix: ~ | ||
parent: ~ | ||
|
Oops, something went wrong.