|
| 1 | +use ruff_diagnostics::{Applicability, Diagnostic, Edit, Fix, FixAvailability, Violation}; |
| 2 | +use ruff_macros::{derive_message_formats, violation}; |
| 3 | +use ruff_python_ast::{Expr, ExprNoneLiteral}; |
| 4 | +use ruff_python_semantic::analyze::typing::traverse_literal; |
| 5 | +use ruff_text_size::Ranged; |
| 6 | + |
| 7 | +use smallvec::SmallVec; |
| 8 | + |
| 9 | +use crate::checkers::ast::Checker; |
| 10 | + |
| 11 | +/// ## What it does |
| 12 | +/// Checks for redundant `Literal[None]` annotations. |
| 13 | +/// |
| 14 | +/// ## Why is this bad? |
| 15 | +/// While `Literal[None]` is a valid type annotation, it is semantically equivalent to `None`. |
| 16 | +/// Prefer `None` over `Literal[None]` for both consistency and readability. |
| 17 | +/// |
| 18 | +/// ## Example |
| 19 | +/// ```python |
| 20 | +/// from typing import Literal |
| 21 | +/// |
| 22 | +/// Literal[None] |
| 23 | +/// Literal[1, 2, 3, "foo", 5, None] |
| 24 | +/// ``` |
| 25 | +/// |
| 26 | +/// Use instead: |
| 27 | +/// ```python |
| 28 | +/// from typing import Literal |
| 29 | +/// |
| 30 | +/// None |
| 31 | +/// Literal[1, 2, 3, "foo", 5] | None |
| 32 | +/// ``` |
| 33 | +/// |
| 34 | +/// ## References |
| 35 | +/// - [Typing documentation: Legal parameters for `Literal` at type check time](https://typing.readthedocs.io/en/latest/spec/literal.html#legal-parameters-for-literal-at-type-check-time) |
| 36 | +#[violation] |
| 37 | +pub struct RedundantNoneLiteral { |
| 38 | + other_literal_elements_seen: bool, |
| 39 | +} |
| 40 | + |
| 41 | +impl Violation for RedundantNoneLiteral { |
| 42 | + const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes; |
| 43 | + |
| 44 | + #[derive_message_formats] |
| 45 | + fn message(&self) -> String { |
| 46 | + if self.other_literal_elements_seen { |
| 47 | + "`Literal[None, ...]` can be replaced with `Literal[...] | None`".to_string() |
| 48 | + } else { |
| 49 | + "`Literal[None]` can be replaced with `None`".to_string() |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + fn fix_title(&self) -> Option<String> { |
| 54 | + Some(if self.other_literal_elements_seen { |
| 55 | + "Replace with `Literal[...] | None`".to_string() |
| 56 | + } else { |
| 57 | + "Replace with `None`".to_string() |
| 58 | + }) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/// RUF037 |
| 63 | +pub(crate) fn redundant_none_literal<'a>(checker: &mut Checker, literal_expr: &'a Expr) { |
| 64 | + if !checker.semantic().seen_typing() { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + let mut none_exprs: SmallVec<[&ExprNoneLiteral; 1]> = SmallVec::new(); |
| 69 | + let mut other_literal_elements_seen = false; |
| 70 | + |
| 71 | + let mut find_none = |expr: &'a Expr, _parent: &'a Expr| { |
| 72 | + if let Expr::NoneLiteral(none_expr) = expr { |
| 73 | + none_exprs.push(none_expr); |
| 74 | + } else { |
| 75 | + other_literal_elements_seen = true; |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + traverse_literal(&mut find_none, checker.semantic(), literal_expr); |
| 80 | + |
| 81 | + if none_exprs.is_empty() { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + // Provide a [`Fix`] when the complete `Literal` can be replaced. Applying the fix |
| 86 | + // can leave an unused import to be fixed by the `unused-import` rule. |
| 87 | + let fix = if other_literal_elements_seen { |
| 88 | + None |
| 89 | + } else { |
| 90 | + Some(Fix::applicable_edit( |
| 91 | + Edit::range_replacement("None".to_string(), literal_expr.range()), |
| 92 | + if checker.comment_ranges().intersects(literal_expr.range()) { |
| 93 | + Applicability::Unsafe |
| 94 | + } else { |
| 95 | + Applicability::Safe |
| 96 | + }, |
| 97 | + )) |
| 98 | + }; |
| 99 | + |
| 100 | + for none_expr in none_exprs { |
| 101 | + let mut diagnostic = Diagnostic::new( |
| 102 | + RedundantNoneLiteral { |
| 103 | + other_literal_elements_seen, |
| 104 | + }, |
| 105 | + none_expr.range(), |
| 106 | + ); |
| 107 | + if let Some(ref fix) = fix { |
| 108 | + diagnostic.set_fix(fix.clone()); |
| 109 | + } |
| 110 | + checker.diagnostics.push(diagnostic); |
| 111 | + } |
| 112 | +} |
0 commit comments