1
- use ruff_diagnostics:: { Diagnostic , Edit , Fix , FixAvailability , Violation } ;
2
- use ruff_macros:: { derive_message_formats, violation} ;
3
- use ruff_python_ast:: {
4
- BoolOp , Expr , ExprBoolOp , ExprDictComp , ExprIfExp , ExprListComp , ExprSetComp ,
5
- } ;
6
- use ruff_text_size:: { Ranged , TextRange } ;
7
-
8
- use crate :: checkers:: ast:: Checker ;
9
- use crate :: fix:: snippet:: SourceCodeSnippet ;
1
+ use ruff_diagnostics:: Violation ;
2
+ use ruff_macros:: violation;
10
3
4
+ /// ## Removal
5
+ /// This rule was removed from Ruff because it was common for it to introduce behavioral changes.
6
+ /// See [#9007](https://github.com/astral-sh/ruff/issues/9007) for more information.
7
+ ///
11
8
/// ## What it does
12
9
/// Checks for uses of the known pre-Python 2.5 ternary syntax.
13
10
///
@@ -31,100 +28,15 @@ use crate::fix::snippet::SourceCodeSnippet;
31
28
/// maximum = x if x >= y else y
32
29
/// ```
33
30
#[ violation]
34
- pub struct AndOrTernary {
35
- ternary : SourceCodeSnippet ,
36
- }
31
+ pub struct AndOrTernary ;
37
32
33
+ /// PLR1706
38
34
impl Violation for AndOrTernary {
39
- const FIX_AVAILABILITY : FixAvailability = FixAvailability :: Sometimes ;
40
-
41
- #[ derive_message_formats]
42
35
fn message ( & self ) -> String {
43
- if let Some ( ternary) = self . ternary . full_display ( ) {
44
- format ! ( "Consider using if-else expression (`{ternary}`)" )
45
- } else {
46
- format ! ( "Consider using if-else expression" )
47
- }
48
- }
49
-
50
- fn fix_title ( & self ) -> Option < String > {
51
- Some ( format ! ( "Convert to if-else expression" ) )
52
- }
53
- }
54
-
55
- /// Returns `Some((condition, true_value, false_value))`, if `bool_op` is of the form `condition and true_value or false_value`.
56
- fn parse_and_or_ternary ( bool_op : & ExprBoolOp ) -> Option < ( & Expr , & Expr , & Expr ) > {
57
- if bool_op. op != BoolOp :: Or {
58
- return None ;
36
+ unreachable ! ( "PLR1706 has been removed" ) ;
59
37
}
60
- let [ expr, false_value] = bool_op. values . as_slice ( ) else {
61
- return None ;
62
- } ;
63
- let Some ( and_op) = expr. as_bool_op_expr ( ) else {
64
- return None ;
65
- } ;
66
- if and_op. op != BoolOp :: And {
67
- return None ;
68
- }
69
- let [ condition, true_value] = and_op. values . as_slice ( ) else {
70
- return None ;
71
- } ;
72
- if false_value. is_bool_op_expr ( ) || true_value. is_bool_op_expr ( ) {
73
- return None ;
74
- }
75
- Some ( ( condition, true_value, false_value) )
76
- }
77
-
78
- /// Returns `true` if the expression is used within a comprehension.
79
- fn is_comprehension_if ( parent : Option < & Expr > , expr : & ExprBoolOp ) -> bool {
80
- let comprehensions = match parent {
81
- Some ( Expr :: ListComp ( ExprListComp { generators, .. } ) ) => generators,
82
- Some ( Expr :: SetComp ( ExprSetComp { generators, .. } ) ) => generators,
83
- Some ( Expr :: DictComp ( ExprDictComp { generators, .. } ) ) => generators,
84
- _ => {
85
- return false ;
86
- }
87
- } ;
88
- comprehensions
89
- . iter ( )
90
- . any ( |comp| comp. ifs . iter ( ) . any ( |ifs| ifs. range ( ) == expr. range ( ) ) )
91
- }
92
38
93
- /// PLR1706
94
- pub ( crate ) fn and_or_ternary ( checker : & mut Checker , bool_op : & ExprBoolOp ) {
95
- if checker. semantic ( ) . current_statement ( ) . is_if_stmt ( ) {
96
- return ;
97
- }
98
- let parent_expr = checker. semantic ( ) . current_expression_parent ( ) ;
99
- if parent_expr. is_some_and ( Expr :: is_bool_op_expr) {
100
- return ;
39
+ fn message_formats ( ) -> & ' static [ & ' static str ] {
40
+ & [ "Consider using if-else expression" ]
101
41
}
102
- let Some ( ( condition, true_value, false_value) ) = parse_and_or_ternary ( bool_op) else {
103
- return ;
104
- } ;
105
-
106
- let if_expr = Expr :: IfExp ( ExprIfExp {
107
- test : Box :: new ( condition. clone ( ) ) ,
108
- body : Box :: new ( true_value. clone ( ) ) ,
109
- orelse : Box :: new ( false_value. clone ( ) ) ,
110
- range : TextRange :: default ( ) ,
111
- } ) ;
112
-
113
- let ternary = if is_comprehension_if ( parent_expr, bool_op) {
114
- format ! ( "({})" , checker. generator( ) . expr( & if_expr) )
115
- } else {
116
- checker. generator ( ) . expr ( & if_expr)
117
- } ;
118
-
119
- let mut diagnostic = Diagnostic :: new (
120
- AndOrTernary {
121
- ternary : SourceCodeSnippet :: new ( ternary. clone ( ) ) ,
122
- } ,
123
- bool_op. range ,
124
- ) ;
125
- diagnostic. set_fix ( Fix :: unsafe_edit ( Edit :: range_replacement (
126
- ternary,
127
- bool_op. range ,
128
- ) ) ) ;
129
- checker. diagnostics . push ( diagnostic) ;
130
42
}
0 commit comments