Skip to content

Commit

Permalink
fixup! Add multiline test case
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhdu committed Nov 14, 2023
1 parent 66d04fa commit 99f291b
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

{**foo, **{"bar": 10}} # PIE800

{**{"bar": 10}, "a": "b"} # PIE800

{ # PIE800
**foo,
**{
"bar": 10
"bar": 10,
},
}

Expand Down
38 changes: 24 additions & 14 deletions crates/ruff_linter/src/rules/flake8_pie/rules/unnecessary_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,32 @@ pub(crate) fn unnecessary_spread(checker: &mut Checker, keys: &[Option<Expr>], v
if let (None, value) = item {
// We only care about when the key is None which indicates a spread `**`
// inside a dict.
if let Expr::Dict(_) = value {
if let Expr::Dict(dict) = value {
let mut diagnostic = Diagnostic::new(UnnecessarySpread, value.range());
if checker.settings.preview.is_enabled() {
let range = value.range();
// unwrap -- `item.0 == None` iff this is a spread operator
// which means there *must* be a `**` here
let doublestar = checker.locator().up_to(range.start()).rfind("**").unwrap();
diagnostic.set_fix(Fix::safe_edits(
// delete the `**{`
Edit::deletion(
TextSize::from(doublestar as u32),
range.start() + TextSize::from(1),
),
// delete the `}`
[Edit::deletion(range.end() - TextSize::from(1), range.end())],
));
// Delete the `**{`
let doublestar = checker
.locator()
.up_to(dict.range.start())
.rfind("**")
.unwrap(); // If item.0 is None, then we *must* have a `**`
let first_pos = match dict.keys.iter().next() {
Some(Some(inner_key)) => inner_key.range().start(),
_ => dict.range.start() + TextSize::from(1)
};
let first = Edit::deletion(
TextSize::from(doublestar as u32),
first_pos,
);

// Delete the `}`
let last_pos = match dict.values.iter().rev().next() {
Some(inner_value) => inner_value.range().end(),
_ => dict.range.end() - TextSize::from(1),
};
let second = Edit::deletion(last_pos, dict.range.end());

diagnostic.set_fix(Fix::safe_edits(first, [second]));
}
checker.diagnostics.push(diagnostic);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,42 @@ PIE800.py:5:11: PIE800 Unnecessary spread `**`
5 | {**foo, **{"bar": 10}} # PIE800
| ^^^^^^^^^^^ PIE800
6 |
7 | { # PIE800
7 | {**{"bar": 10}, "a": "b"} # PIE800
|
= help: Remove unnecessary dict

PIE800.py:9:7: PIE800 Unnecessary spread `**`
PIE800.py:7:4: PIE800 Unnecessary spread `**`
|
5 | {**foo, **{"bar": 10}} # PIE800
6 |
7 | {**{"bar": 10}, "a": "b"} # PIE800
| ^^^^^^^^^^^ PIE800
8 |
9 | { # PIE800
|
= help: Remove unnecessary dict

PIE800.py:11:7: PIE800 Unnecessary spread `**`
|
7 | { # PIE800
8 | **foo,
9 | **{
9 | { # PIE800
10 | **foo,
11 | **{
| _______^
10 | | "bar": 10
11 | | },
12 | | "bar": 10,
13 | | },
| |_____^ PIE800
12 | }
14 | }
|
= help: Remove unnecessary dict

PIE800.py:14:19: PIE800 Unnecessary spread `**`
PIE800.py:16:19: PIE800 Unnecessary spread `**`
|
12 | }
13 |
14 | {**foo, **buzz, **{bar: 10}} # PIE800
| ^^^^^^^^^ PIE800
14 | }
15 |
16 | {**foo, "bar": True } # OK
16 | {**foo, **buzz, **{bar: 10}} # PIE800
| ^^^^^^^^^ PIE800
17 |
18 | {**foo, "bar": True } # OK
|
= help: Remove unnecessary dict

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ PIE800.py:5:11: PIE800 [*] Unnecessary spread `**`
5 | {**foo, **{"bar": 10}} # PIE800
| ^^^^^^^^^^^ PIE800
6 |
7 | { # PIE800
7 | {**{"bar": 10}, "a": "b"} # PIE800
|
= help: Remove unnecessary dict

Expand All @@ -55,54 +55,74 @@ PIE800.py:5:11: PIE800 [*] Unnecessary spread `**`
5 |-{**foo, **{"bar": 10}} # PIE800
5 |+{**foo, "bar": 10} # PIE800
6 6 |
7 7 | { # PIE800
8 8 | **foo,
7 7 | {**{"bar": 10}, "a": "b"} # PIE800
8 8 |

PIE800.py:9:7: PIE800 [*] Unnecessary spread `**`
PIE800.py:7:4: PIE800 [*] Unnecessary spread `**`
|
5 | {**foo, **{"bar": 10}} # PIE800
6 |
7 | {**{"bar": 10}, "a": "b"} # PIE800
| ^^^^^^^^^^^ PIE800
8 |
9 | { # PIE800
|
= help: Remove unnecessary dict

Safe fix
4 4 |
5 5 | {**foo, **{"bar": 10}} # PIE800
6 6 |
7 |-{**{"bar": 10}, "a": "b"} # PIE800
7 |+{"bar": 10, "a": "b"} # PIE800
8 8 |
9 9 | { # PIE800
10 10 | **foo,

PIE800.py:11:7: PIE800 [*] Unnecessary spread `**`
|
7 | { # PIE800
8 | **foo,
9 | **{
9 | { # PIE800
10 | **foo,
11 | **{
| _______^
10 | | "bar": 10
11 | | },
12 | | "bar": 10,
13 | | },
| |_____^ PIE800
12 | }
14 | }
|
= help: Remove unnecessary dict

Safe fix
6 6 |
7 7 | { # PIE800
8 8 | **foo,
9 |- **{
9 |+
10 10 | "bar": 10
11 |- },
11 |+ ,
12 12 | }
13 13 |
14 14 | {**foo, **buzz, **{bar: 10}} # PIE800
8 8 |
9 9 | { # PIE800
10 10 | **foo,
11 |- **{
12 |- "bar": 10,
13 |- },
11 |+ "bar": 10,
14 12 | }
15 13 |
16 14 | {**foo, **buzz, **{bar: 10}} # PIE800

PIE800.py:14:19: PIE800 [*] Unnecessary spread `**`
PIE800.py:16:19: PIE800 [*] Unnecessary spread `**`
|
12 | }
13 |
14 | {**foo, **buzz, **{bar: 10}} # PIE800
| ^^^^^^^^^ PIE800
14 | }
15 |
16 | {**foo, "bar": True } # OK
16 | {**foo, **buzz, **{bar: 10}} # PIE800
| ^^^^^^^^^ PIE800
17 |
18 | {**foo, "bar": True } # OK
|
= help: Remove unnecessary dict

Safe fix
11 11 | },
12 12 | }
13 13 |
14 |-{**foo, **buzz, **{bar: 10}} # PIE800
14 |+{**foo, **buzz, bar: 10} # PIE800
13 13 | },
14 14 | }
15 15 |
16 16 | {**foo, "bar": True } # OK
16 |-{**foo, **buzz, **{bar: 10}} # PIE800
16 |+{**foo, **buzz, bar: 10} # PIE800
17 17 |
18 18 | {**foo, "bar": True } # OK
19 19 |


0 comments on commit 99f291b

Please sign in to comment.