mirror of https://github.com/astral-sh/ruff
parent
019ecc4add
commit
c0845a8c28
|
|
@ -1,28 +1,26 @@
|
||||||
use rustpython_ast::{Constant, ExprKind, KeywordData};
|
use rustpython_ast::{Constant, ExprKind, KeywordData};
|
||||||
use rustpython_parser::ast::Expr;
|
use rustpython_parser::ast::Expr;
|
||||||
|
|
||||||
|
use crate::ast::helpers::{create_expr, unparse_expr};
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::fix::Fix;
|
use crate::fix::Fix;
|
||||||
use crate::registry::Diagnostic;
|
use crate::registry::{Diagnostic, RuleCode};
|
||||||
use crate::settings::types::PythonVersion;
|
use crate::settings::types::PythonVersion;
|
||||||
use crate::violations;
|
use crate::violations;
|
||||||
|
|
||||||
fn rule(
|
/// UP011
|
||||||
checker: &Checker,
|
pub fn unnecessary_lru_cache_params(checker: &mut Checker, decorator_list: &[Expr]) {
|
||||||
decorator_list: &[Expr],
|
|
||||||
target_version: PythonVersion,
|
|
||||||
) -> Option<Diagnostic> {
|
|
||||||
for expr in decorator_list.iter() {
|
for expr in decorator_list.iter() {
|
||||||
let ExprKind::Call {
|
let ExprKind::Call {
|
||||||
func,
|
func,
|
||||||
args,
|
args,
|
||||||
keywords,
|
keywords,
|
||||||
} = &expr.node
|
} = &expr.node else {
|
||||||
else {
|
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Look for, e.g., `import functools; @functools.lru_cache`.
|
||||||
if !(args.is_empty()
|
if !(args.is_empty()
|
||||||
&& checker
|
&& checker
|
||||||
.resolve_call_path(func)
|
.resolve_call_path(func)
|
||||||
|
|
@ -31,21 +29,29 @@ fn rule(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let range = Range::new(func.end_location.unwrap(), expr.end_location.unwrap());
|
|
||||||
// Ex) `functools.lru_cache()`
|
// Ex) `functools.lru_cache()`
|
||||||
if keywords.is_empty() {
|
if keywords.is_empty() {
|
||||||
return Some(Diagnostic::new(
|
let mut diagnostic = Diagnostic::new(
|
||||||
violations::UnnecessaryLRUCacheParams,
|
violations::UnnecessaryLRUCacheParams,
|
||||||
range,
|
Range::new(func.end_location.unwrap(), expr.end_location.unwrap()),
|
||||||
));
|
);
|
||||||
|
if checker.patch(&RuleCode::UP011) {
|
||||||
|
diagnostic.amend(Fix::replacement(
|
||||||
|
unparse_expr(func, checker.stylist),
|
||||||
|
expr.location,
|
||||||
|
expr.end_location.unwrap(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ex) `functools.lru_cache(maxsize=None)`
|
// Ex) `functools.lru_cache(maxsize=None)`
|
||||||
if !(target_version >= PythonVersion::Py39 && keywords.len() == 1) {
|
if !(checker.settings.target_version >= PythonVersion::Py39 && keywords.len() == 1) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let KeywordData { arg, value } = &keywords[0].node;
|
let KeywordData { arg, value } = &keywords[0].node;
|
||||||
if !(arg.as_ref().map(|arg| arg == "maxsize").unwrap_or_default()
|
if !(arg.as_ref().map_or(false, |arg| arg == "maxsize")
|
||||||
&& matches!(
|
&& matches!(
|
||||||
value.node,
|
value.node,
|
||||||
ExprKind::Constant {
|
ExprKind::Constant {
|
||||||
|
|
@ -56,25 +62,27 @@ fn rule(
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
return Some(Diagnostic::new(
|
|
||||||
violations::UnnecessaryLRUCacheParams,
|
|
||||||
range,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
/// UP011
|
let mut diagnostic = Diagnostic::new(
|
||||||
pub fn unnecessary_lru_cache_params(checker: &mut Checker, decorator_list: &[Expr]) {
|
violations::UnnecessaryLRUCacheParams,
|
||||||
let Some(mut diagnostic) = rule(
|
Range::new(func.end_location.unwrap(), expr.end_location.unwrap()),
|
||||||
checker,
|
);
|
||||||
decorator_list,
|
if checker.patch(&RuleCode::UP011) {
|
||||||
checker.settings.target_version,
|
if let ExprKind::Attribute { value, ctx, .. } = &func.node {
|
||||||
) else {
|
diagnostic.amend(Fix::replacement(
|
||||||
return;
|
unparse_expr(
|
||||||
};
|
&create_expr(ExprKind::Attribute {
|
||||||
if checker.patch(diagnostic.kind.code()) {
|
value: value.clone(),
|
||||||
diagnostic.amend(Fix::deletion(diagnostic.location, diagnostic.end_location));
|
attr: "cache".to_string(),
|
||||||
|
ctx: ctx.clone(),
|
||||||
|
}),
|
||||||
|
checker.stylist,
|
||||||
|
),
|
||||||
|
expr.location,
|
||||||
|
expr.end_location.unwrap(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,10 +11,10 @@ expression: diagnostics
|
||||||
row: 5
|
row: 5
|
||||||
column: 12
|
column: 12
|
||||||
fix:
|
fix:
|
||||||
content: ""
|
content: lru_cache
|
||||||
location:
|
location:
|
||||||
row: 5
|
row: 5
|
||||||
column: 10
|
column: 1
|
||||||
end_location:
|
end_location:
|
||||||
row: 5
|
row: 5
|
||||||
column: 12
|
column: 12
|
||||||
|
|
@ -28,10 +28,10 @@ expression: diagnostics
|
||||||
row: 11
|
row: 11
|
||||||
column: 22
|
column: 22
|
||||||
fix:
|
fix:
|
||||||
content: ""
|
content: functools.lru_cache
|
||||||
location:
|
location:
|
||||||
row: 11
|
row: 11
|
||||||
column: 20
|
column: 1
|
||||||
end_location:
|
end_location:
|
||||||
row: 11
|
row: 11
|
||||||
column: 22
|
column: 22
|
||||||
|
|
@ -44,14 +44,7 @@ expression: diagnostics
|
||||||
end_location:
|
end_location:
|
||||||
row: 16
|
row: 16
|
||||||
column: 24
|
column: 24
|
||||||
fix:
|
fix: ~
|
||||||
content: ""
|
|
||||||
location:
|
|
||||||
row: 16
|
|
||||||
column: 10
|
|
||||||
end_location:
|
|
||||||
row: 16
|
|
||||||
column: 24
|
|
||||||
parent: ~
|
parent: ~
|
||||||
- kind:
|
- kind:
|
||||||
UnnecessaryLRUCacheParams: ~
|
UnnecessaryLRUCacheParams: ~
|
||||||
|
|
@ -62,10 +55,10 @@ expression: diagnostics
|
||||||
row: 21
|
row: 21
|
||||||
column: 34
|
column: 34
|
||||||
fix:
|
fix:
|
||||||
content: ""
|
content: functools.cache
|
||||||
location:
|
location:
|
||||||
row: 21
|
row: 21
|
||||||
column: 20
|
column: 1
|
||||||
end_location:
|
end_location:
|
||||||
row: 21
|
row: 21
|
||||||
column: 34
|
column: 34
|
||||||
|
|
@ -79,10 +72,10 @@ expression: diagnostics
|
||||||
row: 28
|
row: 28
|
||||||
column: 1
|
column: 1
|
||||||
fix:
|
fix:
|
||||||
content: ""
|
content: lru_cache
|
||||||
location:
|
location:
|
||||||
row: 27
|
row: 27
|
||||||
column: 10
|
column: 1
|
||||||
end_location:
|
end_location:
|
||||||
row: 28
|
row: 28
|
||||||
column: 1
|
column: 1
|
||||||
|
|
@ -96,10 +89,10 @@ expression: diagnostics
|
||||||
row: 35
|
row: 35
|
||||||
column: 1
|
column: 1
|
||||||
fix:
|
fix:
|
||||||
content: ""
|
content: lru_cache
|
||||||
location:
|
location:
|
||||||
row: 33
|
row: 33
|
||||||
column: 10
|
column: 1
|
||||||
end_location:
|
end_location:
|
||||||
row: 35
|
row: 35
|
||||||
column: 1
|
column: 1
|
||||||
|
|
@ -113,10 +106,10 @@ expression: diagnostics
|
||||||
row: 42
|
row: 42
|
||||||
column: 19
|
column: 19
|
||||||
fix:
|
fix:
|
||||||
content: ""
|
content: functools.cache
|
||||||
location:
|
location:
|
||||||
row: 40
|
row: 40
|
||||||
column: 20
|
column: 1
|
||||||
end_location:
|
end_location:
|
||||||
row: 42
|
row: 42
|
||||||
column: 19
|
column: 19
|
||||||
|
|
@ -130,10 +123,10 @@ expression: diagnostics
|
||||||
row: 51
|
row: 51
|
||||||
column: 1
|
column: 1
|
||||||
fix:
|
fix:
|
||||||
content: ""
|
content: functools.cache
|
||||||
location:
|
location:
|
||||||
row: 47
|
row: 47
|
||||||
column: 20
|
column: 1
|
||||||
end_location:
|
end_location:
|
||||||
row: 51
|
row: 51
|
||||||
column: 1
|
column: 1
|
||||||
|
|
@ -147,10 +140,10 @@ expression: diagnostics
|
||||||
row: 62
|
row: 62
|
||||||
column: 1
|
column: 1
|
||||||
fix:
|
fix:
|
||||||
content: ""
|
content: functools.cache
|
||||||
location:
|
location:
|
||||||
row: 56
|
row: 56
|
||||||
column: 20
|
column: 1
|
||||||
end_location:
|
end_location:
|
||||||
row: 62
|
row: 62
|
||||||
column: 1
|
column: 1
|
||||||
|
|
@ -164,10 +157,10 @@ expression: diagnostics
|
||||||
row: 72
|
row: 72
|
||||||
column: 1
|
column: 1
|
||||||
fix:
|
fix:
|
||||||
content: ""
|
content: functools.cache
|
||||||
location:
|
location:
|
||||||
row: 67
|
row: 67
|
||||||
column: 20
|
column: 1
|
||||||
end_location:
|
end_location:
|
||||||
row: 72
|
row: 72
|
||||||
column: 1
|
column: 1
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue