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

Fallback to end-of-file if ends in trailing continuation #6789

Merged
merged 1 commit into from
Aug 22, 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
6 changes: 6 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_return/RET503.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,9 @@ def example():
if True:
return "" \
; # type: ignore


def end_of_file():
if False:
return 1
x = 2 \
18 changes: 5 additions & 13 deletions crates/ruff/src/rules/flake8_return/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,11 @@ pub(super) fn result_exists(returns: &[&ast::StmtReturn]) -> bool {
/// This method assumes that the statement is the last statement in its body; specifically, that
/// the statement isn't followed by a semicolon, followed by a multi-line statement.
pub(super) fn end_of_last_statement(stmt: &Stmt, locator: &Locator) -> TextSize {
if stmt.end() == locator.text_len() {
// End-of-file, so just return the end of the statement.
stmt.end()
} else {
// Otherwise, find the end of the last line that's "part of" the statement.
let contents = locator.after(stmt.end());

for line in contents.universal_newlines() {
if !line.ends_with('\\') {
return stmt.end() + line.end();
}
// Find the end of the last line that's "part of" the statement.
for line in locator.after(stmt.end()).universal_newlines() {
if !line.ends_with('\\') {
return stmt.end() + line.end();
}

unreachable!("Expected to find end-of-statement")
}
locator.text_len()
}
Original file line number Diff line number Diff line change
Expand Up @@ -380,5 +380,24 @@ RET503.py:320:9: RET503 [*] Missing explicit `return` at the end of function abl
321 321 | return "" \
322 322 | ; # type: ignore
323 |+ return None
323 324 |
324 325 |
325 326 | def end_of_file():

RET503.py:328:5: RET503 [*] Missing explicit `return` at the end of function able to return non-`None` value
|
326 | if False:
327 | return 1
328 | x = 2 \
| ^^^^^ RET503
|
= help: Add explicit `return` statement

ℹ Suggested fix
326 326 | if False:
327 327 | return 1
328 328 | x = 2 \
329 |+
330 |+ return None