Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion crates/ruff_linter/src/fix/edits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,24 @@ pub(crate) fn add_to_dunder_all<'a>(
expr: &Expr,
stylist: &Stylist,
) -> Vec<Edit> {
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),
Expand All @@ -175,14 +193,15 @@ 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),
_ => Edit::insertion(format!(", {quote}{name}{quote}"), insertion_point),
})
.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));
}
}
Expand Down Expand Up @@ -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 = {
Expand Down
10 changes: 9 additions & 1 deletion crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"]

Loading