docs: all `flake8-comprehension` rules (#3631)

This commit is contained in:
Dhruv Manilawala 2023-03-21 19:58:19 +05:30 committed by GitHub
parent 7b9bdc494a
commit 33394e4a69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 181 additions and 0 deletions

View File

@ -12,6 +12,30 @@ use crate::rules::flake8_comprehensions::settings::Settings;
use super::helpers; use super::helpers;
/// ## What it does
/// Checks for unnecessary `dict`, `list` or `tuple` calls that can be
/// rewritten as empty literals.
///
/// ## Why is this bad?
/// It's unnecessary to call e.g., `dict()` as opposed to using an empty
/// literal (`{}`). The former is slower because the name `dict` must be
/// looked up in the global scope in case it has been rebound.
///
/// ## Examples
/// ```python
/// dict()
/// dict(a=1, b=2)
/// list()
/// tuple()
/// ```
///
/// Use instead:
/// ```python
/// {}
/// {"a": 1, "b": 2}
/// []
/// ()
/// ```
#[violation] #[violation]
pub struct UnnecessaryCollectionCall { pub struct UnnecessaryCollectionCall {
pub obj_type: String, pub obj_type: String,

View File

@ -31,6 +31,7 @@ use super::helpers;
/// ///
/// This rule applies to a variety of functions, including `list`, `reversed`, /// This rule applies to a variety of functions, including `list`, `reversed`,
/// `set`, `sorted`, and `tuple`. For example: /// `set`, `sorted`, and `tuple`. For example:
///
/// - Instead of `list(list(iterable))`, use `list(iterable)`. /// - Instead of `list(list(iterable))`, use `list(iterable)`.
/// - Instead of `list(tuple(iterable))`, use `list(iterable)`. /// - Instead of `list(tuple(iterable))`, use `list(iterable)`.
/// - Instead of `tuple(list(iterable))`, use `tuple(iterable)`. /// - Instead of `tuple(list(iterable))`, use `tuple(iterable)`.

View File

@ -11,6 +11,21 @@ use crate::rules::flake8_comprehensions::fixes;
use super::helpers; use super::helpers;
/// ## What it does
/// Checks for unnecessary `list` calls around list comprehensions.
///
/// ## Why is it bad?
/// It is redundant to use a `list` call around a list comprehension.
///
/// ## Examples
/// ```python
/// list([f(x) for x in foo])
/// ```
///
/// Use instead
/// ```python
/// [f(x) for x in foo]
/// ```
#[violation] #[violation]
pub struct UnnecessaryListCall; pub struct UnnecessaryListCall;

View File

@ -11,6 +11,22 @@ use crate::rules::flake8_comprehensions::fixes;
use super::helpers; use super::helpers;
/// ## What it does
/// Checks for unnecessary list comprehensions.
///
/// ## Why is it bad?
/// It's unnecessary to use a list comprehension inside a call to `dict`,
/// since there is an equivalent comprehension for this type.
///
/// ## Examples
/// ```python
/// dict([(x, f(x)) for x in foo])
/// ```
///
/// Use instead:
/// ```python
/// {x: f(x) for x in foo}
/// ```
#[violation] #[violation]
pub struct UnnecessaryListComprehensionDict; pub struct UnnecessaryListComprehensionDict;

View File

@ -11,6 +11,22 @@ use crate::rules::flake8_comprehensions::fixes;
use super::helpers; use super::helpers;
/// ## What it does
/// Checks for unnecessary list comprehensions.
///
/// ## Why is it bad?
/// It's unnecessary to use a list comprehension inside a call to `set`,
/// since there is an equivalent comprehension for this type.
///
/// ## Examples
/// ```python
/// set([f(x) for x in foo])
/// ```
///
/// Use instead:
/// ```python
/// {f(x) for x in foo}
/// ```
#[violation] #[violation]
pub struct UnnecessaryListComprehensionSet; pub struct UnnecessaryListComprehensionSet;

View File

@ -11,6 +11,26 @@ use crate::rules::flake8_comprehensions::fixes;
use super::helpers; use super::helpers;
/// ## What it does
/// Checks for unnecessary `list` or `tuple` literals.
///
/// ## Why is it bad?
/// It's unnecessary to use a list or tuple literal within a call to `dict`.
/// It can be rewritten as a dict literal (`{}`).
///
/// ## Examples
/// ```python
/// dict([(1, 2), (3, 4)])
/// dict(((1, 2), (3, 4)))
/// dict([])
/// ```
///
/// Use instead:
/// ```python
/// {1: 2, 3: 4}
/// {1: 2, 3: 4}
/// {}
/// ```
#[violation] #[violation]
pub struct UnnecessaryLiteralDict { pub struct UnnecessaryLiteralDict {
pub obj_type: String, pub obj_type: String,

View File

@ -11,6 +11,27 @@ use crate::rules::flake8_comprehensions::fixes;
use super::helpers; use super::helpers;
/// ## What it does
/// Checks for `set` calls that take unnecessary `list` or `tuple` literals
/// as arguments.
///
/// ## Why is it bad?
/// It's unnecessary to use a list or tuple literal within a call to `set`.
/// Instead, the expression can be rewritten as a set literal.
///
/// ## Examples
/// ```python
/// set([1, 2])
/// set((1, 2))
/// set([])
/// ```
///
/// Use instead:
/// ```python
/// {1, 2}
/// {1, 2}
/// set()
/// ```
#[violation] #[violation]
pub struct UnnecessaryLiteralSet { pub struct UnnecessaryLiteralSet {
pub obj_type: String, pub obj_type: String,

View File

@ -11,6 +11,29 @@ use crate::rules::flake8_comprehensions::fixes;
use super::helpers; use super::helpers;
/// ## What it does
/// Checks for `list` calls that take unnecessary list or tuple literals as
/// arguments.
///
/// ## Why is it bad?
/// It's unnecessary to use a list or tuple literal within a `list()` call,
/// since there is a literal syntax for these types.
///
/// If a list literal is passed in, then the outer call to `list()` should be
/// removed. Otherwise, if a tuple literal is passed in, then it should be
/// rewritten as a `list` literal.
///
/// ## Examples
/// ```python
/// list([1, 2])
/// list((1, 2))
/// ```
///
/// Use instead:
/// ```python
/// [1, 2]
/// [1, 2]
/// ```
#[violation] #[violation]
pub struct UnnecessaryLiteralWithinListCall { pub struct UnnecessaryLiteralWithinListCall {
pub literal: String, pub literal: String,

View File

@ -11,6 +11,29 @@ use crate::rules::flake8_comprehensions::fixes;
use super::helpers; use super::helpers;
/// ## What it does
/// Checks for `tuple` calls that take unnecessary list or tuple literals as
/// arguments.
///
/// ## Why is it bad?
/// It's unnecessary to use a list or tuple literal within a `tuple()` call,
/// since there is a literal syntax for these types.
///
/// If a list literal was passed, then it should be rewritten as a `tuple`
/// literal. Otherwise, if a tuple literal was passed, then the outer call
/// to `list()` should be removed.
///
/// ## Examples
/// ```python
/// tuple([1, 2])
/// tuple((1, 2))
/// ```
///
/// Use instead:
/// ```python
/// (1, 2)
/// (1, 2)
/// ```
#[violation] #[violation]
pub struct UnnecessaryLiteralWithinTupleCall { pub struct UnnecessaryLiteralWithinTupleCall {
pub literal: String, pub literal: String,

View File

@ -32,6 +32,7 @@ use super::helpers;
/// ///
/// This rule also applies to `map` calls within `list`, `set`, and `dict` /// This rule also applies to `map` calls within `list`, `set`, and `dict`
/// calls. For example: /// calls. For example:
///
/// - Instead of `list(map(lambda num: num * 2, nums))`, use /// - Instead of `list(map(lambda num: num * 2, nums))`, use
/// `[num * 2 for num in nums]`. /// `[num * 2 for num in nums]`.
/// - Instead of `set(map(lambda num: num % 2 == 0, nums))`, use /// - Instead of `set(map(lambda num: num % 2 == 0, nums))`, use

View File

@ -9,6 +9,27 @@ use crate::checkers::ast::Checker;
use super::helpers; use super::helpers;
/// ## What it does
/// Checks for unnecessary subscript reversal of iterable.
///
/// ## Why is it bad?
/// It's unnecessary to reverse the order of an iterable when passing it
/// into `reversed()`, `set()` or `sorted()` functions as they will change
/// the order of the elements again.
///
/// ## Examples
/// ```python
/// reversed(iterable[::-1])
/// set(iterable[::-1])
/// sorted(iterable)[::-1]
/// ```
///
/// Use instead:
/// ```python
/// reversed(iterable)
/// set(iterable)
/// sorted(iterable, reverse=True)
/// ```
#[violation] #[violation]
pub struct UnnecessarySubscriptReversal { pub struct UnnecessarySubscriptReversal {
pub func: String, pub func: String,