1
+ use std:: borrow:: Cow ;
1
2
use std:: str:: FromStr ;
2
3
3
4
use ruff_diagnostics:: { Diagnostic , Edit , Fix , FixAvailability , Violation } ;
@@ -105,7 +106,7 @@ fn simplify_conversion_flag(flags: CConversionFlags) -> String {
105
106
}
106
107
107
108
/// Convert a [`PercentFormat`] struct into a `String`.
108
- fn handle_part ( part : & CFormatPart < String > ) -> String {
109
+ fn handle_part ( part : & CFormatPart < String > ) -> Cow < ' _ , str > {
109
110
match part {
110
111
CFormatPart :: Literal ( item) => curly_escape ( item) ,
111
112
CFormatPart :: Spec ( spec) => {
@@ -114,7 +115,7 @@ fn handle_part(part: &CFormatPart<String>) -> String {
114
115
// TODO(charlie): What case is this?
115
116
if spec. format_char == '%' {
116
117
format_string. push ( '%' ) ;
117
- return format_string;
118
+ return Cow :: Owned ( format_string) ;
118
119
}
119
120
120
121
format_string. push ( '{' ) ;
@@ -171,37 +172,38 @@ fn handle_part(part: &CFormatPart<String>) -> String {
171
172
format_string. push ( spec. format_char ) ;
172
173
}
173
174
format_string. push ( '}' ) ;
174
- format_string
175
+ Cow :: Owned ( format_string)
175
176
}
176
177
}
177
178
}
178
179
179
180
/// Convert a [`CFormatString`] into a `String`.
180
181
fn percent_to_format ( format_string : & CFormatString ) -> String {
181
- let mut contents = String :: new ( ) ;
182
- for ( .., format_part) in format_string. iter ( ) {
183
- contents. push_str ( & handle_part ( format_part) ) ;
184
- }
185
- contents
182
+ format_string
183
+ . iter ( )
184
+ . map ( |( _, part) | handle_part ( part) )
185
+ . collect ( )
186
186
}
187
187
188
188
/// If a tuple has one argument, remove the comma; otherwise, return it as-is.
189
- fn clean_params_tuple ( right : & Expr , locator : & Locator ) -> String {
190
- let mut contents = locator. slice ( right) . to_string ( ) ;
189
+ fn clean_params_tuple < ' a > ( right : & Expr , locator : & Locator < ' a > ) -> Cow < ' a , str > {
191
190
if let Expr :: Tuple ( ast:: ExprTuple { elts, .. } ) = & right {
192
191
if elts. len ( ) == 1 {
193
192
if !locator. contains_line_break ( right. range ( ) ) {
193
+ let mut contents = locator. slice ( right) . to_string ( ) ;
194
194
for ( i, character) in contents. chars ( ) . rev ( ) . enumerate ( ) {
195
195
if character == ',' {
196
196
let correct_index = contents. len ( ) - i - 1 ;
197
197
contents. remove ( correct_index) ;
198
198
break ;
199
199
}
200
200
}
201
+ return Cow :: Owned ( contents) ;
201
202
}
202
203
}
203
204
}
204
- contents
205
+
206
+ Cow :: Borrowed ( locator. slice ( right) )
205
207
}
206
208
207
209
/// Converts a dictionary to a function call while preserving as much styling as
@@ -419,16 +421,16 @@ pub(crate) fn printf_string_formatting(checker: &mut Checker, expr: &Expr, right
419
421
// Parse the parameters.
420
422
let params_string = match right {
421
423
Expr :: Constant ( _) | Expr :: FString ( _) => {
422
- format ! ( "({})" , checker. locator( ) . slice( right) )
424
+ Cow :: Owned ( format ! ( "({})" , checker. locator( ) . slice( right) ) )
423
425
}
424
426
Expr :: Name ( _) | Expr :: Attribute ( _) | Expr :: Subscript ( _) | Expr :: Call ( _) => {
425
427
if num_keyword_arguments > 0 {
426
428
// If we have _any_ named fields, assume the right-hand side is a mapping.
427
- format ! ( "(**{})" , checker. locator( ) . slice( right) )
429
+ Cow :: Owned ( format ! ( "(**{})" , checker. locator( ) . slice( right) ) )
428
430
} else if num_positional_arguments > 1 {
429
431
// If we have multiple fields, but no named fields, assume the right-hand side is a
430
432
// tuple.
431
- format ! ( "(*{})" , checker. locator( ) . slice( right) )
433
+ Cow :: Owned ( format ! ( "(*{})" , checker. locator( ) . slice( right) ) )
432
434
} else {
433
435
// Otherwise, if we have a single field, we can't make any assumptions about the
434
436
// right-hand side. It _could_ be a tuple, but it could also be a single value,
@@ -444,13 +446,12 @@ pub(crate) fn printf_string_formatting(checker: &mut Checker, expr: &Expr, right
444
446
}
445
447
Expr :: Tuple ( _) => clean_params_tuple ( right, checker. locator ( ) ) ,
446
448
Expr :: Dict ( _) => {
447
- if let Some ( params_string) =
449
+ let Some ( params_string) =
448
450
clean_params_dictionary ( right, checker. locator ( ) , checker. stylist ( ) )
449
- {
450
- params_string
451
- } else {
451
+ else {
452
452
return ;
453
- }
453
+ } ;
454
+ Cow :: Owned ( params_string)
454
455
}
455
456
_ => return ,
456
457
} ;
0 commit comments