1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use core::fmt;
use std::path::PathBuf;
use std::sync::Arc;
use sway_types::{integer_bits::IntegerBits, Ident, Span, Spanned};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CompileWarning {
pub span: Span,
pub warning_content: Warning,
}
impl Spanned for CompileWarning {
fn span(&self) -> Span {
self.span.clone()
}
}
impl CompileWarning {
pub fn to_friendly_warning_string(&self) -> String {
self.warning_content.to_string()
}
pub fn path(&self) -> Option<Arc<PathBuf>> {
self.span.path().cloned()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Warning {
NonClassCaseStructName {
struct_name: Ident,
},
NonClassCaseTypeParameter {
name: Ident,
},
NonClassCaseTraitName {
name: Ident,
},
NonClassCaseEnumName {
enum_name: Ident,
},
NonClassCaseEnumVariantName {
variant_name: Ident,
},
NonSnakeCaseStructFieldName {
field_name: Ident,
},
NonSnakeCaseFunctionName {
name: Ident,
},
NonScreamingSnakeCaseConstName {
name: Ident,
},
LossOfPrecision {
initial_type: IntegerBits,
cast_to: IntegerBits,
},
UnusedReturnValue {
r#type: String,
},
SimilarMethodFound {
lib: Ident,
module: Ident,
name: Ident,
},
ShadowsOtherSymbol {
name: Ident,
},
OverridingTraitImplementation,
DeadDeclaration,
DeadEnumDeclaration,
DeadFunctionDeclaration,
DeadStructDeclaration,
DeadTrait,
UnreachableCode,
DeadEnumVariant {
variant_name: Ident,
},
DeadMethod,
StructFieldNeverRead,
ShadowingReservedRegister {
reg_name: Ident,
},
DeadStorageDeclaration,
DeadStorageDeclarationForFunction {
unneeded_attrib: String,
},
MatchExpressionUnreachableArm,
UnrecognizedAttribute {
attrib_name: Ident,
},
EffectAfterInteraction {
effect: String,
effect_in_suggestion: String,
block_name: Ident,
},
}
impl fmt::Display for Warning {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use sway_types::style::*;
use Warning::*;
match self {
NonClassCaseStructName { struct_name } => {
write!(f,
"Struct name \"{}\" is not idiomatic. Structs should have a ClassCase name, like \
\"{}\".",
struct_name,
to_upper_camel_case(struct_name.as_str())
)
}
NonClassCaseTypeParameter { name } => {
write!(f,
"Type parameter \"{}\" is not idiomatic. TypeParameters should have a ClassCase name, like \
\"{}\".",
name,
to_upper_camel_case(name.as_str())
)
}
NonClassCaseTraitName { name } => {
write!(f,
"Trait name \"{}\" is not idiomatic. Traits should have a ClassCase name, like \
\"{}\".",
name,
to_upper_camel_case(name.as_str())
)
}
NonClassCaseEnumName { enum_name } => write!(
f,
"Enum \"{}\"'s capitalization is not idiomatic. Enums should have a ClassCase \
name, like \"{}\".",
enum_name,
to_upper_camel_case(enum_name.as_str())
),
NonSnakeCaseStructFieldName { field_name } => write!(
f,
"Struct field name \"{}\" is not idiomatic. Struct field names should have a \
snake_case name, like \"{}\".",
field_name,
to_snake_case(field_name.as_str())
),
NonClassCaseEnumVariantName { variant_name } => write!(
f,
"Enum variant name \"{}\" is not idiomatic. Enum variant names should be \
ClassCase, like \"{}\".",
variant_name,
to_upper_camel_case(variant_name.as_str())
),
NonSnakeCaseFunctionName { name } => {
write!(f,
"Function name \"{}\" is not idiomatic. Function names should be snake_case, like \
\"{}\".",
name,
to_snake_case(name.as_str())
)
}
NonScreamingSnakeCaseConstName { name } => {
write!(
f,
"Constant name \"{}\" is not idiomatic. Constant names should be SCREAMING_SNAKE_CASE, like \
\"{}\".",
name,
to_screaming_snake_case(name.as_str()),
)
},
LossOfPrecision {
initial_type,
cast_to,
} => write!(f,
"This cast, from integer type of width {initial_type} to integer type of width {cast_to}, will lose precision."
),
UnusedReturnValue { r#type } => write!(
f,
"This returns a value of type {type}, which is not assigned to anything and is \
ignored."
),
SimilarMethodFound { lib, module, name } => write!(
f,
"A method with the same name was found for type {name} in dependency \"{lib}::{module}\". \
Traits must be in scope in order to access their methods. "
),
ShadowsOtherSymbol { name } => write!(
f,
"This shadows another symbol in this scope with the same name \"{name}\"."
),
OverridingTraitImplementation => write!(
f,
"This trait implementation overrides another one that was previously defined."
),
DeadDeclaration => write!(f, "This declaration is never used."),
DeadEnumDeclaration => write!(f, "This enum is never used."),
DeadStructDeclaration => write!(f, "This struct is never used."),
DeadFunctionDeclaration => write!(f, "This function is never called."),
UnreachableCode => write!(f, "This code is unreachable."),
DeadEnumVariant { variant_name } => {
write!(f, "Enum variant {variant_name} is never constructed.")
}
DeadTrait => write!(f, "This trait is never implemented."),
DeadMethod => write!(f, "This method is never called."),
StructFieldNeverRead => write!(f, "This struct field is never accessed."),
ShadowingReservedRegister { reg_name } => write!(
f,
"This register declaration shadows the reserved register, \"{reg_name}\"."
),
DeadStorageDeclaration => write!(
f,
"This storage declaration is never accessed and can be removed."
),
DeadStorageDeclarationForFunction { unneeded_attrib } => write!(
f,
"This function's storage attributes declaration does not match its \
actual storage access pattern: '{unneeded_attrib}' attribute(s) can be removed."
),
MatchExpressionUnreachableArm => write!(f, "This match arm is unreachable."),
UnrecognizedAttribute {attrib_name} => write!(f, "Unknown attribute: \"{attrib_name}\"."),
EffectAfterInteraction {effect, effect_in_suggestion, block_name} =>
write!(f, "{effect} after external contract interaction in function or method \"{block_name}\". \
Consider {effect_in_suggestion} before calling another contract"),
}
}
}
#[cfg(test)]
mod test {
use sway_types::style::*;
#[test]
fn detect_styles() {
let snake_cases = [
"hello",
"__hello",
"blah32",
"some_words_here",
"___some_words_here",
];
let screaming_snake_cases = ["SOME_WORDS_HERE", "___SOME_WORDS_HERE"];
let upper_camel_cases = [
"Hello",
"__Hello",
"Blah32",
"SomeWordsHere",
"___SomeWordsHere",
];
let screaming_snake_case_or_upper_camel_case_idents = ["HELLO", "__HELLO", "BLAH32"];
let styleless_idents = ["Mix_Of_Things", "__Mix_Of_Things", "FooBar_123"];
for name in &snake_cases {
assert!(is_snake_case(name));
assert!(!is_screaming_snake_case(name));
assert!(!is_upper_camel_case(name));
}
for name in &screaming_snake_cases {
assert!(!is_snake_case(name));
assert!(is_screaming_snake_case(name));
assert!(!is_upper_camel_case(name));
}
for name in &upper_camel_cases {
assert!(!is_snake_case(name));
assert!(!is_screaming_snake_case(name));
assert!(is_upper_camel_case(name));
}
for name in &screaming_snake_case_or_upper_camel_case_idents {
assert!(!is_snake_case(name));
assert!(is_screaming_snake_case(name));
assert!(is_upper_camel_case(name));
}
for name in &styleless_idents {
assert!(!is_snake_case(name));
assert!(!is_screaming_snake_case(name));
assert!(!is_upper_camel_case(name));
}
}
#[test]
fn convert_to_snake_case() {
assert_eq!("hello", to_snake_case("HELLO"));
assert_eq!("___hello", to_snake_case("___HELLO"));
assert_eq!("blah32", to_snake_case("BLAH32"));
assert_eq!("some_words_here", to_snake_case("SOME_WORDS_HERE"));
assert_eq!("___some_words_here", to_snake_case("___SOME_WORDS_HERE"));
assert_eq!("hello", to_snake_case("Hello"));
assert_eq!("___hello", to_snake_case("___Hello"));
assert_eq!("blah32", to_snake_case("Blah32"));
assert_eq!("some_words_here", to_snake_case("SomeWordsHere"));
assert_eq!("___some_words_here", to_snake_case("___SomeWordsHere"));
assert_eq!("some_words_here", to_snake_case("someWordsHere"));
assert_eq!("___some_words_here", to_snake_case("___someWordsHere"));
assert_eq!("mix_of_things", to_snake_case("Mix_Of_Things"));
assert_eq!("__mix_of_things", to_snake_case("__Mix_Of_Things"));
assert_eq!("foo_bar_123", to_snake_case("FooBar_123"));
}
#[test]
fn convert_to_screaming_snake_case() {
assert_eq!("HELLO", to_screaming_snake_case("hello"));
assert_eq!("___HELLO", to_screaming_snake_case("___hello"));
assert_eq!("BLAH32", to_screaming_snake_case("blah32"));
assert_eq!(
"SOME_WORDS_HERE",
to_screaming_snake_case("some_words_here")
);
assert_eq!(
"___SOME_WORDS_HERE",
to_screaming_snake_case("___some_words_here")
);
assert_eq!("HELLO", to_screaming_snake_case("Hello"));
assert_eq!("___HELLO", to_screaming_snake_case("___Hello"));
assert_eq!("BLAH32", to_screaming_snake_case("Blah32"));
assert_eq!("SOME_WORDS_HERE", to_screaming_snake_case("SomeWordsHere"));
assert_eq!(
"___SOME_WORDS_HERE",
to_screaming_snake_case("___SomeWordsHere")
);
assert_eq!("SOME_WORDS_HERE", to_screaming_snake_case("someWordsHere"));
assert_eq!(
"___SOME_WORDS_HERE",
to_screaming_snake_case("___someWordsHere")
);
assert_eq!("MIX_OF_THINGS", to_screaming_snake_case("Mix_Of_Things"));
assert_eq!(
"__MIX_OF_THINGS",
to_screaming_snake_case("__Mix_Of_Things")
);
assert_eq!("FOO_BAR_123", to_screaming_snake_case("FooBar_123"));
}
#[test]
fn convert_to_upper_camel_case() {
assert_eq!("Hello", to_upper_camel_case("hello"));
assert_eq!("___Hello", to_upper_camel_case("___hello"));
assert_eq!("Blah32", to_upper_camel_case("blah32"));
assert_eq!("SomeWordsHere", to_upper_camel_case("some_words_here"));
assert_eq!(
"___SomeWordsHere",
to_upper_camel_case("___some_words_here")
);
assert_eq!("Hello", to_upper_camel_case("HELLO"));
assert_eq!("___Hello", to_upper_camel_case("___HELLO"));
assert_eq!("Blah32", to_upper_camel_case("BLAH32"));
assert_eq!("SomeWordsHere", to_upper_camel_case("SOME_WORDS_HERE"));
assert_eq!(
"___SomeWordsHere",
to_upper_camel_case("___SOME_WORDS_HERE")
);
assert_eq!("SomeWordsHere", to_upper_camel_case("someWordsHere"));
assert_eq!("___SomeWordsHere", to_upper_camel_case("___someWordsHere"));
assert_eq!("MixOfThings", to_upper_camel_case("Mix_Of_Things"));
assert_eq!("__MixOfThings", to_upper_camel_case("__Mix_Of_Things"));
assert_eq!("FooBar123", to_upper_camel_case("FooBar_123"));
}
}