From 15977ed2bae6c53b9b70520699d54d1ce995896c Mon Sep 17 00:00:00 2001 From: PLR <51248199+plredmond@users.noreply.github.com> Date: Fri, 31 May 2024 12:18:24 -0700 Subject: [PATCH 1/2] sort bindings before adding to __all__ --- crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 eff33fcb1cc5a..8de9601356993 100644 --- a/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs +++ b/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs @@ -330,7 +330,7 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut fix_by_reexporting( checker, import_statement, - &to_reexport.iter().map(|(b, _)| b).collect::>(), + &mut to_reexport.iter().map(|(b, _)| b).collect::>(), &dunder_all_exprs, ) .ok(), @@ -450,7 +450,7 @@ fn fix_by_removing_imports<'a>( fn fix_by_reexporting( checker: &Checker, node_id: NodeId, - imports: &[&ImportBinding], + imports: &mut [&ImportBinding], dunder_all_exprs: &[&ast::Expr], ) -> Result { let statement = checker.semantic().statement(node_id); @@ -458,6 +458,7 @@ fn fix_by_reexporting( bail!("Expected import bindings"); } + imports.sort_by_key(|b| b.name); let edits = match dunder_all_exprs { [] => fix::edits::make_redundant_alias( imports.iter().map(|b| b.import.member_name()), From cbc6fe75ebf3cef3b735ee8f1169761515dce544 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 31 May 2024 16:25:26 -0400 Subject: [PATCH 2/2] Use vec --- crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 8de9601356993..60347b06d95b6 100644 --- a/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs +++ b/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs @@ -330,7 +330,7 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut fix_by_reexporting( checker, import_statement, - &mut to_reexport.iter().map(|(b, _)| b).collect::>(), + to_reexport.iter().map(|(b, _)| b).collect::>(), &dunder_all_exprs, ) .ok(), @@ -450,7 +450,7 @@ fn fix_by_removing_imports<'a>( fn fix_by_reexporting( checker: &Checker, node_id: NodeId, - imports: &mut [&ImportBinding], + mut imports: Vec<&ImportBinding>, dunder_all_exprs: &[&ast::Expr], ) -> Result { let statement = checker.semantic().statement(node_id); @@ -459,6 +459,7 @@ fn fix_by_reexporting( } imports.sort_by_key(|b| b.name); + let edits = match dunder_all_exprs { [] => fix::edits::make_redundant_alias( imports.iter().map(|b| b.import.member_name()),