mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 06:11:21 +00:00
Fix documented examples for unnecessary-subscript-reversal
(#7774)
## Summary Two of the three listed examples were wrong: one was semantically incorrect, another was _correct_ but not actually within the scope of the rule. Good motivation for us to start linting documentation examples :) Closes https://github.com/astral-sh/ruff/issues/7773.
This commit is contained in:
parent
e129f77bcf
commit
a6ebbf21c3
2 changed files with 9 additions and 16 deletions
|
@ -680,9 +680,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if checker.enabled(Rule::UnnecessarySubscriptReversal) {
|
if checker.enabled(Rule::UnnecessarySubscriptReversal) {
|
||||||
flake8_comprehensions::rules::unnecessary_subscript_reversal(
|
flake8_comprehensions::rules::unnecessary_subscript_reversal(checker, call);
|
||||||
checker, expr, func, args,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
if checker.enabled(Rule::UnnecessaryMap) {
|
if checker.enabled(Rule::UnnecessaryMap) {
|
||||||
flake8_comprehensions::rules::unnecessary_map(
|
flake8_comprehensions::rules::unnecessary_map(
|
||||||
|
|
|
@ -15,16 +15,16 @@ use crate::checkers::ast::Checker;
|
||||||
///
|
///
|
||||||
/// ## Examples
|
/// ## Examples
|
||||||
/// ```python
|
/// ```python
|
||||||
/// reversed(iterable[::-1])
|
/// sorted(iterable[::-1])
|
||||||
/// set(iterable[::-1])
|
/// set(iterable[::-1])
|
||||||
/// sorted(iterable)[::-1]
|
/// reversed(iterable[::-1])
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// Use instead:
|
/// Use instead:
|
||||||
/// ```python
|
/// ```python
|
||||||
/// reversed(iterable)
|
/// sorted(iterable)
|
||||||
/// set(iterable)
|
/// set(iterable)
|
||||||
/// sorted(iterable, reverse=True)
|
/// iterable
|
||||||
/// ```
|
/// ```
|
||||||
#[violation]
|
#[violation]
|
||||||
pub struct UnnecessarySubscriptReversal {
|
pub struct UnnecessarySubscriptReversal {
|
||||||
|
@ -40,16 +40,11 @@ impl Violation for UnnecessarySubscriptReversal {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// C415
|
/// C415
|
||||||
pub(crate) fn unnecessary_subscript_reversal(
|
pub(crate) fn unnecessary_subscript_reversal(checker: &mut Checker, call: &ast::ExprCall) {
|
||||||
checker: &mut Checker,
|
let Some(first_arg) = call.arguments.args.first() else {
|
||||||
expr: &Expr,
|
|
||||||
func: &Expr,
|
|
||||||
args: &[Expr],
|
|
||||||
) {
|
|
||||||
let Some(first_arg) = args.first() else {
|
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
let Some(func) = func.as_name_expr() else {
|
let Some(func) = call.func.as_name_expr() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
if !matches!(func.id.as_str(), "reversed" | "set" | "sorted") {
|
if !matches!(func.id.as_str(), "reversed" | "set" | "sorted") {
|
||||||
|
@ -98,6 +93,6 @@ pub(crate) fn unnecessary_subscript_reversal(
|
||||||
UnnecessarySubscriptReversal {
|
UnnecessarySubscriptReversal {
|
||||||
func: func.id.to_string(),
|
func: func.id.to_string(),
|
||||||
},
|
},
|
||||||
expr.range(),
|
call.range(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue