mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 21:35:58 +00:00
Add extend-immutable-calls
setting for B008 (#706)
This commit is contained in:
parent
2493d48725
commit
aa7681f9ad
11 changed files with 135 additions and 11 deletions
|
@ -23,23 +23,27 @@ const IMMUTABLE_FUNCS: [&str; 11] = [
|
|||
"re.compile",
|
||||
];
|
||||
|
||||
fn is_immutable_func(expr: &Expr) -> bool {
|
||||
compose_call_path(expr).map_or_else(|| false, |func| IMMUTABLE_FUNCS.contains(&func.as_str()))
|
||||
fn is_immutable_func(expr: &Expr, extend_immutable_calls: &[String]) -> bool {
|
||||
compose_call_path(expr).map_or_else(
|
||||
|| false,
|
||||
|func| IMMUTABLE_FUNCS.contains(&func.as_str()) || extend_immutable_calls.contains(&func),
|
||||
)
|
||||
}
|
||||
|
||||
struct ArgumentDefaultVisitor {
|
||||
struct ArgumentDefaultVisitor<'a> {
|
||||
checks: Vec<(CheckKind, Range)>,
|
||||
extend_immutable_calls: &'a [String],
|
||||
}
|
||||
|
||||
impl<'a, 'b> Visitor<'b> for ArgumentDefaultVisitor
|
||||
impl<'a, 'b> Visitor<'b> for ArgumentDefaultVisitor<'b>
|
||||
where
|
||||
'b: 'a,
|
||||
{
|
||||
fn visit_expr(&mut self, expr: &'a Expr) {
|
||||
fn visit_expr(&mut self, expr: &'b Expr) {
|
||||
match &expr.node {
|
||||
ExprKind::Call { func, args, .. } => {
|
||||
if !is_mutable_func(func)
|
||||
&& !is_immutable_func(func)
|
||||
&& !is_immutable_func(func, &self.extend_immutable_calls)
|
||||
&& !is_nan_or_infinity(func, args)
|
||||
{
|
||||
self.checks.push((
|
||||
|
@ -83,7 +87,10 @@ fn is_nan_or_infinity(expr: &Expr, args: &[Expr]) -> bool {
|
|||
|
||||
/// B008
|
||||
pub fn function_call_argument_default(checker: &mut Checker, arguments: &Arguments) {
|
||||
let mut visitor = ArgumentDefaultVisitor { checks: vec![] };
|
||||
let mut visitor = ArgumentDefaultVisitor {
|
||||
checks: vec![],
|
||||
extend_immutable_calls: &checker.settings.flake8_bugbear.extend_immutable_calls,
|
||||
};
|
||||
for expr in arguments
|
||||
.defaults
|
||||
.iter()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue