@@ -7,6 +7,7 @@ use source_span::{
7
7
fmt:: { Formatter , Style } ,
8
8
Position ,
9
9
} ;
10
+ use std:: fmt;
10
11
use thiserror:: Error ;
11
12
12
13
macro_rules! check {
@@ -223,83 +224,99 @@ pub enum Warning<'sc> {
223
224
} ,
224
225
}
225
226
226
- impl < ' sc > Warning < ' sc > {
227
- fn to_string ( & self ) -> String {
227
+ impl < ' sc > fmt:: Display for Warning < ' sc > {
228
+ // This trait requires `fmt` with this exact signature.
229
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
228
230
use Warning :: * ;
229
231
match self {
230
- NonClassCaseStructName { struct_name } => format ! (
232
+ NonClassCaseStructName { struct_name } => {
233
+ write ! ( f,
231
234
"Struct name \" {}\" is not idiomatic. Structs should have a ClassCase name, like \
232
235
\" {}\" .",
233
236
struct_name,
234
237
to_class_case( struct_name)
235
- ) ,
236
- NonClassCaseTraitName { name } => format ! (
238
+ )
239
+ }
240
+ NonClassCaseTraitName { name } => {
241
+ write ! ( f,
237
242
"Trait name \" {}\" is not idiomatic. Traits should have a ClassCase name, like \
238
243
\" {}\" .",
239
244
name,
240
245
to_class_case( name)
241
- ) ,
242
- NonClassCaseEnumName { enum_name } => format ! (
246
+ )
247
+ }
248
+ NonClassCaseEnumName { enum_name } => write ! (
249
+ f,
243
250
"Enum \" {}\" 's capitalization is not idiomatic. Enums should have a ClassCase \
244
251
name, like \" {}\" .",
245
252
enum_name,
246
253
to_class_case( enum_name)
247
254
) ,
248
- NonSnakeCaseStructFieldName { field_name } => format ! (
255
+ NonSnakeCaseStructFieldName { field_name } => write ! (
256
+ f,
249
257
"Struct field name \" {}\" is not idiomatic. Struct field names should have a \
250
258
snake_case name, like \" {}\" .",
251
259
field_name,
252
260
to_snake_case( field_name)
253
261
) ,
254
- NonClassCaseEnumVariantName { variant_name } => format ! (
262
+ NonClassCaseEnumVariantName { variant_name } => write ! (
263
+ f,
255
264
"Enum variant name \" {}\" is not idiomatic. Enum variant names should be \
256
265
ClassCase, like \" {}\" .",
257
266
variant_name,
258
267
to_class_case( variant_name)
259
268
) ,
260
- NonSnakeCaseFunctionName { name } => format ! (
269
+ NonSnakeCaseFunctionName { name } => {
270
+ write ! ( f,
261
271
"Function name \" {}\" is not idiomatic. Function names should be snake_case, like \
262
272
\" {}\" .",
263
273
name,
264
274
to_snake_case( name)
265
- ) ,
275
+ )
276
+ }
266
277
LossOfPrecision {
267
278
initial_type,
268
279
cast_to,
269
- } => format ! (
280
+ } => write ! (
281
+ f,
270
282
"This cast, from type {} to type {}, will lose precision." ,
271
283
initial_type. friendly_type_str( ) ,
272
284
cast_to. friendly_type_str( )
273
285
) ,
274
- UnusedReturnValue { r#type } => format ! (
286
+ UnusedReturnValue { r#type } => write ! (
287
+ f,
275
288
"This returns a value of type {}, which is not assigned to anything and is \
276
289
ignored.",
277
290
r#type. friendly_type_str( )
278
291
) ,
279
- SimilarMethodFound { lib, module, name } => format ! (
292
+ SimilarMethodFound { lib, module, name } => write ! (
293
+ f,
280
294
"A method with the same name was found for type {} in dependency \" {}::{}\" . \
281
295
Traits must be in scope in order to access their methods. ",
282
296
name, lib, module
283
297
) ,
284
- OverridesOtherSymbol { name } => format ! (
298
+ OverridesOtherSymbol { name } => write ! (
299
+ f,
285
300
"This import would override another symbol with the same name \" {}\" in this \
286
301
namespace.",
287
302
name
288
303
) ,
289
- OverridingTraitImplementation => format ! (
304
+ OverridingTraitImplementation => write ! (
305
+ f,
290
306
"This trait implementation overrides another one that was previously defined."
291
307
) ,
292
- DeadDeclaration => "This declaration is never used." . into ( ) ,
293
- DeadStructDeclaration => "This struct is never instantiated." . into ( ) ,
294
- DeadFunctionDeclaration => "This function is never called." . into ( ) ,
295
- UnreachableCode => "This code is unreachable." . into ( ) ,
308
+ DeadDeclaration => write ! ( f , "This declaration is never used." ) ,
309
+ DeadStructDeclaration => write ! ( f , "This struct is never instantiated." ) ,
310
+ DeadFunctionDeclaration => write ! ( f , "This function is never called." ) ,
311
+ UnreachableCode => write ! ( f , "This code is unreachable." ) ,
296
312
DeadEnumVariant { variant_name } => {
297
- format ! ( "Enum variant {} is never constructed." , variant_name)
313
+ write ! ( f , "Enum variant {} is never constructed." , variant_name)
298
314
}
299
- DeadTrait => "This trait is never implemented." . into ( ) ,
300
- DeadMethod => "This method is never called." . into ( ) ,
301
- StructFieldNeverRead => "This struct field is never accessed." . into ( ) ,
302
- ShadowingReservedRegister { reg_name } => format ! (
315
+ DeadTrait => write ! ( f, "This trait is never implemented." ) ,
316
+ DeadMethod => write ! ( f, "This method is never called." ) ,
317
+ StructFieldNeverRead => write ! ( f, "This struct field is never accessed." ) ,
318
+ ShadowingReservedRegister { reg_name } => write ! (
319
+ f,
303
320
"This register declaration shadows the reserved register, \" {}\" ." ,
304
321
reg_name
305
322
) ,
@@ -1043,6 +1060,6 @@ impl<'sc> CompileError<'sc> {
1043
1060
let err_start = Position :: new ( start_line - 1 , start_col - 1 ) ;
1044
1061
let err_end = Position :: new ( end_line - 1 , end_col - 1 ) ;
1045
1062
let err_span = source_span:: Span :: new ( err_start, err_end, err_end. next_column ( ) ) ;
1046
- fmt. add ( err_span, Some ( friendly_string. clone ( ) ) , style) ;
1063
+ fmt. add ( err_span, Some ( friendly_string) , style) ;
1047
1064
}
1048
1065
}
0 commit comments