Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion crates/ruff_linter/src/rules/ruff/rules/os_path_commonprefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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()
Expand All @@ -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]))
});
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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"])
Expand All @@ -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