Skip to content

Commit ed8e5ed

Browse files
committed
Add test
1 parent 41192cb commit ed8e5ed

File tree

4 files changed

+49
-22
lines changed

4 files changed

+49
-22
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
Test: ensure we're able to correctly remove unused imports
3+
even if they have characters in them that undergo NFKC normalization
4+
"""
5+
6+
from .main import MaµToMan

crates/ruff_linter/src/fix/codemods.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -170,33 +170,35 @@ pub(crate) fn retain_imports(
170170
Ok(tree.codegen_stylist(stylist))
171171
}
172172

173-
fn collect_segments<'a>(expr: &'a Expression, parts: &mut SmallVec<[&'a str; 8]>) {
174-
match expr {
175-
Expression::Call(expr) => {
176-
collect_segments(&expr.func, parts);
177-
}
178-
Expression::Attribute(expr) => {
179-
collect_segments(&expr.value, parts);
180-
parts.push(expr.attr.value);
181-
}
182-
Expression::Name(expr) => {
183-
parts.push(expr.value);
173+
fn qualified_name_from_name_or_attribute(module: &NameOrAttribute) -> String {
174+
fn collect_segments<'a>(expr: &'a Expression, parts: &mut SmallVec<[&'a str; 8]>) {
175+
match expr {
176+
Expression::Call(expr) => {
177+
collect_segments(&expr.func, parts);
178+
}
179+
Expression::Attribute(expr) => {
180+
collect_segments(&expr.value, parts);
181+
parts.push(expr.attr.value);
182+
}
183+
Expression::Name(expr) => {
184+
parts.push(expr.value);
185+
}
186+
_ => {}
184187
}
185-
_ => {}
186188
}
187-
}
188189

189-
fn unqualified_name_from_expression<'a>(expr: &'a Expression<'a>) -> Option<UnqualifiedName<'a>> {
190-
let mut segments = smallvec![];
191-
collect_segments(expr, &mut segments);
192-
if segments.is_empty() {
193-
None
194-
} else {
195-
Some(segments.into_iter().collect())
190+
fn unqualified_name_from_expression<'a>(
191+
expr: &'a Expression<'a>,
192+
) -> Option<UnqualifiedName<'a>> {
193+
let mut segments = smallvec![];
194+
collect_segments(expr, &mut segments);
195+
if segments.is_empty() {
196+
None
197+
} else {
198+
Some(segments.into_iter().collect())
199+
}
196200
}
197-
}
198201

199-
fn qualified_name_from_name_or_attribute(module: &NameOrAttribute) -> String {
200202
let unnormalized = match module {
201203
NameOrAttribute::N(name) => Cow::Borrowed(name.value),
202204
NameOrAttribute::A(attr) => {
@@ -208,5 +210,6 @@ fn qualified_name_from_name_or_attribute(module: &NameOrAttribute) -> String {
208210
)
209211
}
210212
};
213+
211214
unnormalized.nfkc().collect()
212215
}

crates/ruff_linter/src/rules/pyflakes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ mod tests {
258258
#[test_case(Rule::UnusedImport, Path::new("F401_27__all_mistyped/__init__.py"))]
259259
#[test_case(Rule::UnusedImport, Path::new("F401_28__all_multiple/__init__.py"))]
260260
#[test_case(Rule::UnusedImport, Path::new("F401_29__all_conditional/__init__.py"))]
261+
#[test_case(Rule::UnusedImport, Path::new("F401_30.py"))]
261262
fn f401_deprecated_option(rule_code: Rule, path: &Path) -> Result<()> {
262263
let snapshot = format!(
263264
"{}_deprecated_option_{}",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
3+
---
4+
F401_30.py:6:19: F401 [*] `.main.MaμToMan` imported but unused
5+
|
6+
4 | """
7+
5 |
8+
6 | from .main import MaµToMan
9+
| ^^^^^^^^ F401
10+
|
11+
= help: Remove unused import: `.main.MaμToMan`
12+
13+
Safe fix
14+
3 3 | even if they have characters in them that undergo NFKC normalization
15+
4 4 | """
16+
5 5 |
17+
6 |-from .main import MaµToMan

0 commit comments

Comments
 (0)