Skip to content
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

Avoid emitting empty logical lines #4452

Merged
merged 1 commit into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions crates/ruff/resources/test/fixtures/pycodestyle/E11.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,18 @@ def start():
#:
if False: #
print()
#:
if False:
print()

print()
#:
if False:
print()
if False:

print()
#:
if False:

print()
charliermarsh marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 15 additions & 0 deletions crates/ruff/src/checkers/logical_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,20 @@ f()"#
"f()",
];
assert_eq!(actual, expected);

let contents = r#"
if False:

print()
"#
.trim();
let lxr: Vec<LexResult> = lexer::lex(contents, Mode::Module).collect();
let locator = Locator::new(contents);
let actual: Vec<String> = LogicalLines::from_tokens(&lxr, &locator)
.into_iter()
.map(|line| line.text_trimmed().to_string())
.collect();
let expected = vec!["if False:", "print()", ""];
assert_eq!(actual, expected);
Comment on lines +253 to +267
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Can we split this into its own test. Giving tests an explicit name help future us because it gives another hint on what the test is asserting and also allows to run the test on its own.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gonna do this as its own PR.

}
}
15 changes: 10 additions & 5 deletions crates/ruff/src/rules/pycodestyle/rules/logical_lines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,16 @@ impl LogicalLinesBuilder {
fn finish_line(&mut self) {
let end = self.tokens.len() as u32;
if self.current_line.tokens_start < end {
self.lines.push(Line {
flags: self.current_line.flags,
tokens_start: self.current_line.tokens_start,
tokens_end: end,
});
let is_empty = self.tokens[self.current_line.tokens_start as usize..end as usize]
.iter()
.all(|token| token.kind.is_newline());
if !is_empty {
self.lines.push(Line {
flags: self.current_line.flags,
tokens_start: self.current_line.tokens_start,
tokens_end: end,
});
}

self.current_line = CurrentLine {
flags: TokenFlags::default(),
Expand Down
5 changes: 5 additions & 0 deletions crates/ruff_python_ast/src/token_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ pub enum TokenKind {
}

impl TokenKind {
#[inline]
pub const fn is_newline(&self) -> bool {
matches!(self, TokenKind::Newline | TokenKind::NonLogicalNewline)
}

#[inline]
pub const fn is_unary(&self) -> bool {
matches!(self, TokenKind::Plus | TokenKind::Minus | TokenKind::Star)
Expand Down