diff --git a/crates/ruff/resources/test/fixtures/flake8_use_pathlib/PTH201.py b/crates/ruff/resources/test/fixtures/flake8_use_pathlib/PTH201.py index 78ebdf261f..ba495cc588 100644 --- a/crates/ruff/resources/test/fixtures/flake8_use_pathlib/PTH201.py +++ b/crates/ruff/resources/test/fixtures/flake8_use_pathlib/PTH201.py @@ -5,6 +5,7 @@ from pathlib import Path as pth _ = Path(".") _ = pth(".") _ = PurePath(".") +_ = Path("") # no match _ = Path() diff --git a/crates/ruff/src/rules/flake8_use_pathlib/rules/path_constructor_current_directory.rs b/crates/ruff/src/rules/flake8_use_pathlib/rules/path_constructor_current_directory.rs index bed84dd239..0dee506ef3 100644 --- a/crates/ruff/src/rules/flake8_use_pathlib/rules/path_constructor_current_directory.rs +++ b/crates/ruff/src/rules/flake8_use_pathlib/rules/path_constructor_current_directory.rs @@ -73,13 +73,11 @@ pub(crate) fn path_constructor_current_directory(checker: &mut Checker, expr: &E return; }; - if value != "." { - return; + if matches!(value.as_str(), "" | ".") { + let mut diagnostic = Diagnostic::new(PathConstructorCurrentDirectory, *range); + if checker.patch(diagnostic.kind.rule()) { + diagnostic.set_fix(Fix::automatic(Edit::range_deletion(*range))); + } + checker.diagnostics.push(diagnostic); } - - let mut diagnostic = Diagnostic::new(PathConstructorCurrentDirectory, *range); - if checker.patch(diagnostic.kind.rule()) { - diagnostic.set_fix(Fix::automatic(Edit::range_deletion(*range))); - } - checker.diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/flake8_use_pathlib/snapshots/ruff__rules__flake8_use_pathlib__tests__PTH201_PTH201.py.snap b/crates/ruff/src/rules/flake8_use_pathlib/snapshots/ruff__rules__flake8_use_pathlib__tests__PTH201_PTH201.py.snap index 2e215356fd..10d9a60ae4 100644 --- a/crates/ruff/src/rules/flake8_use_pathlib/snapshots/ruff__rules__flake8_use_pathlib__tests__PTH201_PTH201.py.snap +++ b/crates/ruff/src/rules/flake8_use_pathlib/snapshots/ruff__rules__flake8_use_pathlib__tests__PTH201_PTH201.py.snap @@ -19,7 +19,7 @@ PTH201.py:5:10: PTH201 [*] Do not pass the current directory explicitly to `Path 5 |+_ = Path() 6 6 | _ = pth(".") 7 7 | _ = PurePath(".") -8 8 | +8 8 | _ = Path("") PTH201.py:6:9: PTH201 [*] Do not pass the current directory explicitly to `Path` | @@ -28,6 +28,7 @@ PTH201.py:6:9: PTH201 [*] Do not pass the current directory explicitly to `Path` 6 | _ = pth(".") | ^^^ PTH201 7 | _ = PurePath(".") +8 | _ = Path("") | = help: Remove the current directory argument @@ -38,8 +39,8 @@ PTH201.py:6:9: PTH201 [*] Do not pass the current directory explicitly to `Path` 6 |-_ = pth(".") 6 |+_ = pth() 7 7 | _ = PurePath(".") -8 8 | -9 9 | # no match +8 8 | _ = Path("") +9 9 | PTH201.py:7:14: PTH201 [*] Do not pass the current directory explicitly to `Path` | @@ -47,8 +48,7 @@ PTH201.py:7:14: PTH201 [*] Do not pass the current directory explicitly to `Path 6 | _ = pth(".") 7 | _ = PurePath(".") | ^^^ PTH201 -8 | -9 | # no match +8 | _ = Path("") | = help: Remove the current directory argument @@ -58,8 +58,29 @@ PTH201.py:7:14: PTH201 [*] Do not pass the current directory explicitly to `Path 6 6 | _ = pth(".") 7 |-_ = PurePath(".") 7 |+_ = PurePath() -8 8 | -9 9 | # no match -10 10 | _ = Path() +8 8 | _ = Path("") +9 9 | +10 10 | # no match + +PTH201.py:8:10: PTH201 [*] Do not pass the current directory explicitly to `Path` + | + 6 | _ = pth(".") + 7 | _ = PurePath(".") + 8 | _ = Path("") + | ^^ PTH201 + 9 | +10 | # no match + | + = help: Remove the current directory argument + +ℹ Fix +5 5 | _ = Path(".") +6 6 | _ = pth(".") +7 7 | _ = PurePath(".") +8 |-_ = Path("") + 8 |+_ = Path() +9 9 | +10 10 | # no match +11 11 | _ = Path()