diff --git a/crates/ruff_linter/src/fix/edits.rs b/crates/ruff_linter/src/fix/edits.rs index b5420e4ee18ab..756ba066961aa 100644 --- a/crates/ruff_linter/src/fix/edits.rs +++ b/crates/ruff_linter/src/fix/edits.rs @@ -149,6 +149,24 @@ pub(crate) fn add_to_dunder_all<'a>( expr: &Expr, stylist: &Stylist, ) -> Vec { + let elts = match expr { + Expr::List(ast::ExprList { elts, .. }) => Some(elts), + Expr::Tuple(ast::ExprTuple { elts, .. }) => Some(elts), + _ => None, + }; + + let existing_names: std::collections::HashSet<&str> = elts + .iter() + .flat_map(|elts| elts.iter()) + .filter_map(|elt| { + if let Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) = elt { + Some(value.to_str()) + } else { + None + } + }) + .collect(); + let (insertion_point, export_prefix_length) = match expr { Expr::List(ExprList { elts, .. }) => ( elts.last().map_or(expr.end() - "]".text_len(), Ranged::end), @@ -175,6 +193,7 @@ pub(crate) fn add_to_dunder_all<'a>( }; let quote = stylist.quote(); let mut edits: Vec<_> = names + .filter(|name| !existing_names.contains(*name)) .enumerate() .map(|(offset, name)| match export_prefix_length + offset { 0 => Edit::insertion(format!("{quote}{name}{quote}"), insertion_point), @@ -182,7 +201,7 @@ pub(crate) fn add_to_dunder_all<'a>( }) .collect(); if let Expr::Tuple(tup) = expr { - if tup.parenthesized && export_prefix_length + edits.len() == 1 { + if !edits.is_empty() && tup.parenthesized && export_prefix_length + edits.len() == 1 { edits.push(Edit::insertion(",".to_string(), insertion_point)); } } @@ -722,6 +741,9 @@ x = 1 \ #[test_case(r#"["a", "b",]"#, &["x"], r#"["a", "b", "x",]"# ; "1 into nonempty list w/trailing comma")] #[test_case(r#"["a", "b"]"#, &["x", "y"], r#"["a", "b", "x", "y"]"# ; "2 into nonempty list")] #[test_case(r#"["a", "b"]"#, &["x"], r#"["a", "b", "x"]"# ; "1 into nonempty list")] + #[test_case(r#"["a"]"#, &["a", "b"], r#"["a", "b"]"# ; "add one new, one duplicate to list")] + #[test_case(r#"("a",)"#, &["a", "b"], r#"("a", "b",)"# ; "add one new, one duplicate to tuple")] + #[test_case(r#"["a", "b"]"#, &["b", "c"], r#"["a", "b", "c"]"# ; "add one new, one duplicate to multi-item list")] fn add_to_dunder_all_test(raw: &str, names: &[&str], expect: &str) -> Result<()> { let locator = Locator::new(raw); let edits = { diff --git a/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs b/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs index af1ae56a0d860..7f26940ef255e 100644 --- a/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs +++ b/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs @@ -736,7 +736,15 @@ fn unused_imports_from_binding<'a, 'b>( .expect("binding to be import binding since current function called after restricting to these in `unused_imports_in_scope`") .qualified_name() .segments().first().expect("import binding to have nonempty qualified name"); - mark_uses_of_qualified_name(&mut marked, &QualifiedName::user_defined(first)); + let qualified_first = QualifiedName::user_defined(first); + for (binding, is_used) in marked.iter_mut() { + if let Some(import) = binding.as_any_import() { + if import.qualified_name().starts_with(&qualified_first) { + *is_used = true; + } + } + } + marked_dunder_all = true; continue; } diff --git a/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__f401_use_in_dunder_all.snap b/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__f401_use_in_dunder_all.snap index 58ec910de06a5..d0b409f39ee0b 100644 --- a/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__f401_use_in_dunder_all.snap +++ b/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__f401_use_in_dunder_all.snap @@ -1,16 +1,4 @@ --- source: crates/ruff_linter/src/rules/pyflakes/mod.rs --- -F401 [*] `a.b` imported but unused - --> f401_preview_submodule.py:3:8 - | -2 | import a -3 | import a.b - | ^^^ -4 | __all__ = ["a"] - | -help: Remove unused import: `a.b` -1 | -2 | import a - - import a.b -3 | __all__ = ["a"] +