Skip to content

Commit

Permalink
Preview
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jan 9, 2024
1 parent 2756113 commit b051d2a
Show file tree
Hide file tree
Showing 41 changed files with 366 additions and 120 deletions.
36 changes: 19 additions & 17 deletions crates/ruff_linter/src/checkers/ast/analyze/deferred_scopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,23 +253,25 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) {
diagnostic.set_parent(range.start());
}

if let Some(import) = binding.as_any_import() {
if let Some(source) = binding.source {
diagnostic.try_set_fix(|| {
let statement = checker.semantic().statement(source);
let parent = checker.semantic().parent_statement(source);
let edit = fix::edits::remove_unused_imports(
std::iter::once(import.member_name().as_ref()),
statement,
parent,
checker.locator(),
checker.stylist(),
checker.indexer(),
)?;
Ok(Fix::unsafe_edit(edit).isolate(Checker::isolation(
checker.semantic().parent_statement_id(source),
)))
});
if checker.settings.preview.is_enabled() {
if let Some(import) = binding.as_any_import() {
if let Some(source) = binding.source {
diagnostic.try_set_fix(|| {
let statement = checker.semantic().statement(source);
let parent = checker.semantic().parent_statement(source);
let edit = fix::edits::remove_unused_imports(
std::iter::once(import.member_name().as_ref()),
statement,
parent,
checker.locator(),
checker.stylist(),
checker.indexer(),
)?;
Ok(Fix::safe_edit(edit).isolate(Checker::isolation(
checker.semantic().parent_statement_id(source),
)))
});
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions crates/ruff_linter/src/rules/pyflakes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,34 @@ mod tests {
Ok(())
}

#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_0.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_1.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_10.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_11.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_12.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_13.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_14.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_15.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_16.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_17.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_18.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_19.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_2.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_20.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_21.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_22.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_23.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_24.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_25.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_26.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_27.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_3.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_4.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_5.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_6.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_7.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_8.py"))]
#[test_case(Rule::RedefinedWhileUnused, Path::new("F811_9.py"))]
#[test_case(Rule::UnusedVariable, Path::new("F841_4.py"))]
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,6 @@ use ruff_source_file::SourceRow;
/// import foo
/// import bar
/// ```
///
/// ## Fix safety
/// This rule's fix is marked as unsafe, as removing a redefinition across
/// branches or scopes may change the behavior of the program in subtle
/// ways.
///
/// For example:
/// ```python
/// import module
///
/// x = int(input())
///
/// if x > 0:
/// from package import module
/// ```
///
/// Removing the redefinition would change the `module` binding when `x > 0`.
#[violation]
pub struct RedefinedWhileUnused {
pub name: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F811_1.py:1:25: F811 [*] Redefinition of unused `FU` from line 1
F811_1.py:1:25: F811 Redefinition of unused `FU` from line 1
|
1 | import fu as FU, bar as FU
| ^^ F811
|
= help: Remove definition: `FU`

Unsafe fix
1 |-import fu as FU, bar as FU
1 |+import fu as FU


Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F811_12.py:6:20: F811 [*] Redefinition of unused `mixer` from line 2
F811_12.py:6:20: F811 Redefinition of unused `mixer` from line 2
|
4 | pass
5 | else:
Expand All @@ -11,12 +11,4 @@ F811_12.py:6:20: F811 [*] Redefinition of unused `mixer` from line 2
|
= help: Remove definition: `mixer`

Unsafe fix
3 3 | except ImportError:
4 4 | pass
5 5 | else:
6 |- from bb import mixer
6 |+ pass
7 7 | mixer(123)


Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F811_17.py:6:12: F811 [*] Redefinition of unused `fu` from line 2
F811_17.py:6:12: F811 Redefinition of unused `fu` from line 2
|
5 | def bar():
6 | import fu
Expand All @@ -11,15 +11,6 @@ F811_17.py:6:12: F811 [*] Redefinition of unused `fu` from line 2
|
= help: Remove definition: `fu`

Unsafe fix
3 3 |
4 4 |
5 5 | def bar():
6 |- import fu
7 6 |
8 7 | def baz():
9 8 | def fu():

F811_17.py:9:13: F811 Redefinition of unused `fu` from line 6
|
8 | def baz():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F811_2.py:1:34: F811 [*] Redefinition of unused `FU` from line 1
F811_2.py:1:34: F811 Redefinition of unused `FU` from line 1
|
1 | from moo import fu as FU, bar as FU
| ^^ F811
|
= help: Remove definition: `FU`

Unsafe fix
1 |-from moo import fu as FU, bar as FU
1 |+from moo import fu as FU


Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F811_21.py:32:5: F811 [*] Redefinition of unused `Sequence` from line 26
F811_21.py:32:5: F811 Redefinition of unused `Sequence` from line 26
|
30 | from typing import (
31 | List, # noqa: F811
Expand All @@ -11,15 +11,4 @@ F811_21.py:32:5: F811 [*] Redefinition of unused `Sequence` from line 26
|
= help: Remove definition: `Sequence`

Unsafe fix
29 29 | # This should ignore the first error.
30 30 | from typing import (
31 31 | List, # noqa: F811
32 |- Sequence,
33 |-)
32 |+ )
34 33 |
35 34 | # This should ignore both errors.
36 35 | from typing import ( # noqa


Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F811_23.py:4:15: F811 [*] Redefinition of unused `foo` from line 3
F811_23.py:4:15: F811 Redefinition of unused `foo` from line 3
|
3 | import foo as foo
4 | import bar as foo
| ^^^ F811
|
= help: Remove definition: `foo`

Unsafe fix
1 1 | """Test that shadowing an explicit re-export produces a warning."""
2 2 |
3 3 | import foo as foo
4 |-import bar as foo


Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F811_6.py:6:12: F811 [*] Redefinition of unused `os` from line 5
F811_6.py:6:12: F811 Redefinition of unused `os` from line 5
|
4 | if i == 1:
5 | import os
Expand All @@ -11,11 +11,4 @@ F811_6.py:6:12: F811 [*] Redefinition of unused `os` from line 5
|
= help: Remove definition: `os`

Unsafe fix
3 3 | i = 2
4 4 | if i == 1:
5 5 | import os
6 |- import os
7 6 | os.path


Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F811_8.py:5:12: F811 [*] Redefinition of unused `os` from line 4
F811_8.py:5:12: F811 Redefinition of unused `os` from line 4
|
3 | try:
4 | import os
Expand All @@ -12,13 +12,4 @@ F811_8.py:5:12: F811 [*] Redefinition of unused `os` from line 4
|
= help: Remove definition: `os`

Unsafe fix
2 2 |
3 3 | try:
4 4 | import os
5 |- import os
6 5 | except:
7 6 | pass
8 7 | os.path


Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ source: crates/ruff_linter/src/rules/pyflakes/mod.rs
4 3 | def f():
5 4 | import os

<filename>:5:12: F811 [*] Redefinition of unused `os` from line 2
<filename>:5:12: F811 Redefinition of unused `os` from line 2
|
4 | def f():
5 | import os
Expand All @@ -27,13 +27,4 @@ source: crates/ruff_linter/src/rules/pyflakes/mod.rs
|
= help: Remove definition: `os`

ℹ Unsafe fix
2 2 | import os
3 3 |
4 4 | def f():
5 |- import os
6 5 |
7 6 | # Despite this `del`, `import os` in `f` should still be flagged as shadowing an unused
8 7 | # import.


Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
<filename>:4:12: F811 [*] Redefinition of unused `os` from line 3
<filename>:4:12: F811 Redefinition of unused `os` from line 3
|
2 | def f():
3 | import os
Expand All @@ -12,13 +12,4 @@ source: crates/ruff_linter/src/rules/pyflakes/mod.rs
|
= help: Remove definition: `os`

ℹ Unsafe fix
1 1 |
2 2 | def f():
3 3 | import os
4 |- import os
5 4 |
6 5 | # Despite this `del`, `import os` should still be flagged as shadowing an unused
7 6 | # import.


Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F811_0.py:10:5: F811 Redefinition of unused `bar` from line 6
|
10 | def bar():
| ^^^ F811
11 | pass
|
= help: Remove definition: `bar`


Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F811_1.py:1:25: F811 [*] Redefinition of unused `FU` from line 1
|
1 | import fu as FU, bar as FU
| ^^ F811
|
= help: Remove definition: `FU`

Safe fix
1 |-import fu as FU, bar as FU
1 |+import fu as FU


Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F811_12.py:6:20: F811 [*] Redefinition of unused `mixer` from line 2
|
4 | pass
5 | else:
6 | from bb import mixer
| ^^^^^ F811
7 | mixer(123)
|
= help: Remove definition: `mixer`

Safe fix
3 3 | except ImportError:
4 4 | pass
5 5 | else:
6 |- from bb import mixer
6 |+ pass
7 7 | mixer(123)


Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---

Loading

0 comments on commit b051d2a

Please sign in to comment.