|
| 1 | +use ruff_diagnostics::{Diagnostic, Violation}; |
| 2 | +use ruff_macros::{derive_message_formats, violation}; |
| 3 | +use ruff_python_ast::helpers::ReturnStatementVisitor; |
| 4 | +use ruff_python_ast::identifier::Identifier; |
| 5 | +use ruff_python_ast::visitor::Visitor; |
| 6 | +use ruff_python_ast::{self as ast}; |
| 7 | +use ruff_python_semantic::analyze::function_type::is_stub; |
| 8 | +use ruff_python_semantic::analyze::terminal::Terminal; |
| 9 | +use ruff_python_semantic::analyze::type_inference::{NumberLike, PythonType, ResolvedPythonType}; |
| 10 | +use ruff_text_size::Ranged; |
| 11 | + |
| 12 | +use crate::checkers::ast::Checker; |
| 13 | + |
| 14 | +/// ## What it does |
| 15 | +/// Checks for `__hash__` implementations that return a type other than `integer`. |
| 16 | +/// |
| 17 | +/// ## Why is this bad? |
| 18 | +/// The `__hash__` method should return an `integer`. Returning a different |
| 19 | +/// type may cause unexpected behavior. |
| 20 | +/// |
| 21 | +/// Note: `bool` is a subclass of `int`, so it's technically valid for `__hash__` to |
| 22 | +/// return `True` or `False`. However, for consistency with other rules, Ruff will |
| 23 | +/// still raise when `__hash__` returns a `bool`. |
| 24 | +/// |
| 25 | +/// ## Example |
| 26 | +/// ```python |
| 27 | +/// class Foo: |
| 28 | +/// def __hash__(self): |
| 29 | +/// return "2" |
| 30 | +/// ``` |
| 31 | +/// |
| 32 | +/// Use instead: |
| 33 | +/// ```python |
| 34 | +/// class Foo: |
| 35 | +/// def __hash__(self): |
| 36 | +/// return 2 |
| 37 | +/// ``` |
| 38 | +/// |
| 39 | +/// |
| 40 | +/// ## References |
| 41 | +/// - [Python documentation: The `__hash__` method](https://docs.python.org/3/reference/datamodel.html#object.__hash__) |
| 42 | +#[violation] |
| 43 | +pub struct InvalidHashReturnType; |
| 44 | + |
| 45 | +impl Violation for InvalidHashReturnType { |
| 46 | + #[derive_message_formats] |
| 47 | + fn message(&self) -> String { |
| 48 | + format!("`__hash__` does not return an integer") |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/// E0309 |
| 53 | +pub(crate) fn invalid_hash_return(checker: &mut Checker, function_def: &ast::StmtFunctionDef) { |
| 54 | + if function_def.name.as_str() != "__hash__" { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if !checker.semantic().current_scope().kind.is_class() { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + if is_stub(function_def, checker.semantic()) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + // Determine the terminal behavior (i.e., implicit return, no return, etc.). |
| 67 | + let terminal = Terminal::from_function(function_def); |
| 68 | + |
| 69 | + // If every control flow path raises an exception, ignore the function. |
| 70 | + if terminal == Terminal::Raise { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + // If there are no return statements, add a diagnostic. |
| 75 | + if terminal == Terminal::Implicit { |
| 76 | + checker.diagnostics.push(Diagnostic::new( |
| 77 | + InvalidHashReturnType, |
| 78 | + function_def.identifier(), |
| 79 | + )); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + let returns = { |
| 84 | + let mut visitor = ReturnStatementVisitor::default(); |
| 85 | + visitor.visit_body(&function_def.body); |
| 86 | + visitor.returns |
| 87 | + }; |
| 88 | + |
| 89 | + for stmt in returns { |
| 90 | + if let Some(value) = stmt.value.as_deref() { |
| 91 | + if !matches!( |
| 92 | + ResolvedPythonType::from(value), |
| 93 | + ResolvedPythonType::Unknown |
| 94 | + | ResolvedPythonType::Atom(PythonType::Number(NumberLike::Integer)) |
| 95 | + ) { |
| 96 | + checker |
| 97 | + .diagnostics |
| 98 | + .push(Diagnostic::new(InvalidHashReturnType, value.range())); |
| 99 | + } |
| 100 | + } else { |
| 101 | + // Disallow implicit `None`. |
| 102 | + checker |
| 103 | + .diagnostics |
| 104 | + .push(Diagnostic::new(InvalidHashReturnType, stmt.range())); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments