Document use of anonymous assignment in useless-expression (#10551)

Closes https://github.com/astral-sh/ruff/issues/10536.
This commit is contained in:
Charlie Marsh 2024-03-24 22:46:33 -04:00 committed by GitHub
parent 39fb6d9bfc
commit 9856c1446b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -25,6 +25,26 @@ use super::super::helpers::at_last_top_level_expression_in_cell;
/// ```python /// ```python
/// foo = 1 + 1 /// foo = 1 + 1
/// ``` /// ```
///
/// ## Known problems
/// This rule ignores expression types that are commonly used for their side
/// effects, such as function calls.
///
/// However, if a seemingly useless expression (like an attribute access) is
/// needed to trigger a side effect, consider assigning it to an anonymous
/// variable, to indicate that the return value is intentionally ignored.
///
/// For example, given:
/// ```python
/// with errors.ExceptionRaisedContext():
/// obj.attribute
/// ```
///
/// Use instead:
/// ```python
/// with errors.ExceptionRaisedContext():
/// _ = obj.attribute
/// ```
#[violation] #[violation]
pub struct UselessExpression { pub struct UselessExpression {
kind: Kind, kind: Kind,