Skip to content

Commit

Permalink
[ruff F401 #10390 #10391] cleanup the other fix-imports function
Browse files Browse the repository at this point in the history
  • Loading branch information
plredmond committed Apr 30, 2024
1 parent a8f5373 commit d666bac
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/fix/edits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Interface for generating fix edits from higher-level actions (e.g., "remove an argument").
use anyhow::{bail, Context, Result};
use anyhow::{Context, Result};

use ruff_diagnostics::Edit;
use ruff_python_ast::parenthesize::parenthesized_range;
Expand Down
20 changes: 10 additions & 10 deletions crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;
use std::iter;

use anyhow::{bail, Result};
use anyhow::{anyhow, bail, Result};
use rustc_hash::FxHashMap;

use ruff_diagnostics::{Applicability, Diagnostic, Fix, FixAvailability, Violation};
Expand Down Expand Up @@ -216,7 +216,7 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut
if !in_except_handler && !checker.settings.preview.is_enabled() {
(
fix_by_removing_imports(checker, node_id, &to_remove, in_init).ok(),
fix_by_reexporting(checker, node_id, &to_explicit, dunder_all),
fix_by_reexporting(checker, node_id, &to_explicit, dunder_all).ok(),
)
} else {
(None, None)
Expand Down Expand Up @@ -341,7 +341,10 @@ fn fix_by_reexporting(
node_id: NodeId,
imports: &[ImportBinding],
dunder_all: Option<NodeId>,
) -> Option<Fix> {
) -> Result<Fix> {
if 0 == imports.len() {
bail!("Expected import bindings");
}
let statement = checker.semantic().statement(node_id);

let member_names = imports
Expand All @@ -351,17 +354,14 @@ fn fix_by_reexporting(
let member_names = member_names.iter().map(AsRef::as_ref);

let edits = match dunder_all {
Some(dunder_all) => todo!(),
Some(_dunder_all) => bail!("Not implemented: add to dunder_all"),
None => fix::edits::make_redundant_alias(member_names, statement),
};

// Only emit a fix if there are edits
if 0 == edits.len() || 0 < imports.len() {
return None;
}
let mut edits_iter = edits.into_iter();
let first = edits_iter.next()?;
let mut tail = edits.into_iter();
let head = tail.next().ok_or(anyhow!("No edits to make"))?;

let isolation = Checker::isolation(checker.semantic().parent_statement_id(node_id));
Some(Fix::safe_edits(first, edits_iter).isolate(isolation))
Ok(Fix::safe_edits(head, tail).isolate(isolation))
}

0 comments on commit d666bac

Please sign in to comment.