Avoid generating dirty call paths (#2144)

This commit is contained in:
Charlie Marsh
2023-01-24 20:40:38 -05:00
committed by GitHub
parent 82d7814101
commit deff503932
4 changed files with 20 additions and 7 deletions

View File

@@ -0,0 +1,3 @@
from pathlib import Path
(Path("") / "").open()

View File

@@ -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,
}
}

View File

@@ -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(

View File

@@ -0,0 +1,6 @@
---
source: src/rules/flake8_use_pathlib/mod.rs
expression: diagnostics
---
[]