mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:10:09 +00:00
Wrap each import in its own backticks (#346)
This commit is contained in:
parent
b60768debb
commit
e3a7357187
4 changed files with 42 additions and 27 deletions
|
@ -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);
|
||||
Ok(fix) => Some(fix),
|
||||
Err(e) => {
|
||||
error!("Failed to fix unused imports: {}", e);
|
||||
None
|
||||
}
|
||||
check.amend(fix)
|
||||
}
|
||||
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);
|
||||
|
|
|
@ -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<String>),
|
||||
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")
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -3,7 +3,8 @@ source: src/linter.rs
|
|||
expression: checks
|
||||
---
|
||||
- kind:
|
||||
UnusedImport: models.Nut
|
||||
UnusedImport:
|
||||
- models.Nut
|
||||
location:
|
||||
row: 5
|
||||
column: 1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue