mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:09:22 +00:00
Simplify iteration idioms (#13834)
Remove unnecessary uses of `.as_ref()`, `.iter()`, `&**` and similar, mostly in situations when iterating over variables. Many of these changes are only possible following #13826, when we bumped our MSRV to 1.80: several useful implementations on `&Box<[T]>` were only stabilised in Rust 1.80. Some of these changes we could have done earlier, however.
This commit is contained in:
parent
7fd8e30eed
commit
72adb09bf3
36 changed files with 72 additions and 73 deletions
|
@ -1012,7 +1012,7 @@ where
|
||||||
|
|
||||||
// Add symbols and definitions for the parameters to the lambda scope.
|
// Add symbols and definitions for the parameters to the lambda scope.
|
||||||
if let Some(parameters) = &lambda.parameters {
|
if let Some(parameters) = &lambda.parameters {
|
||||||
for parameter in &**parameters {
|
for parameter in parameters {
|
||||||
self.declare_parameter(parameter);
|
self.declare_parameter(parameter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -597,7 +597,7 @@ impl<'db> Type<'db> {
|
||||||
tuple
|
tuple
|
||||||
.elements(db)
|
.elements(db)
|
||||||
.iter()
|
.iter()
|
||||||
.zip(other_tuple.elements(db).iter())
|
.zip(other_tuple.elements(db))
|
||||||
.any(|(e1, e2)| e1.is_disjoint_from(db, *e2))
|
.any(|(e1, e2)| e1.is_disjoint_from(db, *e2))
|
||||||
} else {
|
} else {
|
||||||
true
|
true
|
||||||
|
@ -863,7 +863,7 @@ impl<'db> Type<'db> {
|
||||||
fn iterate(self, db: &'db dyn Db) -> IterationOutcome<'db> {
|
fn iterate(self, db: &'db dyn Db) -> IterationOutcome<'db> {
|
||||||
if let Type::Tuple(tuple_type) = self {
|
if let Type::Tuple(tuple_type) = self {
|
||||||
return IterationOutcome::Iterable {
|
return IterationOutcome::Iterable {
|
||||||
element_ty: UnionType::from_elements(db, &**tuple_type.elements(db)),
|
element_ty: UnionType::from_elements(db, tuple_type.elements(db)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1310,7 +1310,7 @@ impl<'db> CallOutcome<'db> {
|
||||||
let mut not_callable = vec![];
|
let mut not_callable = vec![];
|
||||||
let mut union_builder = UnionBuilder::new(db);
|
let mut union_builder = UnionBuilder::new(db);
|
||||||
let mut revealed = false;
|
let mut revealed = false;
|
||||||
for outcome in &**outcomes {
|
for outcome in outcomes {
|
||||||
let return_ty = match outcome {
|
let return_ty = match outcome {
|
||||||
Self::NotCallable { not_callable_ty } => {
|
Self::NotCallable { not_callable_ty } => {
|
||||||
not_callable.push(*not_callable_ty);
|
not_callable.push(*not_callable_ty);
|
||||||
|
|
|
@ -2809,7 +2809,7 @@ impl<'db> TypeInferenceBuilder<'db> {
|
||||||
} = compare;
|
} = compare;
|
||||||
|
|
||||||
self.infer_expression(left);
|
self.infer_expression(left);
|
||||||
for right in comparators.as_ref() {
|
for right in comparators {
|
||||||
self.infer_expression(right);
|
self.infer_expression(right);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2823,10 +2823,10 @@ impl<'db> TypeInferenceBuilder<'db> {
|
||||||
Self::infer_chained_boolean_types(
|
Self::infer_chained_boolean_types(
|
||||||
self.db,
|
self.db,
|
||||||
ast::BoolOp::And,
|
ast::BoolOp::And,
|
||||||
std::iter::once(left.as_ref())
|
std::iter::once(&**left)
|
||||||
.chain(comparators.as_ref().iter())
|
.chain(comparators)
|
||||||
.tuple_windows::<(_, _)>()
|
.tuple_windows::<(_, _)>()
|
||||||
.zip(ops.iter())
|
.zip(ops)
|
||||||
.map(|((left, right), op)| {
|
.map(|((left, right), op)| {
|
||||||
let left_ty = self.expression_ty(left);
|
let left_ty = self.expression_ty(left);
|
||||||
let right_ty = self.expression_ty(right);
|
let right_ty = self.expression_ty(right);
|
||||||
|
@ -3036,8 +3036,8 @@ impl<'db> TypeInferenceBuilder<'db> {
|
||||||
}
|
}
|
||||||
(Type::Tuple(lhs), Type::Tuple(rhs)) => {
|
(Type::Tuple(lhs), Type::Tuple(rhs)) => {
|
||||||
// Note: This only works on heterogeneous tuple types.
|
// Note: This only works on heterogeneous tuple types.
|
||||||
let lhs_elements = lhs.elements(self.db).as_ref();
|
let lhs_elements = lhs.elements(self.db);
|
||||||
let rhs_elements = rhs.elements(self.db).as_ref();
|
let rhs_elements = rhs.elements(self.db);
|
||||||
|
|
||||||
let mut lexicographic_type_comparison =
|
let mut lexicographic_type_comparison =
|
||||||
|op| self.infer_lexicographic_type_comparison(lhs_elements, op, rhs_elements);
|
|op| self.infer_lexicographic_type_comparison(lhs_elements, op, rhs_elements);
|
||||||
|
|
|
@ -153,7 +153,7 @@ impl<'db> NarrowingConstraintsBuilder<'db> {
|
||||||
let symbol = self.symbols().symbol_id_by_name(id).unwrap();
|
let symbol = self.symbols().symbol_id_by_name(id).unwrap();
|
||||||
let scope = self.scope();
|
let scope = self.scope();
|
||||||
let inference = infer_expression_types(self.db, expression);
|
let inference = infer_expression_types(self.db, expression);
|
||||||
for (op, comparator) in std::iter::zip(&**ops, &**comparators) {
|
for (op, comparator) in std::iter::zip(ops, comparators) {
|
||||||
let comp_ty = inference.expression_ty(comparator.scoped_ast_id(self.db, scope));
|
let comp_ty = inference.expression_ty(comparator.scoped_ast_id(self.db, scope));
|
||||||
match op {
|
match op {
|
||||||
ast::CmpOp::IsNot => {
|
ast::CmpOp::IsNot => {
|
||||||
|
|
|
@ -732,7 +732,7 @@ impl<'a> Visitor<'a> for Checker<'a> {
|
||||||
|
|
||||||
// The default values of the parameters needs to be evaluated in the enclosing
|
// The default values of the parameters needs to be evaluated in the enclosing
|
||||||
// scope.
|
// scope.
|
||||||
for parameter in &**parameters {
|
for parameter in parameters {
|
||||||
if let Some(expr) = parameter.default() {
|
if let Some(expr) = parameter.default() {
|
||||||
self.visit_expr(expr);
|
self.visit_expr(expr);
|
||||||
}
|
}
|
||||||
|
@ -744,7 +744,7 @@ impl<'a> Visitor<'a> for Checker<'a> {
|
||||||
self.visit_type_params(type_params);
|
self.visit_type_params(type_params);
|
||||||
}
|
}
|
||||||
|
|
||||||
for parameter in &**parameters {
|
for parameter in parameters {
|
||||||
if let Some(expr) = parameter.annotation() {
|
if let Some(expr) = parameter.annotation() {
|
||||||
if singledispatch && !parameter.is_variadic() {
|
if singledispatch && !parameter.is_variadic() {
|
||||||
self.visit_runtime_required_annotation(expr);
|
self.visit_runtime_required_annotation(expr);
|
||||||
|
|
|
@ -141,7 +141,7 @@ pub(crate) fn fastapi_unused_path_parameter(
|
||||||
.parameters
|
.parameters
|
||||||
.args
|
.args
|
||||||
.iter()
|
.iter()
|
||||||
.chain(function_def.parameters.kwonlyargs.iter())
|
.chain(&function_def.parameters.kwonlyargs)
|
||||||
.map(|ParameterWithDefault { parameter, .. }| {
|
.map(|ParameterWithDefault { parameter, .. }| {
|
||||||
parameter_alias(parameter, checker.semantic())
|
parameter_alias(parameter, checker.semantic())
|
||||||
.unwrap_or_else(|| parameter.name.as_str())
|
.unwrap_or_else(|| parameter.name.as_str())
|
||||||
|
|
|
@ -116,7 +116,7 @@ fn is_infinite_iterator(arg: &Expr, semantic: &SemanticModel) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ex) `iterools.repeat(1, times=None)`
|
// Ex) `iterools.repeat(1, times=None)`
|
||||||
for keyword in &**keywords {
|
for keyword in keywords {
|
||||||
if keyword.arg.as_ref().is_some_and(|name| name == "times") {
|
if keyword.arg.as_ref().is_some_and(|name| name == "times") {
|
||||||
if keyword.value.is_none_literal_expr() {
|
if keyword.value.is_none_literal_expr() {
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -110,7 +110,7 @@ fn check_log_record_attr_clash(checker: &mut Checker, extra: &Keyword) {
|
||||||
..
|
..
|
||||||
}) => {
|
}) => {
|
||||||
if checker.semantic().match_builtin_expr(func, "dict") {
|
if checker.semantic().match_builtin_expr(func, "dict") {
|
||||||
for keyword in &**keywords {
|
for keyword in keywords {
|
||||||
if let Some(attr) = &keyword.arg {
|
if let Some(attr) = &keyword.arg {
|
||||||
if is_reserved_attr(attr) {
|
if is_reserved_attr(attr) {
|
||||||
checker.diagnostics.push(Diagnostic::new(
|
checker.diagnostics.push(Diagnostic::new(
|
||||||
|
|
|
@ -78,7 +78,7 @@ pub(crate) fn custom_type_var_return_type(
|
||||||
let Some(self_or_cls_annotation) = args
|
let Some(self_or_cls_annotation) = args
|
||||||
.posonlyargs
|
.posonlyargs
|
||||||
.iter()
|
.iter()
|
||||||
.chain(args.args.iter())
|
.chain(&args.args)
|
||||||
.next()
|
.next()
|
||||||
.and_then(|parameter_with_default| parameter_with_default.parameter.annotation.as_ref())
|
.and_then(|parameter_with_default| parameter_with_default.parameter.annotation.as_ref())
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -140,7 +140,7 @@ pub(crate) fn bad_exit_annotation(checker: &mut Checker, function: &StmtFunction
|
||||||
let non_self_positional_args: SmallVec<[&ParameterWithDefault; 3]> = parameters
|
let non_self_positional_args: SmallVec<[&ParameterWithDefault; 3]> = parameters
|
||||||
.posonlyargs
|
.posonlyargs
|
||||||
.iter()
|
.iter()
|
||||||
.chain(parameters.args.iter())
|
.chain(¶meters.args)
|
||||||
.skip(1)
|
.skip(1)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
|
|
@ -252,7 +252,7 @@ impl UnittestAssert {
|
||||||
FxHashMap::with_capacity_and_hasher(args.len() + keywords.len(), FxBuildHasher);
|
FxHashMap::with_capacity_and_hasher(args.len() + keywords.len(), FxBuildHasher);
|
||||||
|
|
||||||
// Process positional arguments.
|
// Process positional arguments.
|
||||||
for (arg_name, value) in arg_spec.iter().zip(args.iter()) {
|
for (arg_name, value) in arg_spec.iter().zip(args) {
|
||||||
args_map.insert(arg_name, value);
|
args_map.insert(arg_name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -65,7 +65,7 @@ pub(crate) fn if_with_same_arms(checker: &mut Checker, stmt_if: &ast::StmtIf) {
|
||||||
if !current_branch
|
if !current_branch
|
||||||
.body
|
.body
|
||||||
.iter()
|
.iter()
|
||||||
.zip(following_branch.body.iter())
|
.zip(following_branch.body)
|
||||||
.all(|(stmt1, stmt2)| ComparableStmt::from(stmt1) == ComparableStmt::from(stmt2))
|
.all(|(stmt1, stmt2)| ComparableStmt::from(stmt1) == ComparableStmt::from(stmt2))
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -248,12 +248,7 @@ pub(crate) fn literal_comparisons(checker: &mut Checker, compare: &ast::ExprComp
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check each comparator in order.
|
// Check each comparator in order.
|
||||||
for (index, (op, next)) in compare
|
for (index, (op, next)) in compare.ops.iter().zip(&compare.comparators).enumerate() {
|
||||||
.ops
|
|
||||||
.iter()
|
|
||||||
.zip(compare.comparators.iter())
|
|
||||||
.enumerate()
|
|
||||||
{
|
|
||||||
if helpers::is_constant_non_singleton(comparator) {
|
if helpers::is_constant_non_singleton(comparator) {
|
||||||
comparator = next;
|
comparator = next;
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -62,10 +62,10 @@ impl Violation for TypeComparison {
|
||||||
|
|
||||||
/// E721
|
/// E721
|
||||||
pub(crate) fn type_comparison(checker: &mut Checker, compare: &ast::ExprCompare) {
|
pub(crate) fn type_comparison(checker: &mut Checker, compare: &ast::ExprCompare) {
|
||||||
for (left, right) in std::iter::once(compare.left.as_ref())
|
for (left, right) in std::iter::once(&*compare.left)
|
||||||
.chain(compare.comparators.iter())
|
.chain(&compare.comparators)
|
||||||
.tuple_windows()
|
.tuple_windows()
|
||||||
.zip(compare.ops.iter())
|
.zip(&compare.ops)
|
||||||
.filter(|(_, op)| matches!(op, CmpOp::Eq | CmpOp::NotEq))
|
.filter(|(_, op)| matches!(op, CmpOp::Eq | CmpOp::NotEq))
|
||||||
.map(|((left, right), _)| (left, right))
|
.map(|((left, right), _)| (left, right))
|
||||||
{
|
{
|
||||||
|
|
|
@ -870,7 +870,7 @@ pub(crate) fn string_dot_format_missing_argument(
|
||||||
let missing: Vec<String> = summary
|
let missing: Vec<String> = summary
|
||||||
.autos
|
.autos
|
||||||
.iter()
|
.iter()
|
||||||
.chain(summary.indices.iter())
|
.chain(&summary.indices)
|
||||||
.filter(|&&i| i >= args.len())
|
.filter(|&&i| i >= args.len())
|
||||||
.map(ToString::to_string)
|
.map(ToString::to_string)
|
||||||
.chain(
|
.chain(
|
||||||
|
|
|
@ -76,7 +76,7 @@ pub(crate) fn compare_to_empty_string(
|
||||||
|
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for ((lhs, rhs), op) in std::iter::once(left)
|
for ((lhs, rhs), op) in std::iter::once(left)
|
||||||
.chain(comparators.iter())
|
.chain(comparators)
|
||||||
.tuple_windows::<(&Expr, &Expr)>()
|
.tuple_windows::<(&Expr, &Expr)>()
|
||||||
.zip(ops)
|
.zip(ops)
|
||||||
{
|
{
|
||||||
|
|
|
@ -57,7 +57,7 @@ pub(crate) fn comparison_of_constant(
|
||||||
comparators: &[Expr],
|
comparators: &[Expr],
|
||||||
) {
|
) {
|
||||||
for ((left, right), op) in std::iter::once(left)
|
for ((left, right), op) in std::iter::once(left)
|
||||||
.chain(comparators.iter())
|
.chain(comparators)
|
||||||
.tuple_windows()
|
.tuple_windows()
|
||||||
.zip(ops)
|
.zip(ops)
|
||||||
{
|
{
|
||||||
|
|
|
@ -55,7 +55,7 @@ pub(crate) fn comparison_with_itself(
|
||||||
comparators: &[Expr],
|
comparators: &[Expr],
|
||||||
) {
|
) {
|
||||||
for ((left, right), op) in std::iter::once(left)
|
for ((left, right), op) in std::iter::once(left)
|
||||||
.chain(comparators.iter())
|
.chain(comparators)
|
||||||
.tuple_windows()
|
.tuple_windows()
|
||||||
.zip(ops)
|
.zip(ops)
|
||||||
{
|
{
|
||||||
|
|
|
@ -64,7 +64,7 @@ pub(crate) fn duplicate_bases(checker: &mut Checker, name: &str, arguments: Opti
|
||||||
let bases = &arguments.args;
|
let bases = &arguments.args;
|
||||||
|
|
||||||
let mut seen: FxHashSet<&str> = FxHashSet::with_capacity_and_hasher(bases.len(), FxBuildHasher);
|
let mut seen: FxHashSet<&str> = FxHashSet::with_capacity_and_hasher(bases.len(), FxBuildHasher);
|
||||||
for base in &**bases {
|
for base in bases {
|
||||||
if let Expr::Name(ast::ExprName { id, .. }) = base {
|
if let Expr::Name(ast::ExprName { id, .. }) = base {
|
||||||
if !seen.insert(id) {
|
if !seen.insert(id) {
|
||||||
let mut diagnostic = Diagnostic::new(
|
let mut diagnostic = Diagnostic::new(
|
||||||
|
|
|
@ -97,10 +97,7 @@ fn is_magic_value(literal_expr: LiteralExpressionRef, allowed_types: &[ConstantT
|
||||||
|
|
||||||
/// PLR2004
|
/// PLR2004
|
||||||
pub(crate) fn magic_value_comparison(checker: &mut Checker, left: &Expr, comparators: &[Expr]) {
|
pub(crate) fn magic_value_comparison(checker: &mut Checker, left: &Expr, comparators: &[Expr]) {
|
||||||
for (left, right) in std::iter::once(left)
|
for (left, right) in std::iter::once(left).chain(comparators).tuple_windows() {
|
||||||
.chain(comparators.iter())
|
|
||||||
.tuple_windows()
|
|
||||||
{
|
|
||||||
// If both of the comparators are literals, skip rule for the whole expression.
|
// If both of the comparators are literals, skip rule for the whole expression.
|
||||||
// R0133: comparison-of-constants
|
// R0133: comparison-of-constants
|
||||||
if as_literal(left).is_some() && as_literal(right).is_some() {
|
if as_literal(left).is_some() && as_literal(right).is_some() {
|
||||||
|
@ -108,7 +105,7 @@ pub(crate) fn magic_value_comparison(checker: &mut Checker, left: &Expr, compara
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for comparison_expr in std::iter::once(left).chain(comparators.iter()) {
|
for comparison_expr in std::iter::once(left).chain(comparators) {
|
||||||
if let Some(value) = as_literal(comparison_expr) {
|
if let Some(value) = as_literal(comparison_expr) {
|
||||||
if is_magic_value(value, &checker.settings.pylint.allow_magic_value_types) {
|
if is_magic_value(value, &checker.settings.pylint.allow_magic_value_types) {
|
||||||
checker.diagnostics.push(Diagnostic::new(
|
checker.diagnostics.push(Diagnostic::new(
|
||||||
|
|
|
@ -49,7 +49,7 @@ impl Violation for NanComparison {
|
||||||
|
|
||||||
/// PLW0177
|
/// PLW0177
|
||||||
pub(crate) fn nan_comparison(checker: &mut Checker, left: &Expr, comparators: &[Expr]) {
|
pub(crate) fn nan_comparison(checker: &mut Checker, left: &Expr, comparators: &[Expr]) {
|
||||||
for expr in std::iter::once(left).chain(comparators.iter()) {
|
for expr in std::iter::once(left).chain(comparators) {
|
||||||
if let Some(qualified_name) = checker.semantic().resolve_qualified_name(expr) {
|
if let Some(qualified_name) = checker.semantic().resolve_qualified_name(expr) {
|
||||||
match qualified_name.segments() {
|
match qualified_name.segments() {
|
||||||
["numpy", "nan" | "NAN" | "NaN"] => {
|
["numpy", "nan" | "NAN" | "NaN"] => {
|
||||||
|
|
|
@ -108,7 +108,7 @@ pub(crate) fn repeated_equality_comparison(checker: &mut Checker, bool_op: &ast:
|
||||||
// Break into sequences of consecutive comparisons.
|
// Break into sequences of consecutive comparisons.
|
||||||
let mut sequences: Vec<(Vec<usize>, Vec<&Expr>)> = Vec::new();
|
let mut sequences: Vec<(Vec<usize>, Vec<&Expr>)> = Vec::new();
|
||||||
let mut last = None;
|
let mut last = None;
|
||||||
for (index, comparator) in indices.iter().zip(comparators.iter()) {
|
for (index, comparator) in indices.iter().zip(comparators) {
|
||||||
if last.is_some_and(|last| last + 1 == *index) {
|
if last.is_some_and(|last| last + 1 == *index) {
|
||||||
let (indices, comparators) = sequences.last_mut().unwrap();
|
let (indices, comparators) = sequences.last_mut().unwrap();
|
||||||
indices.push(*index);
|
indices.push(*index);
|
||||||
|
|
|
@ -179,7 +179,7 @@ pub(crate) fn unnecessary_lambda(checker: &mut Checker, lambda: &ExprLambda) {
|
||||||
if call_posargs.len() != lambda_posargs.len() {
|
if call_posargs.len() != lambda_posargs.len() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (param, arg) in lambda_posargs.iter().zip(call_posargs.iter()) {
|
for (param, arg) in lambda_posargs.iter().zip(call_posargs) {
|
||||||
let Expr::Name(ast::ExprName { id, .. }) = arg else {
|
let Expr::Name(ast::ExprName { id, .. }) = arg else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
|
@ -143,10 +143,10 @@ fn concatenate_expressions(expr: &Expr) -> Option<(Expr, Type)> {
|
||||||
}
|
}
|
||||||
// If the splat element is itself a list/tuple, insert them in the other list/tuple.
|
// If the splat element is itself a list/tuple, insert them in the other list/tuple.
|
||||||
Expr::List(ast::ExprList { elts, .. }) if matches!(type_, Type::List) => {
|
Expr::List(ast::ExprList { elts, .. }) if matches!(type_, Type::List) => {
|
||||||
other_elements.iter().chain(elts.iter()).cloned().collect()
|
other_elements.iter().chain(elts).cloned().collect()
|
||||||
}
|
}
|
||||||
Expr::Tuple(ast::ExprTuple { elts, .. }) if matches!(type_, Type::Tuple) => {
|
Expr::Tuple(ast::ExprTuple { elts, .. }) if matches!(type_, Type::Tuple) => {
|
||||||
other_elements.iter().chain(elts.iter()).cloned().collect()
|
other_elements.iter().chain(elts).cloned().collect()
|
||||||
}
|
}
|
||||||
_ => return None,
|
_ => return None,
|
||||||
};
|
};
|
||||||
|
|
|
@ -189,12 +189,12 @@ fn should_be_fstring(
|
||||||
.filter_map(ast::Expr::as_call_expr)
|
.filter_map(ast::Expr::as_call_expr)
|
||||||
{
|
{
|
||||||
let ast::Arguments { keywords, args, .. } = &expr.arguments;
|
let ast::Arguments { keywords, args, .. } = &expr.arguments;
|
||||||
for keyword in &**keywords {
|
for keyword in keywords {
|
||||||
if let Some(ident) = keyword.arg.as_ref() {
|
if let Some(ident) = keyword.arg.as_ref() {
|
||||||
arg_names.insert(&ident.id);
|
arg_names.insert(&ident.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for arg in &**args {
|
for arg in args {
|
||||||
if let ast::Expr::Name(ast::ExprName { id, .. }) = arg {
|
if let ast::Expr::Name(ast::ExprName { id, .. }) = arg {
|
||||||
arg_names.insert(id);
|
arg_names.insert(id);
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,7 +100,7 @@ impl<'a> StatementVisitor<'a> for RaiseStatementVisitor<'a> {
|
||||||
Stmt::Try(ast::StmtTry {
|
Stmt::Try(ast::StmtTry {
|
||||||
body, finalbody, ..
|
body, finalbody, ..
|
||||||
}) => {
|
}) => {
|
||||||
for stmt in body.iter().chain(finalbody.iter()) {
|
for stmt in body.iter().chain(finalbody) {
|
||||||
walk_stmt(self, stmt);
|
walk_stmt(self, stmt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -135,9 +135,8 @@ impl std::fmt::Display for SourceKindDiff<'_> {
|
||||||
}
|
}
|
||||||
DiffKind::IpyNotebook(original, modified) => {
|
DiffKind::IpyNotebook(original, modified) => {
|
||||||
// Cell indices are 1-based.
|
// Cell indices are 1-based.
|
||||||
for ((idx, src_cell), dst_cell) in (1u32..)
|
for ((idx, src_cell), dst_cell) in
|
||||||
.zip(original.cells().iter())
|
(1u32..).zip(original.cells()).zip(modified.cells())
|
||||||
.zip(modified.cells().iter())
|
|
||||||
{
|
{
|
||||||
let (Cell::Code(src_cell), Cell::Code(dst_cell)) = (src_cell, dst_cell) else {
|
let (Cell::Code(src_cell), Cell::Code(dst_cell)) = (src_cell, dst_cell) else {
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -2731,7 +2731,7 @@ impl AstNode for ast::ExprCompare {
|
||||||
|
|
||||||
visitor.visit_expr(left);
|
visitor.visit_expr(left);
|
||||||
|
|
||||||
for (op, comparator) in ops.iter().zip(&**comparators) {
|
for (op, comparator) in ops.iter().zip(comparators) {
|
||||||
visitor.visit_cmp_op(op);
|
visitor.visit_cmp_op(op);
|
||||||
visitor.visit_expr(comparator);
|
visitor.visit_expr(comparator);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2048,7 +2048,7 @@ impl PartialEq for ConcatenatedStringLiteral {
|
||||||
// The `zip` here is safe because we have checked the length of both parts.
|
// The `zip` here is safe because we have checked the length of both parts.
|
||||||
self.strings
|
self.strings
|
||||||
.iter()
|
.iter()
|
||||||
.zip(other.strings.iter())
|
.zip(&other.strings)
|
||||||
.all(|(s1, s2)| s1 == s2)
|
.all(|(s1, s2)| s1 == s2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3660,6 +3660,14 @@ impl<'a> IntoIterator for &'a Parameters {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> IntoIterator for &'a Box<Parameters> {
|
||||||
|
type IntoIter = ParametersIterator<'a>;
|
||||||
|
type Item = AnyParameterRef<'a>;
|
||||||
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
|
(&**self).into_iter()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// An alternative type of AST `arg`. This is used for each function argument that might have a default value.
|
/// An alternative type of AST `arg`. This is used for each function argument that might have a default value.
|
||||||
/// Used by `Arguments` original type.
|
/// Used by `Arguments` original type.
|
||||||
///
|
///
|
||||||
|
|
|
@ -459,10 +459,10 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
|
||||||
range: _,
|
range: _,
|
||||||
}) => {
|
}) => {
|
||||||
visitor.visit_expr(left);
|
visitor.visit_expr(left);
|
||||||
for cmp_op in &**ops {
|
for cmp_op in ops {
|
||||||
visitor.visit_cmp_op(cmp_op);
|
visitor.visit_cmp_op(cmp_op);
|
||||||
}
|
}
|
||||||
for expr in &**comparators {
|
for expr in comparators {
|
||||||
visitor.visit_expr(expr);
|
visitor.visit_expr(expr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1024,7 +1024,7 @@ impl<'a> Generator<'a> {
|
||||||
group_if!(precedence::CMP, {
|
group_if!(precedence::CMP, {
|
||||||
let new_lvl = precedence::CMP + 1;
|
let new_lvl = precedence::CMP + 1;
|
||||||
self.unparse_expr(left, new_lvl);
|
self.unparse_expr(left, new_lvl);
|
||||||
for (op, cmp) in ops.iter().zip(&**comparators) {
|
for (op, cmp) in ops.iter().zip(comparators) {
|
||||||
let op = match op {
|
let op = match op {
|
||||||
CmpOp::Eq => " == ",
|
CmpOp::Eq => " == ",
|
||||||
CmpOp::NotEq => " != ",
|
CmpOp::NotEq => " != ",
|
||||||
|
|
|
@ -48,7 +48,7 @@ impl FormatNodeRule<PatternArguments> for FormatPatternArguments {
|
||||||
pattern.format().with_options(Parentheses::Preserve),
|
pattern.format().with_options(Parentheses::Preserve),
|
||||||
)
|
)
|
||||||
}))
|
}))
|
||||||
.nodes(item.keywords.iter());
|
.nodes(&item.keywords);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -487,7 +487,7 @@ fn check_type<T: TypeChecker>(binding: &Binding, semantic: &SemanticModel) -> bo
|
||||||
// The type checker might know how to infer the type based on `init_expr`.
|
// The type checker might know how to infer the type based on `init_expr`.
|
||||||
Some(Stmt::Assign(ast::StmtAssign { targets, value, .. })) => targets
|
Some(Stmt::Assign(ast::StmtAssign { targets, value, .. })) => targets
|
||||||
.iter()
|
.iter()
|
||||||
.find_map(|target| match_value(binding, target, value.as_ref()))
|
.find_map(|target| match_value(binding, target, value))
|
||||||
.is_some_and(|value| T::match_initializer(value, semantic)),
|
.is_some_and(|value| T::match_initializer(value, semantic)),
|
||||||
|
|
||||||
// ```python
|
// ```python
|
||||||
|
@ -496,7 +496,7 @@ fn check_type<T: TypeChecker>(binding: &Binding, semantic: &SemanticModel) -> bo
|
||||||
//
|
//
|
||||||
// In this situation, we check only the annotation.
|
// In this situation, we check only the annotation.
|
||||||
Some(Stmt::AnnAssign(ast::StmtAnnAssign { annotation, .. })) => {
|
Some(Stmt::AnnAssign(ast::StmtAnnAssign { annotation, .. })) => {
|
||||||
T::match_annotation(annotation.as_ref(), semantic)
|
T::match_annotation(annotation, semantic)
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => false,
|
_ => false,
|
||||||
|
@ -512,7 +512,7 @@ fn check_type<T: TypeChecker>(binding: &Binding, semantic: &SemanticModel) -> bo
|
||||||
.expressions(source)
|
.expressions(source)
|
||||||
.find_map(|expr| expr.as_named_expr())
|
.find_map(|expr| expr.as_named_expr())
|
||||||
.and_then(|ast::ExprNamed { target, value, .. }| {
|
.and_then(|ast::ExprNamed { target, value, .. }| {
|
||||||
match_value(binding, target.as_ref(), value.as_ref())
|
match_value(binding, target, value)
|
||||||
})
|
})
|
||||||
.is_some_and(|value| T::match_initializer(value, semantic))
|
.is_some_and(|value| T::match_initializer(value, semantic))
|
||||||
})
|
})
|
||||||
|
@ -543,13 +543,13 @@ fn check_type<T: TypeChecker>(binding: &Binding, semantic: &SemanticModel) -> bo
|
||||||
//
|
//
|
||||||
// We trust the annotation and see if the type checker matches the annotation.
|
// We trust the annotation and see if the type checker matches the annotation.
|
||||||
Some(Stmt::FunctionDef(ast::StmtFunctionDef { parameters, .. })) => {
|
Some(Stmt::FunctionDef(ast::StmtFunctionDef { parameters, .. })) => {
|
||||||
let Some(parameter) = find_parameter(parameters.as_ref(), binding) else {
|
let Some(parameter) = find_parameter(parameters, binding) else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
let Some(ref annotation) = parameter.parameter.annotation else {
|
let Some(ref annotation) = parameter.parameter.annotation else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
T::match_annotation(annotation.as_ref(), semantic)
|
T::match_annotation(annotation, semantic)
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => false,
|
_ => false,
|
||||||
|
@ -562,7 +562,7 @@ fn check_type<T: TypeChecker>(binding: &Binding, semantic: &SemanticModel) -> bo
|
||||||
//
|
//
|
||||||
// It's a typed declaration, type annotation is the only source of information.
|
// It's a typed declaration, type annotation is the only source of information.
|
||||||
Some(Stmt::AnnAssign(ast::StmtAnnAssign { annotation, .. })) => {
|
Some(Stmt::AnnAssign(ast::StmtAnnAssign { annotation, .. })) => {
|
||||||
T::match_annotation(annotation.as_ref(), semantic)
|
T::match_annotation(annotation, semantic)
|
||||||
}
|
}
|
||||||
_ => false,
|
_ => false,
|
||||||
},
|
},
|
||||||
|
@ -726,7 +726,7 @@ impl TypeChecker for IoBaseChecker {
|
||||||
|
|
||||||
// Ex) `open("file.txt")`
|
// Ex) `open("file.txt")`
|
||||||
semantic
|
semantic
|
||||||
.resolve_qualified_name(func.as_ref())
|
.resolve_qualified_name(func)
|
||||||
.is_some_and(|qualified_name| {
|
.is_some_and(|qualified_name| {
|
||||||
matches!(
|
matches!(
|
||||||
qualified_name.segments(),
|
qualified_name.segments(),
|
||||||
|
@ -899,7 +899,7 @@ pub fn find_binding_value<'a>(binding: &Binding, semantic: &'a SemanticModel) ->
|
||||||
.expressions(parent_id)
|
.expressions(parent_id)
|
||||||
.find_map(|expr| expr.as_named_expr());
|
.find_map(|expr| expr.as_named_expr());
|
||||||
if let Some(ast::ExprNamed { target, value, .. }) = parent {
|
if let Some(ast::ExprNamed { target, value, .. }) = parent {
|
||||||
return match_value(binding, target.as_ref(), value.as_ref());
|
return match_value(binding, target, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Ex) `x = 1`
|
// Ex) `x = 1`
|
||||||
|
@ -907,14 +907,14 @@ pub fn find_binding_value<'a>(binding: &Binding, semantic: &'a SemanticModel) ->
|
||||||
Some(Stmt::Assign(ast::StmtAssign { value, targets, .. })) => {
|
Some(Stmt::Assign(ast::StmtAssign { value, targets, .. })) => {
|
||||||
return targets
|
return targets
|
||||||
.iter()
|
.iter()
|
||||||
.find_map(|target| match_value(binding, target, value.as_ref()))
|
.find_map(|target| match_value(binding, target, value))
|
||||||
}
|
}
|
||||||
Some(Stmt::AnnAssign(ast::StmtAnnAssign {
|
Some(Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||||
value: Some(value),
|
value: Some(value),
|
||||||
target,
|
target,
|
||||||
..
|
..
|
||||||
})) => {
|
})) => {
|
||||||
return match_value(binding, target, value.as_ref());
|
return match_value(binding, target, value);
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
|
@ -958,7 +958,7 @@ fn match_value<'a>(binding: &Binding, target: &Expr, value: &'a Expr) -> Option<
|
||||||
|
|
||||||
/// Given a target and value, find the value that's assigned to the given symbol.
|
/// Given a target and value, find the value that's assigned to the given symbol.
|
||||||
fn match_target<'a>(binding: &Binding, targets: &[Expr], values: &'a [Expr]) -> Option<&'a Expr> {
|
fn match_target<'a>(binding: &Binding, targets: &[Expr], values: &'a [Expr]) -> Option<&'a Expr> {
|
||||||
for (target, value) in targets.iter().zip(values.iter()) {
|
for (target, value) in targets.iter().zip(values) {
|
||||||
match target {
|
match target {
|
||||||
Expr::Tuple(ast::ExprTuple {
|
Expr::Tuple(ast::ExprTuple {
|
||||||
elts: target_elts, ..
|
elts: target_elts, ..
|
||||||
|
|
|
@ -795,7 +795,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
let resolved: QualifiedName = qualified_name
|
let resolved: QualifiedName = qualified_name
|
||||||
.segments()
|
.segments()
|
||||||
.iter()
|
.iter()
|
||||||
.chain(tail.iter())
|
.chain(tail)
|
||||||
.copied()
|
.copied()
|
||||||
.collect();
|
.collect();
|
||||||
Some(resolved)
|
Some(resolved)
|
||||||
|
@ -809,7 +809,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
.segments()
|
.segments()
|
||||||
.iter()
|
.iter()
|
||||||
.take(1)
|
.take(1)
|
||||||
.chain(tail.iter())
|
.chain(tail)
|
||||||
.copied()
|
.copied()
|
||||||
.collect(),
|
.collect(),
|
||||||
)
|
)
|
||||||
|
@ -832,7 +832,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
qualified_name
|
qualified_name
|
||||||
.segments()
|
.segments()
|
||||||
.iter()
|
.iter()
|
||||||
.chain(tail.iter())
|
.chain(tail)
|
||||||
.copied()
|
.copied()
|
||||||
.collect()
|
.collect()
|
||||||
};
|
};
|
||||||
|
|
|
@ -803,7 +803,7 @@ impl LintConfiguration {
|
||||||
.select
|
.select
|
||||||
.iter()
|
.iter()
|
||||||
.flatten()
|
.flatten()
|
||||||
.chain(selection.extend_select.iter())
|
.chain(&selection.extend_select)
|
||||||
.filter(|s| s.specificity() == spec)
|
.filter(|s| s.specificity() == spec)
|
||||||
{
|
{
|
||||||
for rule in selector.rules(&preview) {
|
for rule in selector.rules(&preview) {
|
||||||
|
@ -830,7 +830,7 @@ impl LintConfiguration {
|
||||||
.fixable
|
.fixable
|
||||||
.iter()
|
.iter()
|
||||||
.flatten()
|
.flatten()
|
||||||
.chain(selection.extend_fixable.iter())
|
.chain(&selection.extend_fixable)
|
||||||
.filter(|s| s.specificity() == spec)
|
.filter(|s| s.specificity() == spec)
|
||||||
{
|
{
|
||||||
for rule in selector.all_rules() {
|
for rule in selector.all_rules() {
|
||||||
|
|
|
@ -219,7 +219,7 @@ impl<'a> Resolver<'a> {
|
||||||
|
|
||||||
/// Return an iterator over the resolved [`Settings`] in this [`Resolver`].
|
/// Return an iterator over the resolved [`Settings`] in this [`Resolver`].
|
||||||
pub fn settings(&self) -> impl Iterator<Item = &Settings> {
|
pub fn settings(&self) -> impl Iterator<Item = &Settings> {
|
||||||
std::iter::once(&self.pyproject_config.settings).chain(self.settings.iter())
|
std::iter::once(&self.pyproject_config.settings).chain(&self.settings)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue