-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary Implement [`no-implicit-cwd`](https://github.com/dosisod/refurb/blob/master/docs/checks.md#furb177-no-implicit-cwd) as `implicit-cwd` Related to #1348. ## Test Plan `cargo test`
- Loading branch information
1 parent
246d93e
commit 78b8741
Showing
10 changed files
with
244 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
crates/ruff_linter/resources/test/fixtures/refurb/FURB177.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import pathlib | ||
from pathlib import Path | ||
|
||
# Errors | ||
_ = Path().resolve() | ||
_ = pathlib.Path().resolve() | ||
|
||
_ = Path("").resolve() | ||
_ = pathlib.Path("").resolve() | ||
|
||
_ = Path(".").resolve() | ||
_ = pathlib.Path(".").resolve() | ||
|
||
_ = Path("", **kwargs).resolve() | ||
_ = pathlib.Path("", **kwargs).resolve() | ||
|
||
_ = Path(".", **kwargs).resolve() | ||
_ = pathlib.Path(".", **kwargs).resolve() | ||
|
||
# OK | ||
_ = Path.cwd() | ||
_ = pathlib.Path.cwd() | ||
|
||
_ = Path("foo").resolve() | ||
_ = pathlib.Path("foo").resolve() | ||
|
||
_ = Path(".", "foo").resolve() | ||
_ = pathlib.Path(".", "foo").resolve() | ||
|
||
_ = Path(*args).resolve() | ||
_ = pathlib.Path(*args).resolve() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
crates/ruff_linter/src/rules/refurb/rules/implicit_cwd.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use ruff_diagnostics::{Diagnostic, Edit, Fix, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::{self as ast, Constant, Expr, ExprAttribute, ExprCall}; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::{checkers::ast::Checker, importer::ImportRequest, registry::AsRule}; | ||
|
||
/// ## What it does | ||
/// Checks for current-directory lookups using `Path().resolve()`. | ||
/// | ||
/// ## Why is this bad? | ||
/// When looking up the current directory, prefer `Path.cwd()` over | ||
/// `Path().resolve()`, as `Path.cwd()` is more explicit in its intent. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// cwd = Path.resolve() | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// cwd = Path.cwd() | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Python documentation: `Path.cwd`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.cwd) | ||
|
||
#[violation] | ||
pub struct ImplicitCwd; | ||
|
||
impl Violation for ImplicitCwd { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups") | ||
} | ||
} | ||
|
||
/// FURB177 | ||
pub(crate) fn no_implicit_cwd(checker: &mut Checker, call: &ExprCall) { | ||
if !call.arguments.is_empty() { | ||
return; | ||
} | ||
|
||
let Expr::Attribute(ExprAttribute { attr, value, .. }) = call.func.as_ref() else { | ||
return; | ||
}; | ||
|
||
if attr != "resolve" { | ||
return; | ||
} | ||
|
||
let Expr::Call(ExprCall { | ||
func, arguments, .. | ||
}) = value.as_ref() | ||
else { | ||
return; | ||
}; | ||
|
||
// Match on arguments, but ignore keyword arguments. `Path()` accepts keyword arguments, but | ||
// ignores them. See: https://github.com/python/cpython/issues/98094. | ||
match arguments.args.as_slice() { | ||
// Ex) `Path().resolve()` | ||
[] => {} | ||
// Ex) `Path(".").resolve()` | ||
[arg] => { | ||
let Expr::Constant(ast::ExprConstant { | ||
value: Constant::Str(str), | ||
.. | ||
}) = arg | ||
else { | ||
return; | ||
}; | ||
if !matches!(str.value.as_str(), "" | ".") { | ||
return; | ||
} | ||
} | ||
// Ex) `Path("foo", "bar").resolve()` | ||
_ => return, | ||
} | ||
|
||
if !checker | ||
.semantic() | ||
.resolve_call_path(func) | ||
.is_some_and(|call_path| matches!(call_path.as_slice(), ["pathlib", "Path"])) | ||
{ | ||
return; | ||
} | ||
|
||
let mut diagnostic = Diagnostic::new(ImplicitCwd, call.range()); | ||
|
||
if checker.patch(diagnostic.kind.rule()) { | ||
diagnostic.try_set_fix(|| { | ||
let (import_edit, binding) = checker.importer().get_or_import_symbol( | ||
&ImportRequest::import("pathlib", "Path"), | ||
call.start(), | ||
checker.semantic(), | ||
)?; | ||
Ok(Fix::suggested_edits( | ||
Edit::range_replacement(format!("{binding}.cwd()"), call.range()), | ||
[import_edit], | ||
)) | ||
}); | ||
} | ||
|
||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(ImplicitCwd, call.range())); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...ter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB177_FURB177.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/refurb/mod.rs | ||
--- | ||
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() | ||
| | ||
|
||
FURB177.py:6:5: FURB177 Prefer `Path.cwd()` over `Path().resolve()` for current-directory lookups | ||
| | ||
4 | # Errors | ||
5 | _ = Path().resolve() | ||
6 | _ = pathlib.Path().resolve() | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ FURB177 | ||
7 | | ||
8 | _ = Path("").resolve() | ||
| | ||
|
||
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() | ||
| | ||
|
||
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() | ||
| | ||
|
||
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() | ||
| | ||
|
||
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() | ||
| | ||
|
||
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() | ||
| | ||
|
||
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() | ||
| | ||
|
||
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() | ||
| | ||
|
||
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 | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.