Skip to content

Commit

Permalink
[refurb] - fix unused autofix for implicit-cwd (FURB177) (#12708)
Browse files Browse the repository at this point in the history
  • Loading branch information
diceroll123 authored Aug 6, 2024
1 parent 52630a1 commit 39dd732
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 15 deletions.
12 changes: 8 additions & 4 deletions crates/ruff_linter/src/rules/refurb/rules/implicit_cwd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ruff_diagnostics::{Diagnostic, Edit, Fix, Violation};
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{self as ast, Expr, ExprAttribute, ExprCall};
use ruff_text_size::Ranged;
Expand Down Expand Up @@ -29,10 +29,16 @@ use crate::{checkers::ast::Checker, importer::ImportRequest};
pub struct ImplicitCwd;

impl Violation for ImplicitCwd {
const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes;

#[derive_message_formats]
fn message(&self) -> String {
format!("Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups")
}

fn fix_title(&self) -> Option<String> {
Some("Replace `Path().resolve()` with `Path.cwd()`".to_string())
}
}

/// FURB177
Expand Down Expand Up @@ -96,7 +102,5 @@ pub(crate) fn no_implicit_cwd(checker: &mut Checker, call: &ExprCall) {
))
});

checker
.diagnostics
.push(Diagnostic::new(ImplicitCwd, call.range()));
checker.diagnostics.push(diagnostic);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
---
source: crates/ruff_linter/src/rules/refurb/mod.rs
---
FURB177.py:5:5: FURB177 Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
FURB177.py:5:5: FURB177 [*] Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
|
4 | # Errors
5 | _ = Path().resolve()
| ^^^^^^^^^^^^^^^^ FURB177
6 | _ = pathlib.Path().resolve()
|
= help: Replace `Path().resolve()` with `Path.cwd()`

FURB177.py:6:5: FURB177 Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
Unsafe fix
2 2 | from pathlib import Path
3 3 |
4 4 | # Errors
5 |-_ = Path().resolve()
5 |+_ = pathlib.Path.cwd()
6 6 | _ = pathlib.Path().resolve()
7 7 |
8 8 | _ = Path("").resolve()

FURB177.py:6:5: FURB177 [*] Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
|
4 | # Errors
5 | _ = Path().resolve()
Expand All @@ -18,77 +29,174 @@ FURB177.py:6:5: FURB177 Prefer `Path.cwd()` over `Path().resolve()` for current-
7 |
8 | _ = Path("").resolve()
|
= help: Replace `Path().resolve()` with `Path.cwd()`

Unsafe fix
3 3 |
4 4 | # Errors
5 5 | _ = Path().resolve()
6 |-_ = pathlib.Path().resolve()
6 |+_ = pathlib.Path.cwd()
7 7 |
8 8 | _ = Path("").resolve()
9 9 | _ = pathlib.Path("").resolve()

FURB177.py:8:5: FURB177 Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
FURB177.py:8:5: FURB177 [*] Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
|
6 | _ = pathlib.Path().resolve()
7 |
8 | _ = Path("").resolve()
| ^^^^^^^^^^^^^^^^^^ FURB177
9 | _ = pathlib.Path("").resolve()
|
= help: Replace `Path().resolve()` with `Path.cwd()`

Unsafe fix
5 5 | _ = Path().resolve()
6 6 | _ = pathlib.Path().resolve()
7 7 |
8 |-_ = Path("").resolve()
8 |+_ = pathlib.Path.cwd()
9 9 | _ = pathlib.Path("").resolve()
10 10 |
11 11 | _ = Path(".").resolve()

FURB177.py:9:5: FURB177 Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
FURB177.py:9:5: FURB177 [*] Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
|
8 | _ = Path("").resolve()
9 | _ = pathlib.Path("").resolve()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ FURB177
10 |
11 | _ = Path(".").resolve()
|
= help: Replace `Path().resolve()` with `Path.cwd()`

FURB177.py:11:5: FURB177 Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
Unsafe fix
6 6 | _ = pathlib.Path().resolve()
7 7 |
8 8 | _ = Path("").resolve()
9 |-_ = pathlib.Path("").resolve()
9 |+_ = pathlib.Path.cwd()
10 10 |
11 11 | _ = Path(".").resolve()
12 12 | _ = pathlib.Path(".").resolve()

FURB177.py:11:5: FURB177 [*] Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
|
9 | _ = pathlib.Path("").resolve()
10 |
11 | _ = Path(".").resolve()
| ^^^^^^^^^^^^^^^^^^^ FURB177
12 | _ = pathlib.Path(".").resolve()
|
= help: Replace `Path().resolve()` with `Path.cwd()`

Unsafe fix
8 8 | _ = Path("").resolve()
9 9 | _ = pathlib.Path("").resolve()
10 10 |
11 |-_ = Path(".").resolve()
11 |+_ = pathlib.Path.cwd()
12 12 | _ = pathlib.Path(".").resolve()
13 13 |
14 14 | _ = Path("", **kwargs).resolve()

FURB177.py:12:5: FURB177 Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
FURB177.py:12:5: FURB177 [*] Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
|
11 | _ = Path(".").resolve()
12 | _ = pathlib.Path(".").resolve()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ FURB177
13 |
14 | _ = Path("", **kwargs).resolve()
|
= help: Replace `Path().resolve()` with `Path.cwd()`

FURB177.py:14:5: FURB177 Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
Unsafe fix
9 9 | _ = pathlib.Path("").resolve()
10 10 |
11 11 | _ = Path(".").resolve()
12 |-_ = pathlib.Path(".").resolve()
12 |+_ = pathlib.Path.cwd()
13 13 |
14 14 | _ = Path("", **kwargs).resolve()
15 15 | _ = pathlib.Path("", **kwargs).resolve()

FURB177.py:14:5: FURB177 [*] Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
|
12 | _ = pathlib.Path(".").resolve()
13 |
14 | _ = Path("", **kwargs).resolve()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FURB177
15 | _ = pathlib.Path("", **kwargs).resolve()
|
= help: Replace `Path().resolve()` with `Path.cwd()`

Unsafe fix
11 11 | _ = Path(".").resolve()
12 12 | _ = pathlib.Path(".").resolve()
13 13 |
14 |-_ = Path("", **kwargs).resolve()
14 |+_ = pathlib.Path.cwd()
15 15 | _ = pathlib.Path("", **kwargs).resolve()
16 16 |
17 17 | _ = Path(".", **kwargs).resolve()

FURB177.py:15:5: FURB177 Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
FURB177.py:15:5: FURB177 [*] Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
|
14 | _ = Path("", **kwargs).resolve()
15 | _ = pathlib.Path("", **kwargs).resolve()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FURB177
16 |
17 | _ = Path(".", **kwargs).resolve()
|
= help: Replace `Path().resolve()` with `Path.cwd()`

Unsafe fix
12 12 | _ = pathlib.Path(".").resolve()
13 13 |
14 14 | _ = Path("", **kwargs).resolve()
15 |-_ = pathlib.Path("", **kwargs).resolve()
15 |+_ = pathlib.Path.cwd()
16 16 |
17 17 | _ = Path(".", **kwargs).resolve()
18 18 | _ = pathlib.Path(".", **kwargs).resolve()

FURB177.py:17:5: FURB177 Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
FURB177.py:17:5: FURB177 [*] Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
|
15 | _ = pathlib.Path("", **kwargs).resolve()
16 |
17 | _ = Path(".", **kwargs).resolve()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FURB177
18 | _ = pathlib.Path(".", **kwargs).resolve()
|
= help: Replace `Path().resolve()` with `Path.cwd()`

FURB177.py:18:5: FURB177 Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
Unsafe fix
14 14 | _ = Path("", **kwargs).resolve()
15 15 | _ = pathlib.Path("", **kwargs).resolve()
16 16 |
17 |-_ = Path(".", **kwargs).resolve()
17 |+_ = pathlib.Path.cwd()
18 18 | _ = pathlib.Path(".", **kwargs).resolve()
19 19 |
20 20 | # OK

FURB177.py:18:5: FURB177 [*] Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups
|
17 | _ = Path(".", **kwargs).resolve()
18 | _ = pathlib.Path(".", **kwargs).resolve()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FURB177
19 |
20 | # OK
|
= help: Replace `Path().resolve()` with `Path.cwd()`


Unsafe fix
15 15 | _ = pathlib.Path("", **kwargs).resolve()
16 16 |
17 17 | _ = Path(".", **kwargs).resolve()
18 |-_ = pathlib.Path(".", **kwargs).resolve()
18 |+_ = pathlib.Path.cwd()
19 19 |
20 20 | # OK
21 21 | _ = Path.cwd()

0 comments on commit 39dd732

Please sign in to comment.