@@ -2,7 +2,7 @@ use crate::{LateContext, LateLintPass, LintContext};
2
2
use rustc_ast as ast;
3
3
use rustc_attr as attr;
4
4
use rustc_data_structures:: fx:: FxHashSet ;
5
- use rustc_errors:: Applicability ;
5
+ use rustc_errors:: { fluent , Applicability } ;
6
6
use rustc_hir as hir;
7
7
use rustc_hir:: { is_range_literal, Expr , ExprKind , Node } ;
8
8
use rustc_middle:: ty:: layout:: { IntegerExt , LayoutOf , SizeSkeleton } ;
@@ -139,7 +139,8 @@ fn lint_overflowing_range_endpoint<'tcx>(
139
139
// overflowing and only by 1.
140
140
if eps[ 1 ] . expr . hir_id == expr. hir_id && lit_val - 1 == max {
141
141
cx. struct_span_lint ( OVERFLOWING_LITERALS , parent_expr. span , |lint| {
142
- let mut err = lint. build ( & format ! ( "range endpoint is out of range for `{}`" , ty) ) ;
142
+ let mut err = lint. build ( fluent:: lint:: range_endpoint_out_of_range) ;
143
+ err. set_arg ( "ty" , ty) ;
143
144
if let Ok ( start) = cx. sess ( ) . source_map ( ) . span_to_snippet ( eps[ 0 ] . span ) {
144
145
use ast:: { LitIntType , LitKind } ;
145
146
// We need to preserve the literal's suffix,
@@ -153,7 +154,7 @@ fn lint_overflowing_range_endpoint<'tcx>(
153
154
let suggestion = format ! ( "{}..={}{}" , start, lit_val - 1 , suffix) ;
154
155
err. span_suggestion (
155
156
parent_expr. span ,
156
- "use an inclusive range instead" ,
157
+ fluent :: lint :: suggestion ,
157
158
suggestion,
158
159
Applicability :: MachineApplicable ,
159
160
) ;
@@ -229,38 +230,35 @@ fn report_bin_hex_error(
229
230
( t. name_str ( ) , actually. to_string ( ) )
230
231
}
231
232
} ;
232
- let mut err = lint. build ( & format ! ( "literal out of range for `{}`" , t ) ) ;
233
+ let mut err = lint. build ( fluent :: lint :: overflowing_bin_hex ) ;
233
234
if negative {
234
235
// If the value is negative,
235
236
// emits a note about the value itself, apart from the literal.
236
- err. note ( & format ! (
237
- "the literal `{}` (decimal `{}`) does not fit into \
238
- the type `{}`",
239
- repr_str, val, t
240
- ) ) ;
241
- err. note ( & format ! ( "and the value `-{}` will become `{}{}`" , repr_str, actually, t) ) ;
237
+ err. note ( fluent:: lint:: negative_note) ;
238
+ err. note ( fluent:: lint:: negative_becomes_note) ;
242
239
} else {
243
- err. note ( & format ! (
244
- "the literal `{}` (decimal `{}`) does not fit into \
245
- the type `{}` and will become `{}{}`",
246
- repr_str, val, t, actually, t
247
- ) ) ;
240
+ err. note ( fluent:: lint:: positive_note) ;
248
241
}
249
242
if let Some ( sugg_ty) =
250
243
get_type_suggestion ( cx. typeck_results ( ) . node_type ( expr. hir_id ) , val, negative)
251
244
{
245
+ err. set_arg ( "suggestion_ty" , sugg_ty) ;
252
246
if let Some ( pos) = repr_str. chars ( ) . position ( |c| c == 'i' || c == 'u' ) {
253
247
let ( sans_suffix, _) = repr_str. split_at ( pos) ;
254
248
err. span_suggestion (
255
249
expr. span ,
256
- & format ! ( "consider using the type `{}` instead" , sugg_ty ) ,
250
+ fluent :: lint :: suggestion ,
257
251
format ! ( "{}{}" , sans_suffix, sugg_ty) ,
258
252
Applicability :: MachineApplicable ,
259
253
) ;
260
254
} else {
261
- err. help ( & format ! ( "consider using the type `{}` instead" , sugg_ty ) ) ;
255
+ err. help ( fluent :: lint :: help ) ;
262
256
}
263
257
}
258
+ err. set_arg ( "ty" , t) ;
259
+ err. set_arg ( "lit" , repr_str) ;
260
+ err. set_arg ( "dec" , val) ;
261
+ err. set_arg ( "actually" , actually) ;
264
262
err. emit ( ) ;
265
263
} ) ;
266
264
}
@@ -353,21 +351,23 @@ fn lint_int_literal<'tcx>(
353
351
}
354
352
355
353
cx. struct_span_lint ( OVERFLOWING_LITERALS , e. span , |lint| {
356
- let mut err = lint. build ( & format ! ( "literal out of range for `{}`" , t. name_str( ) ) ) ;
357
- err. note ( & format ! (
358
- "the literal `{}` does not fit into the type `{}` whose range is `{}..={}`" ,
354
+ let mut err = lint. build ( fluent:: lint:: overflowing_int) ;
355
+ err. set_arg ( "ty" , t. name_str ( ) ) ;
356
+ err. set_arg (
357
+ "lit" ,
359
358
cx. sess ( )
360
359
. source_map ( )
361
360
. span_to_snippet ( lit. span )
362
361
. expect ( "must get snippet from literal" ) ,
363
- t . name_str ( ) ,
364
- min,
365
- max,
366
- ) ) ;
362
+ ) ;
363
+ err . set_arg ( " min" , min ) ;
364
+ err . set_arg ( " max" , max ) ;
365
+ err . note ( fluent :: lint :: note ) ;
367
366
if let Some ( sugg_ty) =
368
367
get_type_suggestion ( cx. typeck_results ( ) . node_type ( e. hir_id ) , v, negative)
369
368
{
370
- err. help ( & format ! ( "consider using the type `{}` instead" , sugg_ty) ) ;
369
+ err. set_arg ( "suggestion_ty" , sugg_ty) ;
370
+ err. help ( fluent:: lint:: help) ;
371
371
}
372
372
err. emit ( ) ;
373
373
} ) ;
@@ -395,10 +395,10 @@ fn lint_uint_literal<'tcx>(
395
395
hir:: ExprKind :: Cast ( ..) => {
396
396
if let ty:: Char = cx. typeck_results ( ) . expr_ty ( par_e) . kind ( ) {
397
397
cx. struct_span_lint ( OVERFLOWING_LITERALS , par_e. span , |lint| {
398
- lint. build ( "only `u8` can be cast into `char`" )
398
+ lint. build ( fluent :: lint :: only_cast_u8_to_char )
399
399
. span_suggestion (
400
400
par_e. span ,
401
- "use a `char` literal instead" ,
401
+ fluent :: lint :: suggestion ,
402
402
format ! ( "'\\ u{{{:X}}}'" , lit_val) ,
403
403
Applicability :: MachineApplicable ,
404
404
)
@@ -429,17 +429,18 @@ fn lint_uint_literal<'tcx>(
429
429
return ;
430
430
}
431
431
cx. struct_span_lint ( OVERFLOWING_LITERALS , e. span , |lint| {
432
- lint. build ( & format ! ( "literal out of range for `{}`" , t. name_str( ) ) )
433
- . note ( & format ! (
434
- "the literal `{}` does not fit into the type `{}` whose range is `{}..={}`" ,
432
+ lint. build ( fluent:: lint:: overflowing_uint)
433
+ . set_arg ( "ty" , t. name_str ( ) )
434
+ . set_arg (
435
+ "lit" ,
435
436
cx. sess ( )
436
437
. source_map ( )
437
438
. span_to_snippet ( lit. span )
438
439
. expect ( "must get snippet from literal" ) ,
439
- t . name_str ( ) ,
440
- min,
441
- max,
442
- ) )
440
+ )
441
+ . set_arg ( " min" , min )
442
+ . set_arg ( " max" , max )
443
+ . note ( fluent :: lint :: note )
443
444
. emit ( ) ;
444
445
} ) ;
445
446
}
@@ -471,16 +472,16 @@ fn lint_literal<'tcx>(
471
472
} ;
472
473
if is_infinite == Ok ( true ) {
473
474
cx. struct_span_lint ( OVERFLOWING_LITERALS , e. span , |lint| {
474
- lint. build ( & format ! ( "literal out of range for `{}`" , t. name_str( ) ) )
475
- . note ( & format ! (
476
- "the literal `{}` does not fit into the type `{}` and will be converted to `{}::INFINITY`" ,
475
+ lint. build ( fluent:: lint:: overflowing_literal)
476
+ . set_arg ( "ty" , t. name_str ( ) )
477
+ . set_arg (
478
+ "lit" ,
477
479
cx. sess ( )
478
480
. source_map ( )
479
481
. span_to_snippet ( lit. span )
480
482
. expect ( "must get snippet from literal" ) ,
481
- t. name_str( ) ,
482
- t. name_str( ) ,
483
- ) )
483
+ )
484
+ . note ( fluent:: lint:: note)
484
485
. emit ( ) ;
485
486
} ) ;
486
487
}
@@ -501,7 +502,7 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
501
502
hir:: ExprKind :: Binary ( binop, ref l, ref r) => {
502
503
if is_comparison ( binop) && !check_limits ( cx, binop, & l, & r) {
503
504
cx. struct_span_lint ( UNUSED_COMPARISONS , e. span , |lint| {
504
- lint. build ( "comparison is useless due to type limits" ) . emit ( ) ;
505
+ lint. build ( fluent :: lint :: unused_comparisons ) . emit ( ) ;
505
506
} ) ;
506
507
}
507
508
}
0 commit comments