From 7c2e9f71ea6547e826c64bc58c4aa71a1458b825 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 29 Mar 2024 20:32:34 -0400 Subject: [PATCH] Change `quadratic-list-summation` docs to use `iadd` consistently (#10666) Closes https://github.com/astral-sh/ruff/issues/10088. --- .../src/rules/ruff/rules/quadratic_list_summation.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/src/rules/ruff/rules/quadratic_list_summation.rs b/crates/ruff_linter/src/rules/ruff/rules/quadratic_list_summation.rs index e1130d04ce842..69c87d192590f 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/quadratic_list_summation.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/quadratic_list_summation.rs @@ -24,10 +24,13 @@ use crate::importer::ImportRequest; /// quadratic complexity. The following methods are all linear in the number of /// lists: /// -/// - `functools.reduce(operator.iconcat, lists, [])` +/// - `functools.reduce(operator.iadd, lists, [])` /// - `list(itertools.chain.from_iterable(lists))` /// - `[item for sublist in lists for item in sublist]` /// +/// When fixing relevant violations, Ruff defaults to the `functools.reduce` +/// form, which outperforms the other methods in [microbenchmarks]. +/// /// ## Example /// ```python /// lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] @@ -41,12 +44,14 @@ use crate::importer::ImportRequest; /// /// /// lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] -/// functools.reduce(operator.iconcat, lists, []) +/// functools.reduce(operator.iadd, lists, []) /// ``` /// /// ## References /// - [_How Not to Flatten a List of Lists in Python_](https://mathieularose.com/how-not-to-flatten-a-list-of-lists-in-python) /// - [_How do I make a flat list out of a list of lists?_](https://stackoverflow.com/questions/952914/how-do-i-make-a-flat-list-out-of-a-list-of-lists/953097#953097) +/// +/// [microbenchmarks]: https://github.com/astral-sh/ruff/issues/5073#issuecomment-1591836349 #[violation] pub struct QuadraticListSummation;