From 60359c6adfef7c063a0b3540f3017837eda155d8 Mon Sep 17 00:00:00 2001 From: "Edgar R. M" Date: Tue, 3 Jan 2023 19:44:59 -0600 Subject: [PATCH] Fix leftover whitespace when removing `pass` for `PIE790` (#1612) --- resources/test/fixtures/flake8_pie/PIE790.py | 5 + src/flake8_pie/plugins.rs | 14 +- ...__flake8_pie__tests__PIE790_PIE790.py.snap | 170 ++++++++++-------- 3 files changed, 108 insertions(+), 81 deletions(-) diff --git a/resources/test/fixtures/flake8_pie/PIE790.py b/resources/test/fixtures/flake8_pie/PIE790.py index d9888d5b20577..8c3f45ad0bc66 100644 --- a/resources/test/fixtures/flake8_pie/PIE790.py +++ b/resources/test/fixtures/flake8_pie/PIE790.py @@ -9,6 +9,11 @@ class Foo: pass # PIE790 +def multi_statement() -> None: + """This is a function.""" + pass; print("hello") + + if foo: pass else: diff --git a/src/flake8_pie/plugins.rs b/src/flake8_pie/plugins.rs index 0b68ac2debd22..421b9ecadc556 100644 --- a/src/flake8_pie/plugins.rs +++ b/src/flake8_pie/plugins.rs @@ -1,7 +1,9 @@ +use log::error; use rustc_hash::FxHashSet; use rustpython_ast::{Constant, Expr, ExprKind, Stmt, StmtKind}; use crate::ast::types::Range; +use crate::autofix::helpers::delete_stmt; use crate::autofix::Fix; use crate::checkers::ast::Checker; use crate::registry::{Check, CheckCode, CheckKind}; @@ -27,10 +29,14 @@ pub fn no_unnecessary_pass(checker: &mut Checker, body: &[Stmt]) { let mut check = Check::new(CheckKind::NoUnnecessaryPass, Range::from_located(pass_stmt)); if checker.patch(&CheckCode::PIE790) { - check.amend(Fix::deletion( - pass_stmt.location, - pass_stmt.end_location.unwrap(), - )); + match delete_stmt(pass_stmt, None, &[], checker.locator) { + Ok(fix) => { + check.amend(fix); + } + Err(e) => { + error!("Failed to delete `pass` statement: {}", e); + } + } } checker.add_check(check); } diff --git a/src/flake8_pie/snapshots/ruff__flake8_pie__tests__PIE790_PIE790.py.snap b/src/flake8_pie/snapshots/ruff__flake8_pie__tests__PIE790_PIE790.py.snap index 8ede770470f8c..2288dabfa94ff 100644 --- a/src/flake8_pie/snapshots/ruff__flake8_pie__tests__PIE790_PIE790.py.snap +++ b/src/flake8_pie/snapshots/ruff__flake8_pie__tests__PIE790_PIE790.py.snap @@ -13,10 +13,10 @@ expression: checks content: "" location: row: 4 - column: 4 + column: 0 end_location: - row: 4 - column: 8 + row: 5 + column: 0 parent: ~ - kind: NoUnnecessaryPass location: @@ -29,138 +29,138 @@ expression: checks content: "" location: row: 9 - column: 4 + column: 0 end_location: - row: 9 - column: 8 + row: 10 + column: 0 parent: ~ - kind: NoUnnecessaryPass location: - row: 16 + row: 14 column: 4 end_location: - row: 16 + row: 14 column: 8 fix: content: "" location: - row: 16 + row: 14 column: 4 end_location: - row: 16 - column: 8 + row: 14 + column: 10 parent: ~ - kind: NoUnnecessaryPass location: - row: 23 + row: 21 column: 4 end_location: - row: 23 + row: 21 column: 8 fix: content: "" location: - row: 23 - column: 4 + row: 21 + column: 0 end_location: - row: 23 - column: 8 + row: 22 + column: 0 parent: ~ - kind: NoUnnecessaryPass location: - row: 30 + row: 28 column: 4 end_location: - row: 30 + row: 28 column: 8 fix: content: "" location: - row: 30 - column: 4 + row: 28 + column: 0 end_location: - row: 30 - column: 8 + row: 29 + column: 0 parent: ~ - kind: NoUnnecessaryPass location: - row: 37 + row: 35 column: 4 end_location: - row: 37 + row: 35 column: 8 fix: content: "" location: - row: 37 - column: 4 + row: 35 + column: 0 end_location: - row: 37 - column: 8 + row: 36 + column: 0 parent: ~ - kind: NoUnnecessaryPass location: - row: 45 + row: 42 column: 4 end_location: - row: 45 + row: 42 column: 8 fix: content: "" location: - row: 45 - column: 4 + row: 42 + column: 0 end_location: - row: 45 - column: 8 + row: 43 + column: 0 parent: ~ - kind: NoUnnecessaryPass location: - row: 53 + row: 50 column: 4 end_location: - row: 53 + row: 50 column: 8 fix: content: "" location: - row: 53 - column: 4 + row: 50 + column: 0 end_location: - row: 53 - column: 8 + row: 51 + column: 0 parent: ~ - kind: NoUnnecessaryPass location: - row: 60 + row: 58 column: 4 end_location: - row: 60 + row: 58 column: 8 fix: content: "" location: - row: 60 - column: 4 + row: 58 + column: 0 end_location: - row: 60 - column: 8 + row: 59 + column: 0 parent: ~ - kind: NoUnnecessaryPass location: - row: 69 + row: 65 column: 4 end_location: - row: 69 + row: 65 column: 8 fix: content: "" location: - row: 69 - column: 4 + row: 65 + column: 0 end_location: - row: 69 - column: 8 + row: 66 + column: 0 parent: ~ - kind: NoUnnecessaryPass location: @@ -173,42 +173,42 @@ expression: checks content: "" location: row: 74 - column: 4 + column: 0 end_location: - row: 74 - column: 8 + row: 75 + column: 0 parent: ~ - kind: NoUnnecessaryPass location: - row: 78 + row: 79 column: 4 end_location: - row: 78 + row: 79 column: 8 fix: content: "" location: - row: 78 - column: 4 + row: 79 + column: 0 end_location: - row: 78 - column: 8 + row: 80 + column: 0 parent: ~ - kind: NoUnnecessaryPass location: - row: 82 + row: 83 column: 4 end_location: - row: 82 + row: 83 column: 8 fix: content: "" location: - row: 82 - column: 4 + row: 83 + column: 0 end_location: - row: 82 - column: 8 + row: 84 + column: 0 parent: ~ - kind: NoUnnecessaryPass location: @@ -221,25 +221,41 @@ expression: checks content: "" location: row: 87 - column: 4 + column: 0 end_location: - row: 87 - column: 8 + row: 88 + column: 0 parent: ~ - kind: NoUnnecessaryPass location: - row: 91 + row: 92 column: 4 end_location: - row: 91 + row: 92 column: 8 fix: content: "" location: - row: 91 - column: 4 + row: 92 + column: 0 + end_location: + row: 93 + column: 0 + parent: ~ +- kind: NoUnnecessaryPass + location: + row: 96 + column: 4 + end_location: + row: 96 + column: 8 + fix: + content: "" + location: + row: 96 + column: 0 end_location: - row: 91 - column: 8 + row: 97 + column: 0 parent: ~