mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
Convert UnusedImport violation to struct fields (#2141)
This commit is contained in:
parent
94551a203e
commit
bb85119ba8
2 changed files with 20 additions and 6 deletions
|
@ -4719,7 +4719,11 @@ impl<'a> Checker<'a> {
|
|||
let multiple = unused_imports.len() > 1;
|
||||
for (full_name, range) in unused_imports {
|
||||
let mut diagnostic = Diagnostic::new(
|
||||
violations::UnusedImport(full_name.to_string(), ignore_init, multiple),
|
||||
violations::UnusedImport {
|
||||
name: full_name.to_string(),
|
||||
ignore_init,
|
||||
multiple,
|
||||
},
|
||||
*range,
|
||||
);
|
||||
if matches!(child.node, StmtKind::ImportFrom { .. })
|
||||
|
@ -4741,7 +4745,11 @@ impl<'a> Checker<'a> {
|
|||
let multiple = unused_imports.len() > 1;
|
||||
for (full_name, range) in unused_imports {
|
||||
let mut diagnostic = Diagnostic::new(
|
||||
violations::UnusedImport(full_name.to_string(), ignore_init, multiple),
|
||||
violations::UnusedImport {
|
||||
name: full_name.to_string(),
|
||||
ignore_init,
|
||||
multiple,
|
||||
},
|
||||
*range,
|
||||
);
|
||||
if matches!(child.node, StmtKind::ImportFrom { .. })
|
||||
|
|
|
@ -62,10 +62,14 @@ impl Violation for SyntaxError {
|
|||
// pyflakes
|
||||
|
||||
define_violation!(
|
||||
pub struct UnusedImport(pub String, pub bool, pub bool);
|
||||
pub struct UnusedImport {
|
||||
pub name: String,
|
||||
pub ignore_init: bool,
|
||||
pub multiple: bool,
|
||||
}
|
||||
);
|
||||
fn fmt_unused_import_autofix_msg(unused_import: &UnusedImport) -> String {
|
||||
let UnusedImport(name, _, multiple) = unused_import;
|
||||
let UnusedImport { name, multiple, .. } = unused_import;
|
||||
if *multiple {
|
||||
"Remove unused import".to_string()
|
||||
} else {
|
||||
|
@ -77,7 +81,9 @@ impl Violation for UnusedImport {
|
|||
|
||||
#[derive_message_formats]
|
||||
fn message(&self) -> String {
|
||||
let UnusedImport(name, ignore_init, ..) = self;
|
||||
let UnusedImport {
|
||||
name, ignore_init, ..
|
||||
} = self;
|
||||
if *ignore_init {
|
||||
format!(
|
||||
"`{name}` imported but unused; consider adding to `__all__` or using a redundant \
|
||||
|
@ -89,7 +95,7 @@ impl Violation for UnusedImport {
|
|||
}
|
||||
|
||||
fn autofix_title_formatter(&self) -> Option<fn(&Self) -> String> {
|
||||
let UnusedImport(_, ignore_init, _) = self;
|
||||
let UnusedImport { ignore_init, .. } = self;
|
||||
if *ignore_init {
|
||||
None
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue