@@ -47,6 +47,7 @@ pub fn capture_opt(m: &QueryMatch, name: &str) -> Result<Option<Cursor>> {
47
47
/// Validation options to control which lints generate a diagnostic
48
48
#[ derive( Debug , Clone , bon:: Builder ) ]
49
49
#[ non_exhaustive]
50
+ #[ allow( clippy:: struct_excessive_bools) ]
50
51
pub struct ValidationOptions {
51
52
/// Whether overridden, public and external functions should have an `@inheritdoc`
52
53
#[ builder( default = true ) ]
@@ -70,7 +71,7 @@ pub struct ValidationOptions {
70
71
#[ builder( default = false ) ]
71
72
pub enum_params : bool ,
72
73
73
- /// Which item types should have at least some NatSpec (@dev, @notice) even if they have no param/return/member.
74
+ /// Which item types should have at least some [` NatSpec`] even if they have no param/return/member.
74
75
#[ builder( default = vec![ ] ) ]
75
76
pub enforce : Vec < ItemType > ,
76
77
}
@@ -198,6 +199,7 @@ impl PartialEq for Definition {
198
199
199
200
impl Definition {
200
201
/// Validate a definition and generate [`Diagnostic`]s for errors
202
+ #[ must_use]
201
203
pub fn validate ( & self , options : & ValidationOptions ) -> ItemDiagnostics {
202
204
match self {
203
205
// if there was an error while parsing the NatSpec comments, a special diagnostic is generated
@@ -230,6 +232,7 @@ impl Definition {
230
232
}
231
233
232
234
/// Retrieve the inner constructor definition
235
+ #[ must_use]
233
236
pub fn as_constructor ( self ) -> Option < ConstructorDefinition > {
234
237
match self {
235
238
Definition :: Constructor ( def) => Some ( def) ,
@@ -238,6 +241,7 @@ impl Definition {
238
241
}
239
242
240
243
/// Retrieve the inner enum definition
244
+ #[ must_use]
241
245
pub fn as_enum ( self ) -> Option < EnumDefinition > {
242
246
match self {
243
247
Definition :: Enumeration ( def) => Some ( def) ,
@@ -246,6 +250,7 @@ impl Definition {
246
250
}
247
251
248
252
/// Retrieve the inner error definition
253
+ #[ must_use]
249
254
pub fn as_error ( self ) -> Option < ErrorDefinition > {
250
255
match self {
251
256
Definition :: Error ( def) => Some ( def) ,
@@ -254,6 +259,7 @@ impl Definition {
254
259
}
255
260
256
261
/// Retrieve the inner event definition
262
+ #[ must_use]
257
263
pub fn as_event ( self ) -> Option < EventDefinition > {
258
264
match self {
259
265
Definition :: Event ( def) => Some ( def) ,
@@ -262,6 +268,7 @@ impl Definition {
262
268
}
263
269
264
270
/// Retrieve the inner function definition
271
+ #[ must_use]
265
272
pub fn as_function ( self ) -> Option < FunctionDefinition > {
266
273
match self {
267
274
Definition :: Function ( def) => Some ( def) ,
@@ -270,6 +277,7 @@ impl Definition {
270
277
}
271
278
272
279
/// Retrieve the inner modifier definition
280
+ #[ must_use]
273
281
pub fn as_modifier ( self ) -> Option < ModifierDefinition > {
274
282
match self {
275
283
Definition :: Modifier ( def) => Some ( def) ,
@@ -278,6 +286,7 @@ impl Definition {
278
286
}
279
287
280
288
/// Retrieve the inner struct definition
289
+ #[ must_use]
281
290
pub fn as_struct ( self ) -> Option < StructDefinition > {
282
291
match self {
283
292
Definition :: Struct ( def) => Some ( def) ,
@@ -286,6 +295,7 @@ impl Definition {
286
295
}
287
296
288
297
/// Retrieve the inner variable declaration
298
+ #[ must_use]
289
299
pub fn as_variable ( self ) -> Option < VariableDeclaration > {
290
300
match self {
291
301
Definition :: Variable ( def) => Some ( def) ,
@@ -348,7 +358,8 @@ pub fn find_items(cursor: Cursor) -> Vec<Definition> {
348
358
/// Extract parameters from a function-like source item.
349
359
///
350
360
/// The node kind that holds the `Identifier` (`Parameter`, `EventParameter`) is provided as `kind`.
351
- pub fn extract_params ( cursor : Cursor , kind : NonterminalKind ) -> Vec < Identifier > {
361
+ #[ must_use]
362
+ pub fn extract_params ( cursor : & Cursor , kind : NonterminalKind ) -> Vec < Identifier > {
352
363
let mut cursor = cursor. spawn ( ) ;
353
364
let mut out = Vec :: new ( ) ;
354
365
while cursor. go_to_next_nonterminal_with_kind ( kind) {
@@ -376,8 +387,8 @@ pub fn extract_params(cursor: Cursor, kind: NonterminalKind) -> Vec<Identifier>
376
387
out
377
388
}
378
389
379
- /// Extract and parse the NatSpec comment information, if any
380
- pub fn extract_comment ( cursor : Cursor , returns : & [ Identifier ] ) -> Result < Option < NatSpec > > {
390
+ /// Extract and parse the [` NatSpec`] comment information, if any
391
+ pub fn extract_comment ( cursor : & Cursor , returns : & [ Identifier ] ) -> Result < Option < NatSpec > > {
381
392
let mut cursor = cursor. spawn ( ) ;
382
393
let mut items = Vec :: new ( ) ;
383
394
while cursor. go_to_next ( ) {
@@ -445,7 +456,8 @@ pub fn extract_comment(cursor: Cursor, returns: &[Identifier]) -> Result<Option<
445
456
}
446
457
447
458
/// Extract identifiers from a CST node, filtered by label equal to `name`
448
- pub fn extract_identifiers ( cursor : Cursor ) -> Vec < Identifier > {
459
+ #[ must_use]
460
+ pub fn extract_identifiers ( cursor : & Cursor ) -> Vec < Identifier > {
449
461
let mut cursor = cursor. spawn ( ) . with_edges ( ) ;
450
462
let mut out = Vec :: new ( ) ;
451
463
while cursor. go_to_next_terminal_with_kind ( TerminalKind :: Identifier ) {
@@ -457,13 +469,14 @@ pub fn extract_identifiers(cursor: Cursor) -> Vec<Identifier> {
457
469
out. push ( Identifier {
458
470
name : Some ( cursor. node ( ) . unparse ( ) . trim ( ) . to_string ( ) ) ,
459
471
span : cursor. text_range ( ) ,
460
- } )
472
+ } ) ;
461
473
}
462
474
out
463
475
}
464
476
465
- /// Check a list of params to see if they are documented with a corresponding item in the NatSpec, and generate a
477
+ /// Check a list of params to see if they are documented with a corresponding item in the [` NatSpec`] , and generate a
466
478
/// diagnostic for each missing one or if there are more than 1 entry per param.
479
+ #[ must_use]
467
480
pub fn check_params ( natspec : & NatSpec , params : & [ Identifier ] ) -> Vec < Diagnostic > {
468
481
let mut res = Vec :: new ( ) ;
469
482
for param in params {
@@ -472,22 +485,24 @@ pub fn check_params(natspec: &NatSpec, params: &[Identifier]) -> Vec<Diagnostic>
472
485
continue ;
473
486
} ;
474
487
let message = match natspec. count_param ( param) {
475
- 0 => format ! ( "@param {} is missing" , name ) ,
476
- 2 .. => format ! ( "@param {} is present more than once" , name ) ,
488
+ 0 => format ! ( "@param {name } is missing" ) ,
489
+ 2 .. => format ! ( "@param {name } is present more than once" ) ,
477
490
_ => {
478
491
continue ;
479
492
}
480
493
} ;
481
494
res. push ( Diagnostic {
482
495
span : param. span . clone ( ) ,
483
496
message,
484
- } )
497
+ } ) ;
485
498
}
486
499
res
487
500
}
488
501
489
- /// Check a list of returns to see if they are documented with a corresponding item in the NatSpec, and generate a
502
+ /// Check a list of returns to see if they are documented with a corresponding item in the [` NatSpec`] , and generate a
490
503
/// diagnostic for each missing one or if there are more than 1 entry per param.
504
+ #[ allow( clippy:: cast_possible_wrap) ]
505
+ #[ must_use]
491
506
pub fn check_returns (
492
507
natspec : & NatSpec ,
493
508
returns : & [ Identifier ] ,
@@ -497,43 +512,39 @@ pub fn check_returns(
497
512
let mut unnamed_returns = 0 ;
498
513
let returns_count = natspec. count_all_returns ( ) as isize ;
499
514
for ( idx, ret) in returns. iter ( ) . enumerate ( ) {
500
- let message = match & ret. name {
501
- Some ( name ) => match natspec. count_return ( ret) {
502
- 0 => format ! ( "@return {} is missing" , name ) ,
503
- 2 .. => format ! ( "@return {} is present more than once" , name ) ,
515
+ let message = if let Some ( name ) = & ret. name {
516
+ match natspec. count_return ( ret) {
517
+ 0 => format ! ( "@return {name } is missing" ) ,
518
+ 2 .. => format ! ( "@return {name } is present more than once" ) ,
504
519
_ => {
505
520
continue ;
506
521
}
507
- } ,
508
- None => {
509
- unnamed_returns += 1 ;
510
- if idx as isize > returns_count - 1 {
511
- format ! ( "@return missing for unnamed return #{}" , idx + 1 )
512
- } else {
513
- continue ;
514
- }
522
+ }
523
+ } else {
524
+ unnamed_returns += 1 ;
525
+ if idx as isize > returns_count - 1 {
526
+ format ! ( "@return missing for unnamed return #{}" , idx + 1 )
527
+ } else {
528
+ continue ;
515
529
}
516
530
} ;
517
531
res. push ( Diagnostic {
518
532
span : ret. span . clone ( ) ,
519
533
message,
520
- } )
534
+ } ) ;
521
535
}
522
536
if natspec. count_unnamed_returns ( ) > unnamed_returns {
523
537
res. push ( Diagnostic {
524
- span : returns
525
- . last ( )
526
- . cloned ( )
527
- . map ( |r| r. span )
528
- . unwrap_or ( default_span) ,
538
+ span : returns. last ( ) . cloned ( ) . map_or ( default_span, |r| r. span ) ,
529
539
message : "too many unnamed returns" . to_string ( ) ,
530
- } )
540
+ } ) ;
531
541
}
532
542
res
533
543
}
534
544
535
545
/// Extract the attributes (visibility and override) from a function-like item or state variable
536
- pub fn extract_attributes ( cursor : Cursor ) -> Attributes {
546
+ #[ must_use]
547
+ pub fn extract_attributes ( cursor : & Cursor ) -> Attributes {
537
548
let mut cursor = cursor. spawn ( ) ;
538
549
let mut out = Attributes :: default ( ) ;
539
550
while cursor. go_to_next_terminal_with_kinds ( & [
@@ -561,6 +572,7 @@ pub fn extract_attributes(cursor: Cursor) -> Attributes {
561
572
}
562
573
563
574
/// Find the parent's name (contract, interface, library), if any
575
+ #[ must_use]
564
576
pub fn extract_parent_name ( mut cursor : Cursor ) -> Option < Parent > {
565
577
while cursor. go_to_parent ( ) {
566
578
if let Some ( parent) = cursor. node ( ) . as_nonterminal_with_kinds ( & [
@@ -647,7 +659,7 @@ mod tests {
647
659
kind: NatSpecKind :: Inheritdoc {
648
660
parent: "IParserTest" . to_string( )
649
661
} ,
650
- comment: "" . to_string ( )
662
+ comment: String :: new ( )
651
663
} ,
652
664
NatSpecItem {
653
665
kind: NatSpecKind :: Dev ,
@@ -672,7 +684,7 @@ mod tests {
672
684
kind: NatSpecKind :: Inheritdoc {
673
685
parent: "IParserTest" . to_string( )
674
686
} ,
675
- comment: "" . to_string ( )
687
+ comment: String :: new ( )
676
688
} , ]
677
689
) ;
678
690
}
@@ -692,7 +704,7 @@ mod tests {
692
704
kind: NatSpecKind :: Inheritdoc {
693
705
parent: "IParserTest" . to_string( )
694
706
} ,
695
- comment: "" . to_string ( )
707
+ comment: String :: new ( )
696
708
} , ]
697
709
) ;
698
710
}
@@ -993,7 +1005,7 @@ mod tests {
993
1005
kind: NatSpecKind :: Inheritdoc {
994
1006
parent: "IParserTest" . to_string( )
995
1007
} ,
996
- comment: "" . to_string ( )
1008
+ comment: String :: new ( )
997
1009
} ,
998
1010
NatSpecItem {
999
1011
kind: NatSpecKind :: Dev ,
@@ -1145,11 +1157,11 @@ mod tests {
1145
1157
} ,
1146
1158
NatSpecItem {
1147
1159
kind: NatSpecKind :: Return { name: None } ,
1148
- comment: "" . to_string ( )
1160
+ comment: String :: new ( )
1149
1161
} ,
1150
1162
NatSpecItem {
1151
1163
kind: NatSpecKind :: Return { name: None } ,
1152
- comment: "" . to_string ( )
1164
+ comment: String :: new ( )
1153
1165
} ,
1154
1166
]
1155
1167
) ;
0 commit comments