From f9e2d2b7353ab5cb95d4ecbce7aa4a9f2a5d7c0b Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 2 Jan 2024 22:14:46 -0500 Subject: [PATCH] Remove temporary files --- .../src/rules/flake8_annotations/helpers.rs | 5 +-- .../src/analyze/terminal.rs | 33 +++++++++++-------- foo.py | 7 ---- 3 files changed, 20 insertions(+), 25 deletions(-) delete mode 100644 foo.py diff --git a/crates/ruff_linter/src/rules/flake8_annotations/helpers.rs b/crates/ruff_linter/src/rules/flake8_annotations/helpers.rs index b7c0938cf25b5d..873b1859820c53 100644 --- a/crates/ruff_linter/src/rules/flake8_annotations/helpers.rs +++ b/crates/ruff_linter/src/rules/flake8_annotations/helpers.rs @@ -89,10 +89,7 @@ pub(crate) fn auto_return_type(function: &ast::StmtFunctionDef) -> Option 0: // return 1 // ``` - if terminal == Terminal::ConditionalReturn - || terminal == Terminal::None - || terminal == Terminal::Implicit - { + if terminal.has_implicit_return() { return_type = return_type.union(ResolvedPythonType::Atom(PythonType::None)); } diff --git a/crates/ruff_python_semantic/src/analyze/terminal.rs b/crates/ruff_python_semantic/src/analyze/terminal.rs index e3d6d7d1ec2cc8..9116225888d466 100644 --- a/crates/ruff_python_semantic/src/analyze/terminal.rs +++ b/crates/ruff_python_semantic/src/analyze/terminal.rs @@ -22,6 +22,19 @@ impl Terminal { Self::from_body(&function.body) } + /// Returns `true` if the [`Terminal`] behavior includes at least one `return` path. + pub fn has_any_return(self) -> bool { + matches!( + self, + Self::Return | Self::Explicit | Self::ConditionalReturn + ) + } + + /// Returns `true` if the [`Terminal`] behavior includes at least one implicit `return` path. + pub fn has_implicit_return(self) -> bool { + matches!(self, Self::None | Self::Implicit | Self::ConditionalReturn) + } + /// Returns the [`Terminal`] behavior of the body, if it can be determined. fn from_body(stmts: &[Stmt]) -> Terminal { let mut terminal = Terminal::None; @@ -58,7 +71,7 @@ impl Terminal { if elif_else_clauses.iter().any(|clause| clause.test.is_none()) { // And all branches return, then the `if` statement returns. terminal = terminal.and_then(branch_terminal); - } else if branch_terminal.has_return() { + } else if branch_terminal.has_any_return() { // Otherwise, if any branch returns, we know this can't be a // non-returning function. terminal = terminal.and_then(Terminal::ConditionalReturn); @@ -77,7 +90,7 @@ impl Terminal { } else { // Otherwise, if any branch returns, we know this can't be a // non-returning function. - if branch_terminal.has_return() { + if branch_terminal.has_any_return() { terminal = terminal.and_then(Terminal::ConditionalReturn); } } @@ -91,7 +104,7 @@ impl Terminal { }) => { // If the body returns, then this can't be a non-returning function. let body_terminal = Self::from_body(body); - if body_terminal.has_return() { + if body_terminal.has_any_return() { terminal = terminal.and_then(Terminal::ConditionalReturn); } @@ -109,7 +122,7 @@ impl Terminal { if orelse.is_empty() { // If there's no `else`, we may fall through. - if branch_terminal.has_return() { + if branch_terminal.has_any_return() { terminal = terminal.and_then(Terminal::ConditionalReturn); } } else { @@ -137,14 +150,6 @@ impl Terminal { } } - /// Returns `true` if the [`Terminal`] behavior includes at least one `return` path. - fn has_return(self) -> bool { - matches!( - self, - Self::Return | Self::Explicit | Self::ConditionalReturn - ) - } - /// Combine two [`Terminal`] operators, with one appearing after the other. fn and_then(self, other: Self) -> Self { match (self, other) { @@ -238,7 +243,7 @@ fn sometimes_breaks(stmts: &[Stmt]) -> bool { for stmt in stmts { match stmt { Stmt::For(ast::StmtFor { body, orelse, .. }) => { - if Terminal::from_body(body).has_return() { + if Terminal::from_body(body).has_any_return() { return false; } if sometimes_breaks(orelse) { @@ -246,7 +251,7 @@ fn sometimes_breaks(stmts: &[Stmt]) -> bool { } } Stmt::While(ast::StmtWhile { body, orelse, .. }) => { - if Terminal::from_body(body).has_return() { + if Terminal::from_body(body).has_any_return() { return false; } if sometimes_breaks(orelse) { diff --git a/foo.py b/foo.py deleted file mode 100644 index 870035d6c0ca11..00000000000000 --- a/foo.py +++ /dev/null @@ -1,7 +0,0 @@ -def func(x: int): - try: - return 1 - except: - return 2 - else: - pass