diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/PTH208.py b/crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/PTH208.py index 23f3abd63dd240..8a31b2b472e056 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/PTH208.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/PTH208.py @@ -14,3 +14,10 @@ path_path = Path('.') os.listdir(path_path) + + +if os.listdir("dir"): + ... + +if "file" in os.listdir("dir"): + ... diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH208_PTH208.py.snap b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH208_PTH208.py.snap index 4797d9c3f567ec..27d5009df016d1 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH208_PTH208.py.snap +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH208_PTH208.py.snap @@ -42,3 +42,19 @@ PTH208.py:16:1: PTH208 Use `pathlib.Path.iterdir()` instead. 16 | os.listdir(path_path) | ^^^^^^^^^^ PTH208 | + +PTH208.py:19:4: PTH208 Use `pathlib.Path.iterdir()` instead. + | +19 | if os.listdir("dir"): + | ^^^^^^^^^^ PTH208 +20 | ... + | + +PTH208.py:22:14: PTH208 Use `pathlib.Path.iterdir()` instead. + | +20 | ... +21 | +22 | if "file" in os.listdir("dir"): + | ^^^^^^^^^^ PTH208 +23 | ... + | diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/violations.rs b/crates/ruff_linter/src/rules/flake8_use_pathlib/violations.rs index c6f4499f514044..f6dd246bab3a00 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/violations.rs +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/violations.rs @@ -1118,6 +1118,12 @@ impl Violation for PyPath { /// p = "." /// for d in os.listdir(p): /// ... +/// +/// if os.listdir(p): +/// ... +/// +/// if "file" in os.listdir(p): +/// ... /// ``` /// /// Use instead: @@ -1126,6 +1132,12 @@ impl Violation for PyPath { /// p = Path(".") /// for d in p.iterdir(): /// ... +/// +/// if any(p.iterdir()): +/// ... +/// +/// if (p / "file").exists(): +/// ... /// ``` #[violation] pub struct OsListdir;