Repository: astral-sh/ruff
File: crates/ty_python_semantic/src/types/infer/builder/type_expression.rs (Rust)

Code before:
        let arguments = &*subscript_node.slice;
        let args = if let ast::Expr::Tuple(t) = arguments {
            &*t.elts
        } else {
            std::slice::from_ref(arguments)
        };
        if args.len() != expected_arg_count {
            if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript_node) {
                let noun = if expected_arg_count == 1 {
                    "argument"
                } else {
                    "arguments"
                };
                builder.into_diagnostic(format_args!(
                    "Legacy alias `{alias}` expected exactly {expected_arg_count} {noun}, \
                    got {}",
                    args.len()
                ));
            }
        }
        let ty = class.to_specialized_instance(
            self.db(),
            &args
                .iter()
                .map(|node| self.infer_type_expression(node))
                .collect::<Box<[Type]>>(),
        );
        if arguments.is_tuple_expr() {
            self.store_expression_type(arguments, ty);
        }
        ty
    }

    /// Infer the type of a `Callable[...]` type expression.
    pub(crate) fn infer_callable_type(&mut self, subscript: &ast::ExprSubscript) -> Type<'db> {
        let db = self.db();

        let arguments_slice = &*subscript.slice;

        let mut arguments = match arguments_slice {
            ast::Expr::Tuple(tuple) => Either::Left(tuple.iter()),
            _ => {
                self.infer_callable_parameter_types(arguments_slice);
                Either::Right(std::iter::empty::<&ast::Expr>())
            }
        };

        let first_argument = arguments.next();

        let parameters = first_argument.and_then(|arg| self.infer_callable_parameter_types(arg));


Review comment (suggestion):
This is one of the few instances where we now collect into a `Box<[Type]>` only to throw the allocation away. 

I think an alternative is to change `to_specialized_instance` to either take a `SmallVec` or have our own struct that implements `Lookup` but can be a collected value or a slice (or implement `Lookup` for `Cow`?))

Code after:
        let arguments = &*subscript_node.slice;
        let args = if let ast::Expr::Tuple(t) = arguments {
            &*t.elts
        } else {
            std::slice::from_ref(arguments)
        };
        if args.len() != expected_arg_count {
            if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript_node) {
                let noun = if expected_arg_count == 1 {
                    "argument"
                } else {
                    "arguments"
                };
                builder.into_diagnostic(format_args!(
                    "Legacy alias `{alias}` expected exactly {expected_arg_count} {noun}, \
                    got {}",
                    args.len()
                ));
            }
        }
        let ty = class.to_specialized_instance(
            self.db(),
            args.iter()
                .map(|node| self.infer_type_expression(node))
                .collect::<Vec<_>>(),
        );
        if arguments.is_tuple_expr() {
            self.store_expression_type(arguments, ty);
        }
        ty
    }

    /// Infer the type of a `Callable[...]` type expression.
    pub(crate) fn infer_callable_type(&mut self, subscript: &ast::ExprSubscript) -> Type<'db> {
        let db = self.db();

        let arguments_slice = &*subscript.slice;

        let mut arguments = match arguments_slice {
            ast::Expr::Tuple(tuple) => Either::Left(tuple.iter()),
            _ => {
                self.infer_callable_parameter_types(arguments_slice);
                Either::Right(std::iter::empty::<&ast::Expr>())
            }
        };

        let first_argument = arguments.next();

        let parameters = first_argument.and_then(|arg| self.infer_callable_parameter_types(arg));

        let return_type = arguments.next().map(|arg| self.infer_type_expression(arg));