diff --git a/crates/ruff_linter/src/rules/ruff/rules/os_path_commonprefix.rs b/crates/ruff_linter/src/rules/ruff/rules/os_path_commonprefix.rs index 2084acce10d570..a1936be192e28d 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/os_path_commonprefix.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/os_path_commonprefix.rs @@ -2,8 +2,9 @@ use ruff_macros::{ViolationMetadata, derive_message_formats}; use ruff_python_ast as ast; use ruff_text_size::Ranged; -use crate::Violation; use crate::checkers::ast::Checker; +use crate::importer::ImportRequest; +use crate::{Edit, Fix, FixAvailability, Violation}; /// ## What it does /// Checks for uses of `os.path.commonprefix`. @@ -44,6 +45,8 @@ use crate::checkers::ast::Checker; pub(crate) struct OsPathCommonprefix; impl Violation for OsPathCommonprefix { + const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes; + #[derive_message_formats] fn message(&self) -> String { "`os.path.commonprefix()` compares strings character-by-character".to_string() @@ -61,4 +64,14 @@ pub(crate) fn os_path_commonprefix(checker: &Checker, call: &ast::ExprCall, segm } let mut diagnostic = checker.report_diagnostic(OsPathCommonprefix, call.func.range()); diagnostic.add_primary_tag(ruff_db::diagnostic::DiagnosticTag::Deprecated); + + diagnostic.try_set_fix(|| { + let (import_edit, binding) = checker.importer().get_or_import_symbol( + &ImportRequest::import_from("os.path", "commonpath"), + call.func.start(), + checker.semantic(), + )?; + let reference_edit = Edit::range_replacement(binding, call.func.range()); + Ok(Fix::unsafe_edits(import_edit, [reference_edit])) + }); } diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF071_RUF071.py.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF071_RUF071.py.snap index 7a06011a6e43ed..a01c3520a01ebe 100644 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF071_RUF071.py.snap +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__preview__RUF071_RUF071.py.snap @@ -1,7 +1,7 @@ --- source: crates/ruff_linter/src/rules/ruff/mod.rs --- -RUF071 `os.path.commonprefix()` compares strings character-by-character +RUF071 [*] `os.path.commonprefix()` compares strings character-by-character --> RUF071.py:7:1 | 6 | # Errors @@ -11,8 +11,17 @@ RUF071 `os.path.commonprefix()` compares strings character-by-character 9 | path.commonprefix(["/usr/lib", "/usr/local/lib"]) | help: Use `os.path.commonpath()` to compare path components +4 | from os import path +5 | +6 | # Errors + - os.path.commonprefix(["/usr/lib", "/usr/local/lib"]) +7 + os.path.commonpath(["/usr/lib", "/usr/local/lib"]) +8 | commonprefix(["/usr/lib", "/usr/local/lib"]) +9 | path.commonprefix(["/usr/lib", "/usr/local/lib"]) +10 | +note: This is an unsafe fix and may change runtime behavior -RUF071 `os.path.commonprefix()` compares strings character-by-character +RUF071 [*] `os.path.commonprefix()` compares strings character-by-character --> RUF071.py:8:1 | 6 | # Errors @@ -22,8 +31,17 @@ RUF071 `os.path.commonprefix()` compares strings character-by-character 9 | path.commonprefix(["/usr/lib", "/usr/local/lib"]) | help: Use `os.path.commonpath()` to compare path components +5 | +6 | # Errors +7 | os.path.commonprefix(["/usr/lib", "/usr/local/lib"]) + - commonprefix(["/usr/lib", "/usr/local/lib"]) +8 + os.path.commonpath(["/usr/lib", "/usr/local/lib"]) +9 | path.commonprefix(["/usr/lib", "/usr/local/lib"]) +10 | +11 | # OK +note: This is an unsafe fix and may change runtime behavior -RUF071 `os.path.commonprefix()` compares strings character-by-character +RUF071 [*] `os.path.commonprefix()` compares strings character-by-character --> RUF071.py:9:1 | 7 | os.path.commonprefix(["/usr/lib", "/usr/local/lib"]) @@ -34,3 +52,12 @@ RUF071 `os.path.commonprefix()` compares strings character-by-character 11 | # OK | help: Use `os.path.commonpath()` to compare path components +6 | # Errors +7 | os.path.commonprefix(["/usr/lib", "/usr/local/lib"]) +8 | commonprefix(["/usr/lib", "/usr/local/lib"]) + - path.commonprefix(["/usr/lib", "/usr/local/lib"]) +9 + os.path.commonpath(["/usr/lib", "/usr/local/lib"]) +10 | +11 | # OK +12 | os.path.commonpath(["/usr/lib", "/usr/local/lib"]) +note: This is an unsafe fix and may change runtime behavior