diff --git a/src/check_ast.rs b/src/check_ast.rs index 559f979428..74bbc7c0ae 100644 --- a/src/check_ast.rs +++ b/src/check_ast.rs @@ -1721,12 +1721,8 @@ impl<'a> Checker<'a> { let child = self.parents[defined_by]; let parent = defined_in.map(|defined_in| self.parents[defined_in]); - let mut check = Check::new( - CheckKind::UnusedImport(full_names.join(", ")), - self.locate_check(Range::from_located(child)), - ); - - if matches!(self.autofix, fixer::Mode::Generate | fixer::Mode::Apply) { + let fix = if matches!(self.autofix, fixer::Mode::Generate | fixer::Mode::Apply) + { let deleted: Vec<&Stmt> = self .deletions .iter() @@ -1739,14 +1735,22 @@ impl<'a> Checker<'a> { }; match removal_fn(&mut self.locator, &full_names, child, parent, &deleted) { - Ok(fix) => { - if fix.content.is_empty() || fix.content == "pass" { - self.deletions.insert(defined_by); - } - check.amend(fix) + Ok(fix) => Some(fix), + Err(e) => { + error!("Failed to fix unused imports: {}", e); + None } - Err(e) => error!("Failed to fix unused imports: {}", e), } + } else { + None + }; + + let mut check = Check::new( + CheckKind::UnusedImport(full_names.into_iter().map(String::from).collect()), + self.locate_check(Range::from_located(child)), + ); + if let Some(fix) = fix { + check.amend(fix); } self.checks.push(check); diff --git a/src/checks.rs b/src/checks.rs index 089f413535..7962af35f3 100644 --- a/src/checks.rs +++ b/src/checks.rs @@ -366,7 +366,7 @@ impl CheckCode { CheckCode::E902 => CheckKind::IOError("IOError: `...`".to_string()), CheckCode::E999 => CheckKind::SyntaxError("`...`".to_string()), // pyflakes - CheckCode::F401 => CheckKind::UnusedImport("...".to_string()), + CheckCode::F401 => CheckKind::UnusedImport(vec!["...".to_string()]), CheckCode::F402 => CheckKind::ImportShadowedByLoopVar("...".to_string(), 1), CheckCode::F403 => CheckKind::ImportStarUsed("...".to_string()), CheckCode::F404 => CheckKind::LateFutureImport, @@ -476,7 +476,7 @@ pub enum CheckKind { UndefinedExport(String), UndefinedLocal(String), UndefinedName(String), - UnusedImport(String), + UnusedImport(Vec), UnusedVariable(String), YieldOutsideFunction, // flake8-builtin @@ -764,7 +764,10 @@ impl CheckKind { CheckKind::UndefinedName(name) => { format!("Undefined name `{name}`") } - CheckKind::UnusedImport(name) => format!("`{name}` imported but unused"), + CheckKind::UnusedImport(names) => { + let names = names.iter().map(|name| format!("`{name}`")).join(", "); + format!("{names} imported but unused") + } CheckKind::UnusedVariable(name) => { format!("Local variable `{name}` is assigned to but never used") } diff --git a/src/snapshots/ruff__linter__tests__f401.snap b/src/snapshots/ruff__linter__tests__f401.snap index 47f1129106..04b0b2a61d 100644 --- a/src/snapshots/ruff__linter__tests__f401.snap +++ b/src/snapshots/ruff__linter__tests__f401.snap @@ -3,7 +3,8 @@ source: src/linter.rs expression: checks --- - kind: - UnusedImport: functools + UnusedImport: + - functools location: row: 2 column: 1 @@ -20,7 +21,8 @@ expression: checks column: 21 applied: false - kind: - UnusedImport: collections.OrderedDict + UnusedImport: + - collections.OrderedDict location: row: 4 column: 1 @@ -37,7 +39,8 @@ expression: checks column: 2 applied: false - kind: - UnusedImport: logging.handlers + UnusedImport: + - logging.handlers location: row: 12 column: 1 @@ -54,7 +57,8 @@ expression: checks column: 24 applied: false - kind: - UnusedImport: shelve + UnusedImport: + - shelve location: row: 33 column: 5 @@ -71,7 +75,8 @@ expression: checks column: 1 applied: false - kind: - UnusedImport: importlib + UnusedImport: + - importlib location: row: 34 column: 5 @@ -79,16 +84,17 @@ expression: checks row: 34 column: 21 fix: - content: pass + content: "" location: row: 34 - column: 5 + column: 1 end_location: - row: 34 - column: 21 + row: 35 + column: 1 applied: false - kind: - UnusedImport: pathlib + UnusedImport: + - pathlib location: row: 38 column: 5 @@ -105,7 +111,8 @@ expression: checks column: 1 applied: false - kind: - UnusedImport: pickle + UnusedImport: + - pickle location: row: 53 column: 9 diff --git a/src/snapshots/ruff__linter__tests__future_annotations.snap b/src/snapshots/ruff__linter__tests__future_annotations.snap index 291bd1122b..f179c21d31 100644 --- a/src/snapshots/ruff__linter__tests__future_annotations.snap +++ b/src/snapshots/ruff__linter__tests__future_annotations.snap @@ -3,7 +3,8 @@ source: src/linter.rs expression: checks --- - kind: - UnusedImport: models.Nut + UnusedImport: + - models.Nut location: row: 5 column: 1