Skip to content

Commit cb3c3ba

Browse files
authored
[flake8-use-pathlib] A bit clean up PTH100 (#20452)
## Summary Part of #20215 ## Test Plan
1 parent c585c9f commit cb3c3ba

File tree

1 file changed

+11
-39
lines changed

1 file changed

+11
-39
lines changed

crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_abspath.rs

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
use ruff_diagnostics::{Edit, Fix};
1+
use ruff_diagnostics::Applicability;
22
use ruff_macros::{ViolationMetadata, derive_message_formats};
33
use ruff_python_ast::ExprCall;
4-
use ruff_text_size::Ranged;
54

65
use crate::checkers::ast::Checker;
7-
use crate::importer::ImportRequest;
86
use crate::preview::is_fix_os_path_abspath_enabled;
97
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,
119
};
1210
use crate::{FixAvailability, Violation};
1311

@@ -75,43 +73,17 @@ pub(crate) fn os_path_abspath(checker: &Checker, call: &ExprCall, segments: &[&s
7573
return;
7674
}
7775

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-
9176
if has_unknown_keywords_or_starred_expr(&call.arguments, &["path"]) {
9277
return;
9378
}
9479

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+
);
11789
}

0 commit comments

Comments
 (0)