From 4cf2d4e7fdb28bbbeb2a56e546f23bec540e3da9 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 16 Nov 2024 13:29:14 -0500 Subject: [PATCH] Nits --- .../ruff/rules/redundant_bool_literal.rs | 27 ++++++++++--------- ..._rules__ruff__tests__RUF038_RUF038.py.snap | 10 +++---- ...rules__ruff__tests__RUF038_RUF038.pyi.snap | 10 +++---- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/crates/ruff_linter/src/rules/ruff/rules/redundant_bool_literal.rs b/crates/ruff_linter/src/rules/ruff/rules/redundant_bool_literal.rs index efeef1c28795f..884bab83fc28d 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/redundant_bool_literal.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/redundant_bool_literal.rs @@ -51,9 +51,9 @@ use crate::checkers::ast::Checker; /// - [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) /// - [Python documentation: Boolean type - `bool`](https://docs.python.org/3/library/stdtypes.html#boolean-type-bool) /// -/// [mypy](https://github.com/python/mypy/blob/master/mypy/typeops.py#L985) -/// [#14764](https://github.com/python/mypy/issues/14764) -/// [#5421](https://github.com/microsoft/pyright/issues/5421) +/// [mypy]: https://github.com/python/mypy/blob/master/mypy/typeops.py#L985 +/// [#14764]: https://github.com/python/mypy/issues/14764 +/// [#5421]: https://github.com/microsoft/pyright/issues/5421 #[violation] pub struct RedundantBoolLiteral { seen_others: bool, @@ -67,17 +67,16 @@ impl Violation for RedundantBoolLiteral { if self.seen_others { "`Literal[True, False, ...]` can be replaced with `Literal[...] | bool`".to_string() } else { - "`Literal[True, False]` can be replaced with a bare `bool`".to_string() + "`Literal[True, False]` can be replaced with `bool`".to_string() } } fn fix_title(&self) -> Option { - let title = if self.seen_others { - "Replace with `Literal[...] | bool`" + Some(if self.seen_others { + "Replace with `Literal[...] | bool`".to_string() } else { - "Replace with `bool`" - }; - Some(title.to_string()) + "Replace with `bool`".to_string() + }) } } @@ -117,10 +116,12 @@ pub(crate) fn redundant_bool_literal<'a>(checker: &mut Checker, literal_expr: &' // Provide a [`Fix`] when the complete `Literal` can be replaced. Applying the fix // can leave an unused import to be fixed by the `unused-import` rule. if !seen_others { - diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement( - "bool".to_string(), - literal_expr.range(), - ))); + if checker.semantic().has_builtin_binding("bool") { + diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement( + "bool".to_string(), + literal_expr.range(), + ))); + } } checker.diagnostics.push(diagnostic); diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF038_RUF038.py.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF038_RUF038.py.snap index d04ef809752b8..a5a0ffa3859d6 100644 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF038_RUF038.py.snap +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF038_RUF038.py.snap @@ -1,7 +1,7 @@ --- source: crates/ruff_linter/src/rules/ruff/mod.rs --- -RUF038.py:4:17: RUF038 [*] `Literal[True, False]` can be replaced with a bare `bool` +RUF038.py:4:17: RUF038 [*] `Literal[True, False]` can be replaced with `bool` | 4 | def func1(arg1: Literal[True, False]): | ^^^^^^^^^^^^^^^^^^^^ RUF038 @@ -19,7 +19,7 @@ RUF038.py:4:17: RUF038 [*] `Literal[True, False]` can be replaced with a bare `b 6 6 | 7 7 | -RUF038.py:8:17: RUF038 [*] `Literal[True, False]` can be replaced with a bare `bool` +RUF038.py:8:17: RUF038 [*] `Literal[True, False]` can be replaced with `bool` | 8 | def func2(arg1: Literal[True, False, True]): | ^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF038 @@ -37,7 +37,7 @@ RUF038.py:8:17: RUF038 [*] `Literal[True, False]` can be replaced with a bare `b 10 10 | 11 11 | -RUF038.py:12:16: RUF038 [*] `Literal[True, False]` can be replaced with a bare `bool` +RUF038.py:12:16: RUF038 [*] `Literal[True, False]` can be replaced with `bool` | 12 | def func3() -> Literal[True, False]: | ^^^^^^^^^^^^^^^^^^^^ RUF038 @@ -55,7 +55,7 @@ RUF038.py:12:16: RUF038 [*] `Literal[True, False]` can be replaced with a bare ` 14 14 | 15 15 | -RUF038.py:16:17: RUF038 [*] `Literal[True, False]` can be replaced with a bare `bool` +RUF038.py:16:17: RUF038 [*] `Literal[True, False]` can be replaced with `bool` | 16 | def func4(arg1: Literal[True, False] | bool): | ^^^^^^^^^^^^^^^^^^^^ RUF038 @@ -73,7 +73,7 @@ RUF038.py:16:17: RUF038 [*] `Literal[True, False]` can be replaced with a bare ` 18 18 | 19 19 | -RUF038.py:20:17: RUF038 [*] `Literal[True, False]` can be replaced with a bare `bool` +RUF038.py:20:17: RUF038 [*] `Literal[True, False]` can be replaced with `bool` | 20 | def func5(arg1: Literal[False, True]): | ^^^^^^^^^^^^^^^^^^^^ RUF038 diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF038_RUF038.pyi.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF038_RUF038.pyi.snap index 3844c74eb814f..d6d6d902ab299 100644 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF038_RUF038.pyi.snap +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF038_RUF038.pyi.snap @@ -1,7 +1,7 @@ --- source: crates/ruff_linter/src/rules/ruff/mod.rs --- -RUF038.pyi:4:17: RUF038 [*] `Literal[True, False]` can be replaced with a bare `bool` +RUF038.pyi:4:17: RUF038 [*] `Literal[True, False]` can be replaced with `bool` | 4 | def func1(arg1: Literal[True, False]): ... | ^^^^^^^^^^^^^^^^^^^^ RUF038 @@ -20,7 +20,7 @@ RUF038.pyi:4:17: RUF038 [*] `Literal[True, False]` can be replaced with a bare ` 6 6 | def func2(arg1: Literal[True, False, True]): ... 7 7 | -RUF038.pyi:6:17: RUF038 [*] `Literal[True, False]` can be replaced with a bare `bool` +RUF038.pyi:6:17: RUF038 [*] `Literal[True, False]` can be replaced with `bool` | 4 | def func1(arg1: Literal[True, False]): ... 5 | @@ -41,7 +41,7 @@ RUF038.pyi:6:17: RUF038 [*] `Literal[True, False]` can be replaced with a bare ` 8 8 | def func3() -> Literal[True, False]: ... 9 9 | -RUF038.pyi:8:16: RUF038 [*] `Literal[True, False]` can be replaced with a bare `bool` +RUF038.pyi:8:16: RUF038 [*] `Literal[True, False]` can be replaced with `bool` | 6 | def func2(arg1: Literal[True, False, True]): ... 7 | @@ -62,7 +62,7 @@ RUF038.pyi:8:16: RUF038 [*] `Literal[True, False]` can be replaced with a bare ` 10 10 | def func4(arg1: Literal[True, False] | bool): ... 11 11 | -RUF038.pyi:10:17: RUF038 [*] `Literal[True, False]` can be replaced with a bare `bool` +RUF038.pyi:10:17: RUF038 [*] `Literal[True, False]` can be replaced with `bool` | 8 | def func3() -> Literal[True, False]: ... 9 | @@ -83,7 +83,7 @@ RUF038.pyi:10:17: RUF038 [*] `Literal[True, False]` can be replaced with a bare 12 12 | def func5(arg1: Literal[False, True]): ... 13 13 | -RUF038.pyi:12:17: RUF038 [*] `Literal[True, False]` can be replaced with a bare `bool` +RUF038.pyi:12:17: RUF038 [*] `Literal[True, False]` can be replaced with `bool` | 10 | def func4(arg1: Literal[True, False] | bool): ... 11 |