From 8bf799566d0df8a5204a0a621e7cef867499700e Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 22 Aug 2023 14:31:00 -0400 Subject: [PATCH] Add --- .../test/fixtures/flake8_return/RET503.py | 6 ++++++ .../ruff/src/rules/flake8_return/helpers.rs | 18 +++++------------- ...lake8_return__tests__RET503_RET503.py.snap | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/flake8_return/RET503.py b/crates/ruff/resources/test/fixtures/flake8_return/RET503.py index 77d59cd8d48e9..9ec90617d31c4 100644 --- a/crates/ruff/resources/test/fixtures/flake8_return/RET503.py +++ b/crates/ruff/resources/test/fixtures/flake8_return/RET503.py @@ -320,3 +320,9 @@ def example(): if True: return "" \ ; # type: ignore + + +def end_of_file(): + if False: + return 1 + x = 2 \ diff --git a/crates/ruff/src/rules/flake8_return/helpers.rs b/crates/ruff/src/rules/flake8_return/helpers.rs index ab2053c73f458..a24cae600ff23 100644 --- a/crates/ruff/src/rules/flake8_return/helpers.rs +++ b/crates/ruff/src/rules/flake8_return/helpers.rs @@ -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() } diff --git a/crates/ruff/src/rules/flake8_return/snapshots/ruff__rules__flake8_return__tests__RET503_RET503.py.snap b/crates/ruff/src/rules/flake8_return/snapshots/ruff__rules__flake8_return__tests__RET503_RET503.py.snap index 8c8fd2cb46eb0..9627ff3dca666 100644 --- a/crates/ruff/src/rules/flake8_return/snapshots/ruff__rules__flake8_return__tests__RET503_RET503.py.snap +++ b/crates/ruff/src/rules/flake8_return/snapshots/ruff__rules__flake8_return__tests__RET503_RET503.py.snap @@ -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