From deff503932c5f5659aef3403a73f23e6bf5255df Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 24 Jan 2023 20:40:38 -0500 Subject: [PATCH] Avoid generating dirty call paths (#2144) --- .../fixtures/flake8_use_pathlib/use_pathlib.py | 3 +++ src/ast/helpers.rs | 17 ++++++++++------- src/rules/flake8_use_pathlib/mod.rs | 1 + ...ake8_use_pathlib__tests__use_pathlib.py.snap | 6 ++++++ 4 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 resources/test/fixtures/flake8_use_pathlib/use_pathlib.py create mode 100644 src/rules/flake8_use_pathlib/snapshots/ruff__rules__flake8_use_pathlib__tests__use_pathlib.py.snap diff --git a/resources/test/fixtures/flake8_use_pathlib/use_pathlib.py b/resources/test/fixtures/flake8_use_pathlib/use_pathlib.py new file mode 100644 index 0000000000..748442f50a --- /dev/null +++ b/resources/test/fixtures/flake8_use_pathlib/use_pathlib.py @@ -0,0 +1,3 @@ +from pathlib import Path + +(Path("") / "").open() diff --git a/src/ast/helpers.rs b/src/ast/helpers.rs index 71ce1296a6..fb414b4db8 100644 --- a/src/ast/helpers.rs +++ b/src/ast/helpers.rs @@ -40,19 +40,22 @@ pub fn unparse_stmt(stmt: &Stmt, stylist: &Stylist) -> String { generator.generate() } -fn collect_call_path_inner<'a>(expr: &'a Expr, parts: &mut CallPath<'a>) { +fn collect_call_path_inner<'a>(expr: &'a Expr, parts: &mut CallPath<'a>) -> bool { match &expr.node { - ExprKind::Call { func, .. } => { - collect_call_path_inner(func, parts); - } + ExprKind::Call { func, .. } => collect_call_path_inner(func, parts), ExprKind::Attribute { value, attr, .. } => { - collect_call_path_inner(value, parts); - parts.push(attr); + if collect_call_path_inner(value, parts) { + parts.push(attr); + true + } else { + false + } } ExprKind::Name { id, .. } => { parts.push(id); + true } - _ => {} + _ => false, } } diff --git a/src/rules/flake8_use_pathlib/mod.rs b/src/rules/flake8_use_pathlib/mod.rs index 27944b7014..22de240f8d 100644 --- a/src/rules/flake8_use_pathlib/mod.rs +++ b/src/rules/flake8_use_pathlib/mod.rs @@ -17,6 +17,7 @@ mod tests { #[test_case(Path::new("import_as.py"); "PTH1_2")] #[test_case(Path::new("import_from_as.py"); "PTH1_3")] #[test_case(Path::new("import_from.py"); "PTH1_4")] + #[test_case(Path::new("use_pathlib.py"); "PTH1_5")] fn rules(path: &Path) -> Result<()> { let snapshot = format!("{}", path.to_string_lossy()); let diagnostics = test_path( diff --git a/src/rules/flake8_use_pathlib/snapshots/ruff__rules__flake8_use_pathlib__tests__use_pathlib.py.snap b/src/rules/flake8_use_pathlib/snapshots/ruff__rules__flake8_use_pathlib__tests__use_pathlib.py.snap new file mode 100644 index 0000000000..359d76f726 --- /dev/null +++ b/src/rules/flake8_use_pathlib/snapshots/ruff__rules__flake8_use_pathlib__tests__use_pathlib.py.snap @@ -0,0 +1,6 @@ +--- +source: src/rules/flake8_use_pathlib/mod.rs +expression: diagnostics +--- +[] +