From e63ea704f0449c2fc864e6b9dfd7c8c7536152e4 Mon Sep 17 00:00:00 2001 From: Jonathan Plasse <13716151+JonathanPlasse@users.noreply.github.com> Date: Sun, 20 Nov 2022 16:13:29 +0100 Subject: [PATCH] Adjust U011 start location (#828) --- src/ast/types.rs | 4 +-- src/pyupgrade/checks.rs | 14 ++++----- src/pyupgrade/fixes.rs | 31 ------------------- .../plugins/unnecessary_lru_cache_params.rs | 9 ++---- .../ruff__linter__tests__U011_U011_0.py.snap | 20 ++++++------ 5 files changed, 20 insertions(+), 58 deletions(-) diff --git a/src/ast/types.rs b/src/ast/types.rs index 70aad5f438..3821a469bf 100644 --- a/src/ast/types.rs +++ b/src/ast/types.rs @@ -19,9 +19,7 @@ impl Range { pub fn from_located(located: &Located) -> Self { Range { location: located.location, - end_location: located - .end_location - .expect("AST nodes should have end_location."), + end_location: located.end_location.unwrap(), } } } diff --git a/src/pyupgrade/checks.rs b/src/pyupgrade/checks.rs index f9d80b0e67..3ef31d0313 100644 --- a/src/pyupgrade/checks.rs +++ b/src/pyupgrade/checks.rs @@ -162,12 +162,13 @@ pub fn unnecessary_lru_cache_params( import_aliases, ) { + let range = Range { + location: func.end_location.unwrap(), + end_location: expr.end_location.unwrap(), + }; // Ex) `functools.lru_cache()` if keywords.is_empty() { - return Some(Check::new( - CheckKind::UnnecessaryLRUCacheParams, - Range::from_located(expr), - )); + return Some(Check::new(CheckKind::UnnecessaryLRUCacheParams, range)); } // Ex) `functools.lru_cache(maxsize=None)` if target_version >= PythonVersion::Py39 && keywords.len() == 1 { @@ -181,10 +182,7 @@ pub fn unnecessary_lru_cache_params( } ) { - return Some(Check::new( - CheckKind::UnnecessaryLRUCacheParams, - Range::from_located(expr), - )); + return Some(Check::new(CheckKind::UnnecessaryLRUCacheParams, range)); } } } diff --git a/src/pyupgrade/fixes.rs b/src/pyupgrade/fixes.rs index 195742149d..b329095911 100644 --- a/src/pyupgrade/fixes.rs +++ b/src/pyupgrade/fixes.rs @@ -191,34 +191,3 @@ pub fn remove_unnecessary_future_import( )) } } - -/// U011 -pub fn remove_unnecessary_lru_cache_params( - locator: &SourceCodeLocator, - decor_at: &Location, -) -> Option { - let contents = locator.slice_source_code_at(decor_at); - let mut fix_start = None; - let mut fix_end = None; - let mut count: usize = 0; - for (start, tok, end) in lexer::make_tokenizer(&contents).flatten() { - if matches!(tok, Tok::Lpar) { - if count == 0 { - fix_start = Some(helpers::to_absolute(&start, decor_at)); - } - count += 1; - } - - if matches!(tok, Tok::Rpar) { - count -= 1; - if count == 0 { - fix_end = Some(helpers::to_absolute(&end, decor_at)); - break; - } - } - } - match (fix_start, fix_end) { - (Some(start), Some(end)) => Some(Fix::deletion(start, end)), - _ => None, - } -} diff --git a/src/pyupgrade/plugins/unnecessary_lru_cache_params.rs b/src/pyupgrade/plugins/unnecessary_lru_cache_params.rs index 5e1ecca7e9..a255350376 100644 --- a/src/pyupgrade/plugins/unnecessary_lru_cache_params.rs +++ b/src/pyupgrade/plugins/unnecessary_lru_cache_params.rs @@ -1,7 +1,8 @@ use rustpython_parser::ast::Expr; +use crate::autofix::Fix; use crate::check_ast::Checker; -use crate::pyupgrade::{checks, fixes}; +use crate::pyupgrade::checks; /// U011 pub fn unnecessary_lru_cache_params(checker: &mut Checker, decorator_list: &[Expr]) { @@ -12,11 +13,7 @@ pub fn unnecessary_lru_cache_params(checker: &mut Checker, decorator_list: &[Exp &checker.import_aliases, ) { if checker.patch(check.kind.code()) { - if let Some(fix) = - fixes::remove_unnecessary_lru_cache_params(checker.locator, &check.location) - { - check.amend(fix); - } + check.amend(Fix::deletion(check.location, check.end_location)); } checker.add_check(check); } diff --git a/src/snapshots/ruff__linter__tests__U011_U011_0.py.snap b/src/snapshots/ruff__linter__tests__U011_U011_0.py.snap index 949484ddde..7f2cec5783 100644 --- a/src/snapshots/ruff__linter__tests__U011_U011_0.py.snap +++ b/src/snapshots/ruff__linter__tests__U011_U011_0.py.snap @@ -5,7 +5,7 @@ expression: checks - kind: UnnecessaryLRUCacheParams location: row: 5 - column: 1 + column: 10 end_location: row: 5 column: 12 @@ -22,7 +22,7 @@ expression: checks - kind: UnnecessaryLRUCacheParams location: row: 11 - column: 1 + column: 20 end_location: row: 11 column: 22 @@ -39,7 +39,7 @@ expression: checks - kind: UnnecessaryLRUCacheParams location: row: 16 - column: 1 + column: 10 end_location: row: 16 column: 24 @@ -56,7 +56,7 @@ expression: checks - kind: UnnecessaryLRUCacheParams location: row: 21 - column: 1 + column: 20 end_location: row: 21 column: 34 @@ -73,7 +73,7 @@ expression: checks - kind: UnnecessaryLRUCacheParams location: row: 27 - column: 1 + column: 10 end_location: row: 28 column: 1 @@ -90,7 +90,7 @@ expression: checks - kind: UnnecessaryLRUCacheParams location: row: 33 - column: 1 + column: 10 end_location: row: 35 column: 1 @@ -107,7 +107,7 @@ expression: checks - kind: UnnecessaryLRUCacheParams location: row: 40 - column: 1 + column: 20 end_location: row: 42 column: 19 @@ -124,7 +124,7 @@ expression: checks - kind: UnnecessaryLRUCacheParams location: row: 47 - column: 1 + column: 20 end_location: row: 51 column: 1 @@ -141,7 +141,7 @@ expression: checks - kind: UnnecessaryLRUCacheParams location: row: 56 - column: 1 + column: 20 end_location: row: 62 column: 1 @@ -158,7 +158,7 @@ expression: checks - kind: UnnecessaryLRUCacheParams location: row: 67 - column: 1 + column: 20 end_location: row: 72 column: 1