Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/refurb/FURB136.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@
x
> y
) else y # FURB136


highest_score = (
x
# text
if x > y
else y
)
22 changes: 18 additions & 4 deletions crates/ruff_linter/src/rules/refurb/rules/if_expr_min_max.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use ruff_diagnostics::Applicability;
use ruff_macros::{ViolationMetadata, derive_message_formats};
use ruff_python_ast::comparable::ComparableExpr;
use ruff_python_ast::{self as ast, CmpOp, Expr};
Expand All @@ -19,14 +20,21 @@ use crate::{Edit, Fix, FixAvailability, Violation};
///
/// ## Example
/// ```python
/// score1, score2 = 4, 5
///
/// highest_score = score1 if score1 > score2 else score2
/// ```
///
/// Use instead:
/// ```python
/// score1, score2 = 4, 5
///
/// highest_score = max(score2, score1)
/// ```
///
/// ## Fix safety
/// This rule's fix is marked as safe, unless the expression contains comments.
///
/// ## References
/// - [Python documentation: `min`](https://docs.python.org/3.11/library/functions.html#min)
/// - [Python documentation: `max`](https://docs.python.org/3.11/library/functions.html#max)
Expand Down Expand Up @@ -141,10 +149,16 @@ pub(crate) fn if_expr_min_max(checker: &Checker, if_exp: &ast::ExprIf) {
);

if checker.semantic().has_builtin_binding(min_max.as_str()) {
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
replacement,
if_exp.range(),
)));
let applicability = if checker.comment_ranges().intersects(if_exp.range()) {
Applicability::Unsafe
} else {
Applicability::Safe
};

diagnostic.set_fix(Fix::applicable_edit(
Edit::range_replacement(replacement, if_exp.range()),
applicability,
));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,29 @@ help: Replace with `max(y, x)`
- > y
- ) else y # FURB136
22 + max(y, x) # FURB136
23 |
24 |
25 | highest_score = (

FURB136 [*] Replace `if` expression with `max(y, x)`
--> FURB136.py:29:5
|
28 | highest_score = (
29 | / x
30 | | # text
31 | | if x > y
32 | | else y
| |__________^
33 | )
|
help: Replace with `max(y, x)`
26 |
27 |
28 | highest_score = (
- x
- # text
- if x > y
- else y
29 + max(y, x)
30 | )
note: This is an unsafe fix and may change runtime behavior