Adjust U011 start location (#828)

This commit is contained in:
Jonathan Plasse 2022-11-20 16:13:29 +01:00 committed by GitHub
parent 4be09b45ea
commit e63ea704f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 58 deletions

View file

@ -19,9 +19,7 @@ impl Range {
pub fn from_located<T>(located: &Located<T>) -> Self { pub fn from_located<T>(located: &Located<T>) -> Self {
Range { Range {
location: located.location, location: located.location,
end_location: located end_location: located.end_location.unwrap(),
.end_location
.expect("AST nodes should have end_location."),
} }
} }
} }

View file

@ -162,12 +162,13 @@ pub fn unnecessary_lru_cache_params(
import_aliases, import_aliases,
) )
{ {
let range = Range {
location: func.end_location.unwrap(),
end_location: expr.end_location.unwrap(),
};
// Ex) `functools.lru_cache()` // Ex) `functools.lru_cache()`
if keywords.is_empty() { if keywords.is_empty() {
return Some(Check::new( return Some(Check::new(CheckKind::UnnecessaryLRUCacheParams, range));
CheckKind::UnnecessaryLRUCacheParams,
Range::from_located(expr),
));
} }
// Ex) `functools.lru_cache(maxsize=None)` // Ex) `functools.lru_cache(maxsize=None)`
if target_version >= PythonVersion::Py39 && keywords.len() == 1 { if target_version >= PythonVersion::Py39 && keywords.len() == 1 {
@ -181,10 +182,7 @@ pub fn unnecessary_lru_cache_params(
} }
) )
{ {
return Some(Check::new( return Some(Check::new(CheckKind::UnnecessaryLRUCacheParams, range));
CheckKind::UnnecessaryLRUCacheParams,
Range::from_located(expr),
));
} }
} }
} }

View file

@ -191,34 +191,3 @@ pub fn remove_unnecessary_future_import(
)) ))
} }
} }
/// U011
pub fn remove_unnecessary_lru_cache_params(
locator: &SourceCodeLocator,
decor_at: &Location,
) -> Option<Fix> {
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,
}
}

View file

@ -1,7 +1,8 @@
use rustpython_parser::ast::Expr; use rustpython_parser::ast::Expr;
use crate::autofix::Fix;
use crate::check_ast::Checker; use crate::check_ast::Checker;
use crate::pyupgrade::{checks, fixes}; use crate::pyupgrade::checks;
/// U011 /// U011
pub fn unnecessary_lru_cache_params(checker: &mut Checker, decorator_list: &[Expr]) { 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, &checker.import_aliases,
) { ) {
if checker.patch(check.kind.code()) { if checker.patch(check.kind.code()) {
if let Some(fix) = check.amend(Fix::deletion(check.location, check.end_location));
fixes::remove_unnecessary_lru_cache_params(checker.locator, &check.location)
{
check.amend(fix);
}
} }
checker.add_check(check); checker.add_check(check);
} }

View file

@ -5,7 +5,7 @@ expression: checks
- kind: UnnecessaryLRUCacheParams - kind: UnnecessaryLRUCacheParams
location: location:
row: 5 row: 5
column: 1 column: 10
end_location: end_location:
row: 5 row: 5
column: 12 column: 12
@ -22,7 +22,7 @@ expression: checks
- kind: UnnecessaryLRUCacheParams - kind: UnnecessaryLRUCacheParams
location: location:
row: 11 row: 11
column: 1 column: 20
end_location: end_location:
row: 11 row: 11
column: 22 column: 22
@ -39,7 +39,7 @@ expression: checks
- kind: UnnecessaryLRUCacheParams - kind: UnnecessaryLRUCacheParams
location: location:
row: 16 row: 16
column: 1 column: 10
end_location: end_location:
row: 16 row: 16
column: 24 column: 24
@ -56,7 +56,7 @@ expression: checks
- kind: UnnecessaryLRUCacheParams - kind: UnnecessaryLRUCacheParams
location: location:
row: 21 row: 21
column: 1 column: 20
end_location: end_location:
row: 21 row: 21
column: 34 column: 34
@ -73,7 +73,7 @@ expression: checks
- kind: UnnecessaryLRUCacheParams - kind: UnnecessaryLRUCacheParams
location: location:
row: 27 row: 27
column: 1 column: 10
end_location: end_location:
row: 28 row: 28
column: 1 column: 1
@ -90,7 +90,7 @@ expression: checks
- kind: UnnecessaryLRUCacheParams - kind: UnnecessaryLRUCacheParams
location: location:
row: 33 row: 33
column: 1 column: 10
end_location: end_location:
row: 35 row: 35
column: 1 column: 1
@ -107,7 +107,7 @@ expression: checks
- kind: UnnecessaryLRUCacheParams - kind: UnnecessaryLRUCacheParams
location: location:
row: 40 row: 40
column: 1 column: 20
end_location: end_location:
row: 42 row: 42
column: 19 column: 19
@ -124,7 +124,7 @@ expression: checks
- kind: UnnecessaryLRUCacheParams - kind: UnnecessaryLRUCacheParams
location: location:
row: 47 row: 47
column: 1 column: 20
end_location: end_location:
row: 51 row: 51
column: 1 column: 1
@ -141,7 +141,7 @@ expression: checks
- kind: UnnecessaryLRUCacheParams - kind: UnnecessaryLRUCacheParams
location: location:
row: 56 row: 56
column: 1 column: 20
end_location: end_location:
row: 62 row: 62
column: 1 column: 1
@ -158,7 +158,7 @@ expression: checks
- kind: UnnecessaryLRUCacheParams - kind: UnnecessaryLRUCacheParams
location: location:
row: 67 row: 67
column: 1 column: 20
end_location: end_location:
row: 72 row: 72
column: 1 column: 1