|
1 | | -use ruff_diagnostics::{Edit, Fix}; |
| 1 | +use ruff_diagnostics::Applicability; |
2 | 2 | use ruff_macros::{ViolationMetadata, derive_message_formats}; |
3 | 3 | use ruff_python_ast::ExprCall; |
4 | | -use ruff_text_size::Ranged; |
5 | 4 |
|
6 | 5 | use crate::checkers::ast::Checker; |
7 | | -use crate::importer::ImportRequest; |
8 | 6 | use crate::preview::is_fix_os_path_abspath_enabled; |
9 | 7 | use crate::rules::flake8_use_pathlib::helpers::{ |
10 | | - has_unknown_keywords_or_starred_expr, is_pathlib_path_call, |
| 8 | + check_os_pathlib_single_arg_calls, has_unknown_keywords_or_starred_expr, |
11 | 9 | }; |
12 | 10 | use crate::{FixAvailability, Violation}; |
13 | 11 |
|
@@ -75,43 +73,17 @@ pub(crate) fn os_path_abspath(checker: &Checker, call: &ExprCall, segments: &[&s |
75 | 73 | return; |
76 | 74 | } |
77 | 75 |
|
78 | | - if call.arguments.len() != 1 { |
79 | | - return; |
80 | | - } |
81 | | - |
82 | | - let Some(arg) = call.arguments.find_argument_value("path", 0) else { |
83 | | - return; |
84 | | - }; |
85 | | - |
86 | | - let arg_code = checker.locator().slice(arg.range()); |
87 | | - let range = call.range(); |
88 | | - |
89 | | - let mut diagnostic = checker.report_diagnostic(OsPathAbspath, call.func.range()); |
90 | | - |
91 | 76 | if has_unknown_keywords_or_starred_expr(&call.arguments, &["path"]) { |
92 | 77 | return; |
93 | 78 | } |
94 | 79 |
|
95 | | - if !is_fix_os_path_abspath_enabled(checker.settings()) { |
96 | | - return; |
97 | | - } |
98 | | - |
99 | | - diagnostic.try_set_fix(|| { |
100 | | - let (import_edit, binding) = checker.importer().get_or_import_symbol( |
101 | | - &ImportRequest::import("pathlib", "Path"), |
102 | | - call.start(), |
103 | | - checker.semantic(), |
104 | | - )?; |
105 | | - |
106 | | - let replacement = if is_pathlib_path_call(checker, arg) { |
107 | | - format!("{arg_code}.resolve()") |
108 | | - } else { |
109 | | - format!("{binding}({arg_code}).resolve()") |
110 | | - }; |
111 | | - |
112 | | - Ok(Fix::unsafe_edits( |
113 | | - Edit::range_replacement(replacement, range), |
114 | | - [import_edit], |
115 | | - )) |
116 | | - }); |
| 80 | + check_os_pathlib_single_arg_calls( |
| 81 | + checker, |
| 82 | + call, |
| 83 | + "resolve()", |
| 84 | + "path", |
| 85 | + is_fix_os_path_abspath_enabled(checker.settings()), |
| 86 | + OsPathAbspath, |
| 87 | + Some(Applicability::Unsafe), |
| 88 | + ); |
117 | 89 | } |
0 commit comments