From 57fab8153a805943f5e1664ecc460eb80996a9e0 Mon Sep 17 00:00:00 2001 From: Tibor Reiss Date: Tue, 16 Apr 2024 00:10:31 +0200 Subject: [PATCH] Formatting --- crates/ruff_linter/src/rules/pylint/mod.rs | 5 ++++- .../src/rules/pylint/rules/invalid_length_return.rs | 13 ++++++------- ...ests__PLE0303_invalid_return_type_length.py.snap | 10 ++++++++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/crates/ruff_linter/src/rules/pylint/mod.rs b/crates/ruff_linter/src/rules/pylint/mod.rs index 26dae5ecce9577..52ab1ee7b0845f 100644 --- a/crates/ruff_linter/src/rules/pylint/mod.rs +++ b/crates/ruff_linter/src/rules/pylint/mod.rs @@ -77,7 +77,10 @@ mod tests { #[test_case(Rule::InvalidAllFormat, Path::new("invalid_all_format.py"))] #[test_case(Rule::InvalidAllObject, Path::new("invalid_all_object.py"))] #[test_case(Rule::InvalidBoolReturnType, Path::new("invalid_return_type_bool.py"))] - #[test_case(Rule::InvalidLengthReturnType, Path::new("invalid_return_type_length.py"))] + #[test_case( + Rule::InvalidLengthReturnType, + Path::new("invalid_return_type_length.py") + )] #[test_case(Rule::InvalidStrReturnType, Path::new("invalid_return_type_str.py"))] #[test_case(Rule::DuplicateBases, Path::new("duplicate_bases.py"))] #[test_case(Rule::InvalidCharacterBackspace, Path::new("invalid_characters.py"))] diff --git a/crates/ruff_linter/src/rules/pylint/rules/invalid_length_return.rs b/crates/ruff_linter/src/rules/pylint/rules/invalid_length_return.rs index 72b76451773e8a..5220d086afdd01 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/invalid_length_return.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/invalid_length_return.rs @@ -32,7 +32,6 @@ use crate::checkers::ast::Checker; /// Note: Strictly speaking `bool` is a subclass of `int`, thus returning `True`/`False` is valid. /// To be consistent with other rules (e.g. PLE0305 invalid-index-returned), ruff will raise, compared /// to pylint which will not raise. -/// /// ## References /// - [Python documentation: The `__len__` method](https://docs.python.org/3/reference/datamodel.html#object.__len__) #[violation] @@ -67,7 +66,8 @@ pub(crate) fn invalid_length_return(checker: &mut Checker, name: &str, body: &[S ResolvedPythonType::from(value), ResolvedPythonType::Unknown | ResolvedPythonType::Atom(PythonType::Number(NumberLike::Integer)) - ) || is_negative_integer(value) { + ) || is_negative_integer(value) + { checker .diagnostics .push(Diagnostic::new(InvalidLengthReturnType, value.range())); @@ -82,9 +82,8 @@ pub(crate) fn invalid_length_return(checker: &mut Checker, name: &str, body: &[S } fn is_negative_integer(value: &Expr) -> bool { - let Expr::UnaryOp(ast::ExprUnaryOp { op, .. }) = value else { return false; }; - match op { - ast::UnaryOp::USub => true, - _ => false, - } + let Expr::UnaryOp(ast::ExprUnaryOp { op, .. }) = value else { + return false; + }; + matches!(op, ast::UnaryOp::USub) } diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLE0303_invalid_return_type_length.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLE0303_invalid_return_type_length.py.snap index faf481c646606c..2a0f53da757c08 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLE0303_invalid_return_type_length.py.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLE0303_invalid_return_type_length.py.snap @@ -30,3 +30,13 @@ invalid_return_type_length.py:13:16: PLE0303 `__len__` does not return a non-neg 14 | 15 | class LengthNegative: | + +invalid_return_type_length.py:17:16: PLE0303 `__len__` does not return a non-negative `integer` + | +15 | class LengthNegative: +16 | def __len__(self): +17 | return -42 # [invalid-length-return] + | ^^^ PLE0303 +18 | +19 | # TODO: Once Ruff has better type checking + |