@@ -16,7 +16,6 @@ use crate::cst::helpers::or_space;
16
16
use crate :: cst:: matchers:: { match_comparison, transform_expression} ;
17
17
use crate :: fix:: edits:: pad;
18
18
use crate :: fix:: snippet:: SourceCodeSnippet ;
19
- use crate :: settings:: types:: PreviewMode ;
20
19
21
20
/// ## What it does
22
21
/// Checks for conditions that position a constant on the left-hand side of the
@@ -58,26 +57,15 @@ impl Violation for YodaConditions {
58
57
59
58
#[ derive_message_formats]
60
59
fn message ( & self ) -> String {
61
- let YodaConditions { suggestion } = self ;
62
- if let Some ( suggestion) = suggestion
63
- . as_ref ( )
64
- . and_then ( SourceCodeSnippet :: full_display)
65
- {
66
- format ! ( "Yoda conditions are discouraged, use `{suggestion}` instead" )
67
- } else {
68
- format ! ( "Yoda conditions are discouraged" )
69
- }
60
+ format ! ( "Yoda condition detected" )
70
61
}
71
62
72
63
fn fix_title ( & self ) -> Option < String > {
73
64
let YodaConditions { suggestion } = self ;
74
- suggestion. as_ref ( ) . map ( |suggestion| {
75
- if let Some ( suggestion) = suggestion. full_display ( ) {
76
- format ! ( "Replace Yoda condition with `{suggestion}`" )
77
- } else {
78
- format ! ( "Replace Yoda condition" )
79
- }
80
- } )
65
+ suggestion
66
+ . as_ref ( )
67
+ . and_then ( |suggestion| suggestion. full_display ( ) )
68
+ . map ( |suggestion| format ! ( "Rewrite as `{suggestion}`" ) )
81
69
}
82
70
}
83
71
@@ -94,9 +82,9 @@ enum ConstantLikelihood {
94
82
Definitely = 2 ,
95
83
}
96
84
97
- impl ConstantLikelihood {
85
+ impl From < & Expr > for ConstantLikelihood {
98
86
/// Determine the [`ConstantLikelihood`] of an expression.
99
- fn from_expression ( expr : & Expr , preview : PreviewMode ) -> Self {
87
+ fn from ( expr : & Expr ) -> Self {
100
88
match expr {
101
89
_ if expr. is_literal_expr ( ) => ConstantLikelihood :: Definitely ,
102
90
Expr :: Attribute ( ast:: ExprAttribute { attr, .. } ) => {
@@ -105,34 +93,36 @@ impl ConstantLikelihood {
105
93
Expr :: Name ( ast:: ExprName { id, .. } ) => ConstantLikelihood :: from_identifier ( id) ,
106
94
Expr :: Tuple ( ast:: ExprTuple { elts, .. } ) => elts
107
95
. iter ( )
108
- . map ( |expr| ConstantLikelihood :: from_expression ( expr , preview ) )
96
+ . map ( ConstantLikelihood :: from )
109
97
. min ( )
110
98
. unwrap_or ( ConstantLikelihood :: Definitely ) ,
111
- Expr :: List ( ast:: ExprList { elts, .. } ) if preview . is_enabled ( ) => elts
99
+ Expr :: List ( ast:: ExprList { elts, .. } ) => elts
112
100
. iter ( )
113
- . map ( |expr| ConstantLikelihood :: from_expression ( expr , preview ) )
101
+ . map ( ConstantLikelihood :: from )
114
102
. min ( )
115
103
. unwrap_or ( ConstantLikelihood :: Definitely ) ,
116
- Expr :: Dict ( ast:: ExprDict { items, .. } ) if preview . is_enabled ( ) => {
104
+ Expr :: Dict ( ast:: ExprDict { items, .. } ) => {
117
105
if items. is_empty ( ) {
118
106
ConstantLikelihood :: Definitely
119
107
} else {
120
108
ConstantLikelihood :: Probably
121
109
}
122
110
}
123
111
Expr :: BinOp ( ast:: ExprBinOp { left, right, .. } ) => cmp:: min (
124
- ConstantLikelihood :: from_expression ( left, preview ) ,
125
- ConstantLikelihood :: from_expression ( right, preview ) ,
112
+ ConstantLikelihood :: from ( & * * left) ,
113
+ ConstantLikelihood :: from ( & * * right) ,
126
114
) ,
127
115
Expr :: UnaryOp ( ast:: ExprUnaryOp {
128
116
op : UnaryOp :: UAdd | UnaryOp :: USub | UnaryOp :: Invert ,
129
117
operand,
130
118
range : _,
131
- } ) => ConstantLikelihood :: from_expression ( operand, preview ) ,
119
+ } ) => ConstantLikelihood :: from ( & * * operand) ,
132
120
_ => ConstantLikelihood :: Unlikely ,
133
121
}
134
122
}
123
+ }
135
124
125
+ impl ConstantLikelihood {
136
126
/// Determine the [`ConstantLikelihood`] of an identifier.
137
127
fn from_identifier ( identifier : & str ) -> Self {
138
128
if str:: is_cased_uppercase ( identifier) {
@@ -230,9 +220,7 @@ pub(crate) fn yoda_conditions(
230
220
return ;
231
221
}
232
222
233
- if ConstantLikelihood :: from_expression ( left, checker. settings . preview )
234
- <= ConstantLikelihood :: from_expression ( right, checker. settings . preview )
235
- {
223
+ if ConstantLikelihood :: from ( left) <= ConstantLikelihood :: from ( right) {
236
224
return ;
237
225
}
238
226
0 commit comments