Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flake8-pie PIE800: no-unnecessary-spread #1881

Merged
merged 8 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix
  • Loading branch information
sbdchd committed Jan 22, 2023
commit ba8911daba626b63c31a2ec56da9b1cec150668e
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ For more, see [flake8-pie](https://pypi.org/project/flake8-pie/) on PyPI.
| PIE790 | no-unnecessary-pass | Unnecessary `pass` statement | 🛠 |
| PIE794 | dupe-class-field-definitions | Class field `{name}` is defined multiple times | 🛠 |
| PIE796 | prefer-unique-enums | Enum contains duplicate value: `{value}` | |
| PIE800 | no-unnecessary-spread | Unnecessary spread `**` | 🛠 |
| PIE800 | no-unnecessary-spread | Unnecessary spread `**` | |
| PIE807 | prefer-list-builtin | Prefer `list()` over useless lambda | 🛠 |

### flake8-commas (COM)
Expand Down
2 changes: 1 addition & 1 deletion resources/test/fixtures/flake8_pie/PIE800.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{**foo, "bar": True }
{**foo, "bar": True } # okay
{"foo": 1, **{"bar": 1}} # PIE800

foo({**foo, **{"bar": True}}) # PIE800
Expand Down
2 changes: 1 addition & 1 deletion src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2592,7 +2592,7 @@ where
pyflakes::rules::repeated_keys(self, keys, values);
}

if self.settings.enabled.contains(&RuleCode::PIE800) {
if self.settings.rules.enabled(&Rule::NoUnnecessarySpread) {
flake8_pie::rules::no_unnecessary_spread(self, keys, values);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/flake8_pie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod tests {
#[test_case(Rule::NoUnnecessaryPass, Path::new("PIE790.py"); "PIE790")]
#[test_case(Rule::DupeClassFieldDefinitions, Path::new("PIE794.py"); "PIE794")]
#[test_case(Rule::PreferUniqueEnums, Path::new("PIE796.py"); "PIE796")]
#[test_case(RuleCode::PIE800, Path::new("PIE800.py"); "PIE800")]
#[test_case(Rule::NoUnnecessarySpread, Path::new("PIE800.py"); "PIE800")]
#[test_case(Rule::PreferListBuiltin, Path::new("PIE807.py"); "PIE807")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy());
Expand Down
22 changes: 6 additions & 16 deletions src/rules/flake8_pie/rules.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use itertools::EitherOrBoth;
use itertools::Itertools;
use log::error;
use rustc_hash::FxHashSet;
use rustpython_ast::Located;
Expand Down Expand Up @@ -155,33 +153,25 @@ where
}
}


/// PIE800
pub fn no_unnecessary_spread(checker: &mut Checker, keys: &[Expr], values: &[Expr]) {
for item in keys.iter().zip_longest(values.iter()) {
pub fn no_unnecessary_spread(checker: &mut Checker, keys: &[Option<Expr>], values: &[Expr]) {
for item in keys.iter().zip(values.iter()) {
match item {
EitherOrBoth::Both(_, _) => {}
EitherOrBoth::Left(_) => {}
EitherOrBoth::Right(value) => {
// We only care about when the key is None which indicates a spread `**` inside a dict
(None, value) => {
if let Located {
node: ExprKind::Dict { .. },
..
} = value
{
let mut diagnostic = Diagnostic::new(
let diagnostic = Diagnostic::new(
violations::NoUnnecessarySpread,
Range::from_located(value),
);
// if checker.patch(&RuleCode::PIE807) {
// diagnostic.amend(Fix::replacement(
// "pooooooooooooooooo".to_string(),
// value.location,
// value.end_location.unwrap(),
// ));
// }
checker.diagnostics.push(diagnostic);
}
}
(_, _) => {}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@
source: src/rules/flake8_pie/mod.rs
expression: diagnostics
---
- kind:
NoUnnecessarySpread: ~
location:
row: 1
column: 11
end_location:
row: 1
column: 16
fix: ~
parent: ~
- kind:
NoUnnecessarySpread: ~
location:
Expand Down Expand Up @@ -52,24 +42,4 @@ expression: diagnostics
column: 27
fix: ~
parent: ~
- kind:
NoUnnecessarySpread: ~
location:
row: 12
column: 11
end_location:
row: 12
column: 16
fix: ~
parent: ~
- kind:
NoUnnecessarySpread: ~
location:
row: 16
column: 15
end_location:
row: 16
column: 20
fix: ~
parent: ~

11 changes: 2 additions & 9 deletions src/violations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4987,18 +4987,11 @@ impl Violation for PreferUniqueEnums {
define_violation!(
pub struct NoUnnecessarySpread;
);
impl AlwaysAutofixableViolation for NoUnnecessarySpread {
impl Violation for NoUnnecessarySpread {
#[derive_message_formats]
fn message(&self) -> String {
format!("Unnecessary spread `**`")
}

fn autofix_title(&self) -> String {
format!("Remove unnecessary spread `**`")
}

fn placeholder() -> Self {
NoUnnecessarySpread
}
}

define_violation!(
Expand Down