|
| 1 | +use ruff_diagnostics::{Diagnostic, Violation}; |
| 2 | +use ruff_macros::{derive_message_formats, violation}; |
| 3 | +use ruff_python_ast::Expr; |
| 4 | +use ruff_python_semantic::analyze::typing::traverse_union; |
| 5 | +use ruff_text_size::Ranged; |
| 6 | +use smallvec::SmallVec; |
| 7 | + |
| 8 | +use crate::checkers::ast::Checker; |
| 9 | + |
| 10 | +/// ## What it does |
| 11 | +/// Checks for type annotations where `None` is not at the end of an union. |
| 12 | +/// |
| 13 | +/// ## Why is this bad? |
| 14 | +/// Type annotation unions are associative, meaning that the order of the elements |
| 15 | +/// does not matter. The `None` literal represents the absence of a value. For |
| 16 | +/// readability, it's preferred to write the more informative type expressions first. |
| 17 | +/// |
| 18 | +/// ## Example |
| 19 | +/// ```python |
| 20 | +/// def func(arg: None | int): ... |
| 21 | +/// ``` |
| 22 | +/// |
| 23 | +/// Use instead: |
| 24 | +/// ```python |
| 25 | +/// def func(arg: int | None): ... |
| 26 | +/// ``` |
| 27 | +/// |
| 28 | +/// ## References |
| 29 | +/// - [Python documentation: Union type](https://docs.python.org/3/library/stdtypes.html#types-union) |
| 30 | +/// - [Python documentation: `typing.Optional`](https://docs.python.org/3/library/typing.html#typing.Optional) |
| 31 | +/// - [Python documentation: `None`](https://docs.python.org/3/library/constants.html#None) |
| 32 | +#[violation] |
| 33 | +pub struct NoneNotAtEndOfUnion; |
| 34 | + |
| 35 | +impl Violation for NoneNotAtEndOfUnion { |
| 36 | + #[derive_message_formats] |
| 37 | + fn message(&self) -> String { |
| 38 | + "`None` not at the end of the type annotation.".to_string() |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/// RUF036 |
| 43 | +pub(crate) fn none_not_at_end_of_union<'a>(checker: &mut Checker, union: &'a Expr) { |
| 44 | + let semantic = checker.semantic(); |
| 45 | + let mut none_exprs: SmallVec<[&Expr; 1]> = SmallVec::new(); |
| 46 | + |
| 47 | + let mut last_expr: Option<&Expr> = None; |
| 48 | + let mut find_none = |expr: &'a Expr, _parent: &Expr| { |
| 49 | + if matches!(expr, Expr::NoneLiteral(_)) { |
| 50 | + none_exprs.push(expr); |
| 51 | + } |
| 52 | + last_expr = Some(expr); |
| 53 | + }; |
| 54 | + |
| 55 | + // Walk through all type expressions in the union and keep track of `None` literals. |
| 56 | + traverse_union(&mut find_none, semantic, union); |
| 57 | + |
| 58 | + let Some(last_expr) = last_expr else { |
| 59 | + return; |
| 60 | + }; |
| 61 | + |
| 62 | + // The must be at least one `None` expression. |
| 63 | + let Some(last_none) = none_exprs.last() else { |
| 64 | + return; |
| 65 | + }; |
| 66 | + |
| 67 | + // If any of the `None` literals is last we do not emit. |
| 68 | + if *last_none == last_expr { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + for none_expr in none_exprs { |
| 73 | + checker |
| 74 | + .diagnostics |
| 75 | + .push(Diagnostic::new(NoneNotAtEndOfUnion, none_expr.range())); |
| 76 | + } |
| 77 | +} |
0 commit comments