diff --git a/crates/ruff_linter/resources/test/fixtures/pylint/invalid_return_type_bytes.py b/crates/ruff_linter/resources/test/fixtures/pylint/invalid_return_type_bytes.py index cab11da110e6f..8f12af9346bac 100644 --- a/crates/ruff_linter/resources/test/fixtures/pylint/invalid_return_type_bytes.py +++ b/crates/ruff_linter/resources/test/fixtures/pylint/invalid_return_type_bytes.py @@ -12,6 +12,10 @@ class Str: def __bytes__(self): return "some bytes" # [invalid-bytes-return] +class BytesNoReturn: + def __bytes__(self): + print("ruff") # [invalid-bytes-return] + # TODO: Once Ruff has better type checking def return_bytes(): return "some string" diff --git a/crates/ruff_linter/src/rules/pylint/rules/invalid_bytes_return.rs b/crates/ruff_linter/src/rules/pylint/rules/invalid_bytes_return.rs index 28e2e1f0d021d..3040572663efb 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/invalid_bytes_return.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/invalid_bytes_return.rs @@ -57,6 +57,13 @@ pub(crate) fn invalid_bytes_return(checker: &mut Checker, name: &str, body: &[St visitor.returns }; + if returns.is_empty() { + checker.diagnostics.push(Diagnostic::new( + InvalidBytesReturnType, + body.last().unwrap().range(), + )); + } + for stmt in returns { if let Some(value) = stmt.value.as_deref() { if !matches!( diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLE0308_invalid_return_type_bytes.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLE0308_invalid_return_type_bytes.py.snap index 7da53c8db9c6f..333aa49fabfa6 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLE0308_invalid_return_type_bytes.py.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLE0308_invalid_return_type_bytes.py.snap @@ -28,5 +28,15 @@ invalid_return_type_bytes.py:13:16: PLE0308 `__bytes__` does not return `bytes` 13 | return "some bytes" # [invalid-bytes-return] | ^^^^^^^^^^^^ PLE0308 14 | -15 | # TODO: Once Ruff has better type checking +15 | class BytesNoReturn: + | + +invalid_return_type_bytes.py:17:9: PLE0308 `__bytes__` does not return `bytes` + | +15 | class BytesNoReturn: +16 | def __bytes__(self): +17 | print("ruff") # [invalid-bytes-return] + | ^^^^^^^^^^^^^ PLE0308 +18 | +19 | # TODO: Once Ruff has better type checking |