Convert UnusedImport violation to struct fields (#2141)

This commit is contained in:
Sladyn 2023-01-27 08:31:39 -08:00 committed by GitHub
parent 94551a203e
commit bb85119ba8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 6 deletions

View file

@ -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 { .. })

View file

@ -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 {