Skip to content

Commit

Permalink
[ruff F401 #10390 #10391] add information to unused-import-context; u…
Browse files Browse the repository at this point in the history
…pdate generation of help messages
  • Loading branch information
plredmond committed Apr 30, 2024
1 parent 1800eb7 commit 37c5152
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::rules::isort;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum UnusedImportContext {
ExceptHandler,
Init,
Init { first_party: bool, dunder_all: bool },
}

/// ## What it does
Expand Down Expand Up @@ -95,7 +95,7 @@ impl Violation for UnusedImport {
"`{name}` imported but unused; consider using `importlib.util.find_spec` to test for availability"
)
}
Some(UnusedImportContext::Init) => {
Some(UnusedImportContext::Init { .. }) => {
format!(
"`{name}` imported but unused; consider removing, adding to `__all__`, or using a redundant alias"
)
Expand All @@ -106,10 +106,21 @@ impl Violation for UnusedImport {

fn fix_title(&self) -> Option<String> {
let UnusedImport { name, multiple, .. } = self;
let resolution = match self.context {
Some(UnusedImportContext::Init {
first_party: true,
dunder_all: true,
}) => "Add unused import to __all__",
Some(UnusedImportContext::Init {
first_party: true,
dunder_all: false,
}) => "Use a redundant alias",
_ => "Remove unused import",
};
Some(if *multiple {
"Remove unused import".to_string()
resolution.to_string()
} else {
format!("Remove unused import: `{name}`")
format!("{resolution}: `{name}`")
})
}
}
Expand Down

0 comments on commit 37c5152

Please sign in to comment.