From 102b9d930f05c533d9ff610859ae4b782e18d43f Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Thu, 23 May 2024 16:49:08 +0530 Subject: [PATCH] Use `Importer` available on `Checker` (#11513) ## Summary This PR updates the `FA102` rule logic to use the `Importer` which is available on the `Checker`. The main motivation is that this would make updating the `Importer` to use the `Tokens` struct which will be required to remove the `lex_starts_at` usage in `Insertion::start_of_block` method. ## Test Plan `cargo insta test` --- .../rules/future_required_type_annotation.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/crates/ruff_linter/src/rules/flake8_future_annotations/rules/future_required_type_annotation.rs b/crates/ruff_linter/src/rules/flake8_future_annotations/rules/future_required_type_annotation.rs index 0a373e03fd..11643f5a9f 100644 --- a/crates/ruff_linter/src/rules/flake8_future_annotations/rules/future_required_type_annotation.rs +++ b/crates/ruff_linter/src/rules/flake8_future_annotations/rules/future_required_type_annotation.rs @@ -7,7 +7,6 @@ use ruff_python_ast::Expr; use ruff_text_size::{Ranged, TextSize}; use crate::checkers::ast::Checker; -use crate::importer::Importer; /// ## What it does /// Checks for uses of PEP 585- and PEP 604-style type annotations in Python @@ -87,13 +86,11 @@ impl AlwaysFixableViolation for FutureRequiredTypeAnnotation { /// FA102 pub(crate) fn future_required_type_annotation(checker: &mut Checker, expr: &Expr, reason: Reason) { let mut diagnostic = Diagnostic::new(FutureRequiredTypeAnnotation { reason }, expr.range()); - if let Some(python_ast) = checker.semantic().definitions.python_ast() { - let required_import = - AnyImport::ImportFrom(ImportFrom::member("__future__", "annotations")); - diagnostic.set_fix(Fix::unsafe_edit( - Importer::new(python_ast, checker.locator(), checker.stylist()) - .add_import(&required_import, TextSize::default()), - )); - } + let required_import = AnyImport::ImportFrom(ImportFrom::member("__future__", "annotations")); + diagnostic.set_fix(Fix::unsafe_edit( + checker + .importer() + .add_import(&required_import, TextSize::default()), + )); checker.diagnostics.push(diagnostic); }