@@ -12,7 +12,6 @@ use crate::checkers::ast::{Checker, LintContext};
12
12
use crate :: preview:: is_unicode_to_unicode_confusables_enabled;
13
13
use crate :: rules:: ruff:: rules:: Context ;
14
14
use crate :: rules:: ruff:: rules:: confusables:: confusable;
15
- use crate :: settings:: LinterSettings ;
16
15
17
16
/// ## What it does
18
17
/// Checks for ambiguous Unicode characters in strings.
@@ -180,9 +179,7 @@ pub(crate) fn ambiguous_unicode_character_comment(
180
179
range : TextRange ,
181
180
) {
182
181
let text = locator. slice ( range) ;
183
- for candidate in ambiguous_unicode_character ( text, range, context. settings ( ) ) {
184
- candidate. into_diagnostic ( Context :: Comment , context) ;
185
- }
182
+ ambiguous_unicode_character ( text, range, Context :: Comment , context) ;
186
183
}
187
184
188
185
/// RUF001, RUF002
@@ -203,22 +200,19 @@ pub(crate) fn ambiguous_unicode_character_string(checker: &Checker, string_like:
203
200
match part {
204
201
ast:: StringLikePart :: String ( string_literal) => {
205
202
let text = checker. locator ( ) . slice ( string_literal) ;
206
- for candidate in
207
- ambiguous_unicode_character ( text, string_literal. range ( ) , checker. settings ( ) )
208
- {
209
- candidate. report_diagnostic ( checker, context) ;
210
- }
203
+ ambiguous_unicode_character (
204
+ text,
205
+ string_literal. range ( ) ,
206
+ context,
207
+ checker. context ( ) ,
208
+ ) ;
211
209
}
212
210
ast:: StringLikePart :: Bytes ( _) => { }
213
211
ast:: StringLikePart :: FString ( FString { elements, .. } )
214
212
| ast:: StringLikePart :: TString ( TString { elements, .. } ) => {
215
213
for literal in elements. literals ( ) {
216
214
let text = checker. locator ( ) . slice ( literal) ;
217
- for candidate in
218
- ambiguous_unicode_character ( text, literal. range ( ) , checker. settings ( ) )
219
- {
220
- candidate. report_diagnostic ( checker, context) ;
221
- }
215
+ ambiguous_unicode_character ( text, literal. range ( ) , context, checker. context ( ) ) ;
222
216
}
223
217
}
224
218
}
@@ -228,13 +222,12 @@ pub(crate) fn ambiguous_unicode_character_string(checker: &Checker, string_like:
228
222
fn ambiguous_unicode_character (
229
223
text : & str ,
230
224
range : TextRange ,
231
- settings : & LinterSettings ,
232
- ) -> Vec < Candidate > {
233
- let mut candidates = Vec :: new ( ) ;
234
-
225
+ context : Context ,
226
+ lint_context : & LintContext ,
227
+ ) {
235
228
// Most of the time, we don't need to check for ambiguous unicode characters at all.
236
229
if text. is_ascii ( ) {
237
- return candidates ;
230
+ return ;
238
231
}
239
232
240
233
// Iterate over the "words" in the text.
@@ -246,7 +239,7 @@ fn ambiguous_unicode_character(
246
239
if !word_candidates. is_empty ( ) {
247
240
if word_flags. is_candidate_word ( ) {
248
241
for candidate in word_candidates. drain ( ..) {
249
- candidates . push ( candidate ) ;
242
+ candidate . into_diagnostic ( context , lint_context ) ;
250
243
}
251
244
}
252
245
word_candidates. clear ( ) ;
@@ -257,21 +250,23 @@ fn ambiguous_unicode_character(
257
250
// case, it's always included as a diagnostic.
258
251
if !current_char. is_ascii ( ) {
259
252
if let Some ( representant) = confusable ( current_char as u32 ) . filter ( |representant| {
260
- is_unicode_to_unicode_confusables_enabled ( settings) || representant. is_ascii ( )
253
+ is_unicode_to_unicode_confusables_enabled ( lint_context. settings ( ) )
254
+ || representant. is_ascii ( )
261
255
} ) {
262
256
let candidate = Candidate :: new (
263
257
TextSize :: try_from ( relative_offset) . unwrap ( ) + range. start ( ) ,
264
258
current_char,
265
259
representant,
266
260
) ;
267
- candidates . push ( candidate ) ;
261
+ candidate . into_diagnostic ( context , lint_context ) ;
268
262
}
269
263
}
270
264
} else if current_char. is_ascii ( ) {
271
265
// The current word contains at least one ASCII character.
272
266
word_flags |= WordFlags :: ASCII ;
273
267
} else if let Some ( representant) = confusable ( current_char as u32 ) . filter ( |representant| {
274
- is_unicode_to_unicode_confusables_enabled ( settings) || representant. is_ascii ( )
268
+ is_unicode_to_unicode_confusables_enabled ( lint_context. settings ( ) )
269
+ || representant. is_ascii ( )
275
270
} ) {
276
271
// The current word contains an ambiguous unicode character.
277
272
word_candidates. push ( Candidate :: new (
@@ -289,13 +284,11 @@ fn ambiguous_unicode_character(
289
284
if !word_candidates. is_empty ( ) {
290
285
if word_flags. is_candidate_word ( ) {
291
286
for candidate in word_candidates. drain ( ..) {
292
- candidates . push ( candidate ) ;
287
+ candidate . into_diagnostic ( context , lint_context ) ;
293
288
}
294
289
}
295
290
word_candidates. clear ( ) ;
296
291
}
297
-
298
- candidates
299
292
}
300
293
301
294
bitflags ! {
@@ -373,39 +366,6 @@ impl Candidate {
373
366
} ;
374
367
}
375
368
}
376
-
377
- fn report_diagnostic ( self , checker : & Checker , context : Context ) {
378
- if !checker
379
- . settings ( )
380
- . allowed_confusables
381
- . contains ( & self . confusable )
382
- {
383
- let char_range = TextRange :: at ( self . offset , self . confusable . text_len ( ) ) ;
384
- match context {
385
- Context :: String => checker. report_diagnostic_if_enabled (
386
- AmbiguousUnicodeCharacterString {
387
- confusable : self . confusable ,
388
- representant : self . representant ,
389
- } ,
390
- char_range,
391
- ) ,
392
- Context :: Docstring => checker. report_diagnostic_if_enabled (
393
- AmbiguousUnicodeCharacterDocstring {
394
- confusable : self . confusable ,
395
- representant : self . representant ,
396
- } ,
397
- char_range,
398
- ) ,
399
- Context :: Comment => checker. report_diagnostic_if_enabled (
400
- AmbiguousUnicodeCharacterComment {
401
- confusable : self . confusable ,
402
- representant : self . representant ,
403
- } ,
404
- char_range,
405
- ) ,
406
- } ;
407
- }
408
- }
409
369
}
410
370
411
371
struct NamedUnicode ( char ) ;
0 commit comments