@@ -16,7 +16,6 @@ use crate::cst::helpers::or_space;
1616use crate :: cst:: matchers:: { match_comparison, transform_expression} ;
1717use crate :: fix:: edits:: pad;
1818use crate :: fix:: snippet:: SourceCodeSnippet ;
19- use crate :: settings:: types:: PreviewMode ;
2019
2120/// ## What it does
2221/// Checks for conditions that position a constant on the left-hand side of the
@@ -58,26 +57,15 @@ impl Violation for YodaConditions {
5857
5958 #[ derive_message_formats]
6059 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" )
7061 }
7162
7263 fn fix_title ( & self ) -> Option < String > {
7364 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}`" ) )
8169 }
8270}
8371
@@ -94,9 +82,9 @@ enum ConstantLikelihood {
9482 Definitely = 2 ,
9583}
9684
97- impl ConstantLikelihood {
85+ impl From < & Expr > for ConstantLikelihood {
9886 /// Determine the [`ConstantLikelihood`] of an expression.
99- fn from_expression ( expr : & Expr , preview : PreviewMode ) -> Self {
87+ fn from ( expr : & Expr ) -> Self {
10088 match expr {
10189 _ if expr. is_literal_expr ( ) => ConstantLikelihood :: Definitely ,
10290 Expr :: Attribute ( ast:: ExprAttribute { attr, .. } ) => {
@@ -105,34 +93,36 @@ impl ConstantLikelihood {
10593 Expr :: Name ( ast:: ExprName { id, .. } ) => ConstantLikelihood :: from_identifier ( id) ,
10694 Expr :: Tuple ( ast:: ExprTuple { elts, .. } ) => elts
10795 . iter ( )
108- . map ( |expr| ConstantLikelihood :: from_expression ( expr , preview ) )
96+ . map ( ConstantLikelihood :: from )
10997 . min ( )
11098 . unwrap_or ( ConstantLikelihood :: Definitely ) ,
111- Expr :: List ( ast:: ExprList { elts, .. } ) if preview . is_enabled ( ) => elts
99+ Expr :: List ( ast:: ExprList { elts, .. } ) => elts
112100 . iter ( )
113- . map ( |expr| ConstantLikelihood :: from_expression ( expr , preview ) )
101+ . map ( ConstantLikelihood :: from )
114102 . min ( )
115103 . unwrap_or ( ConstantLikelihood :: Definitely ) ,
116- Expr :: Dict ( ast:: ExprDict { items, .. } ) if preview . is_enabled ( ) => {
104+ Expr :: Dict ( ast:: ExprDict { items, .. } ) => {
117105 if items. is_empty ( ) {
118106 ConstantLikelihood :: Definitely
119107 } else {
120108 ConstantLikelihood :: Probably
121109 }
122110 }
123111 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) ,
126114 ) ,
127115 Expr :: UnaryOp ( ast:: ExprUnaryOp {
128116 op : UnaryOp :: UAdd | UnaryOp :: USub | UnaryOp :: Invert ,
129117 operand,
130118 range : _,
131- } ) => ConstantLikelihood :: from_expression ( operand, preview ) ,
119+ } ) => ConstantLikelihood :: from ( & * * operand) ,
132120 _ => ConstantLikelihood :: Unlikely ,
133121 }
134122 }
123+ }
135124
125+ impl ConstantLikelihood {
136126 /// Determine the [`ConstantLikelihood`] of an identifier.
137127 fn from_identifier ( identifier : & str ) -> Self {
138128 if str:: is_cased_uppercase ( identifier) {
@@ -230,9 +220,7 @@ pub(crate) fn yoda_conditions(
230220 return ;
231221 }
232222
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) {
236224 return ;
237225 }
238226
0 commit comments