Skip to content

Commit 9c7dad9

Browse files
authored
refactor!: apply more lints from clippy (#45)
Some functions could benefit from an API change, so this is breaking
1 parent 7373f00 commit 9c7dad9

16 files changed

+117
-87
lines changed

Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ repository = "https://github.com/beeb/lintspec"
1111
rust-version = "1.80.0"
1212
version = "0.2.0"
1313

14+
[lints.clippy]
15+
module_name_repetitions = "allow"
16+
missing_errors_doc = "allow"
17+
missing_panics_doc = "allow"
18+
pedantic = { level = "warn", priority = -1 }
19+
1420
[dependencies]
1521
anyhow = "1.0.95"
1622
bon = "3.3.2"

src/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ pub struct Args {
9494
#[derive(Debug, Clone, Serialize, Deserialize, bon::Builder)]
9595
#[non_exhaustive]
9696
#[builder(on(PathBuf, into))]
97+
#[allow(clippy::struct_excessive_bools)]
9798
pub struct Config {
9899
pub paths: Vec<PathBuf>,
99100
pub exclude: Vec<PathBuf>,
@@ -126,6 +127,7 @@ impl From<Args> for Config {
126127
}
127128
}
128129

130+
/// Read the configuration from config file, environment variables and CLI arguments
129131
pub fn read_config() -> Result<Config> {
130132
let args = Args::parse();
131133
let mut temp: Args = Figment::new()

src/definitions/constructor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ impl Validate for ConstructorDefinition {
5050
let params = capture(&m, "constructor_params")?;
5151

5252
let span = params.text_range();
53-
let params = extract_params(params, NonterminalKind::Parameter);
54-
let natspec = extract_comment(constructor.clone(), &[])?;
53+
let params = extract_params(&params, NonterminalKind::Parameter);
54+
let natspec = extract_comment(&constructor.clone(), &[])?;
5555
let parent = extract_parent_name(constructor);
5656

5757
Ok(ConstructorDefinition {

src/definitions/enumeration.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ impl Validate for EnumDefinition {
5353

5454
let span = enumeration.text_range();
5555
let name = name.node().unparse().trim().to_string();
56-
let members = extract_enum_members(members);
57-
let natspec = extract_comment(enumeration.clone(), &[])?;
56+
let members = extract_enum_members(&members);
57+
let natspec = extract_comment(&enumeration.clone(), &[])?;
5858
let parent = extract_parent_name(enumeration);
5959

6060
Ok(EnumDefinition {
@@ -94,14 +94,14 @@ impl Validate for EnumDefinition {
9494
}
9595
}
9696

97-
fn extract_enum_members(cursor: Cursor) -> Vec<Identifier> {
97+
fn extract_enum_members(cursor: &Cursor) -> Vec<Identifier> {
9898
let mut cursor = cursor.spawn().with_edges();
9999
let mut out = Vec::new();
100100
while cursor.go_to_next_terminal_with_kind(TerminalKind::Identifier) {
101101
out.push(Identifier {
102102
name: Some(cursor.node().unparse().trim().to_string()),
103103
span: cursor.text_range(),
104-
})
104+
});
105105
}
106106
out
107107
}

src/definitions/error.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ impl Validate for ErrorDefinition {
5353

5454
let span = err.text_range();
5555
let name = name.node().unparse().trim().to_string();
56-
let params = extract_identifiers(params);
57-
let natspec = extract_comment(err.clone(), &[])?;
56+
let params = extract_identifiers(&params);
57+
let natspec = extract_comment(&err.clone(), &[])?;
5858
let parent = extract_parent_name(err);
5959

6060
Ok(ErrorDefinition {

src/definitions/event.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ impl Validate for EventDefinition {
5353

5454
let span = event.text_range();
5555
let name = name.node().unparse().trim().to_string();
56-
let params = extract_params(params, NonterminalKind::EventParameter);
57-
let natspec = extract_comment(event.clone(), &[])?;
56+
let params = extract_params(&params, NonterminalKind::EventParameter);
57+
let natspec = extract_comment(&event.clone(), &[])?;
5858
let parent = extract_parent_name(event);
5959

6060
Ok(EventDefinition {

src/definitions/function.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ impl Validate for FunctionDefinition {
8888
keyword.text_range().start..attributes.text_range().end
8989
};
9090
let name = name.node().unparse().trim().to_string();
91-
let params = extract_params(params, NonterminalKind::Parameter);
91+
let params = extract_params(&params, NonterminalKind::Parameter);
9292
let returns = returns
93-
.map(|r| extract_params(r, NonterminalKind::Parameter))
93+
.map(|r| extract_params(&r, NonterminalKind::Parameter))
9494
.unwrap_or_default();
95-
let natspec = extract_comment(func.clone(), &returns)?;
95+
let natspec = extract_comment(&func.clone(), &returns)?;
9696
let parent = extract_parent_name(func);
9797

9898
Ok(FunctionDefinition {
@@ -102,7 +102,7 @@ impl Validate for FunctionDefinition {
102102
params,
103103
returns,
104104
natspec,
105-
attributes: extract_attributes(attributes),
105+
attributes: extract_attributes(&attributes),
106106
}
107107
.into())
108108
}

src/definitions/mod.rs

+49-37
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub fn capture_opt(m: &QueryMatch, name: &str) -> Result<Option<Cursor>> {
4747
/// Validation options to control which lints generate a diagnostic
4848
#[derive(Debug, Clone, bon::Builder)]
4949
#[non_exhaustive]
50+
#[allow(clippy::struct_excessive_bools)]
5051
pub struct ValidationOptions {
5152
/// Whether overridden, public and external functions should have an `@inheritdoc`
5253
#[builder(default = true)]
@@ -70,7 +71,7 @@ pub struct ValidationOptions {
7071
#[builder(default = false)]
7172
pub enum_params: bool,
7273

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.
7475
#[builder(default = vec![])]
7576
pub enforce: Vec<ItemType>,
7677
}
@@ -198,6 +199,7 @@ impl PartialEq for Definition {
198199

199200
impl Definition {
200201
/// Validate a definition and generate [`Diagnostic`]s for errors
202+
#[must_use]
201203
pub fn validate(&self, options: &ValidationOptions) -> ItemDiagnostics {
202204
match self {
203205
// if there was an error while parsing the NatSpec comments, a special diagnostic is generated
@@ -230,6 +232,7 @@ impl Definition {
230232
}
231233

232234
/// Retrieve the inner constructor definition
235+
#[must_use]
233236
pub fn as_constructor(self) -> Option<ConstructorDefinition> {
234237
match self {
235238
Definition::Constructor(def) => Some(def),
@@ -238,6 +241,7 @@ impl Definition {
238241
}
239242

240243
/// Retrieve the inner enum definition
244+
#[must_use]
241245
pub fn as_enum(self) -> Option<EnumDefinition> {
242246
match self {
243247
Definition::Enumeration(def) => Some(def),
@@ -246,6 +250,7 @@ impl Definition {
246250
}
247251

248252
/// Retrieve the inner error definition
253+
#[must_use]
249254
pub fn as_error(self) -> Option<ErrorDefinition> {
250255
match self {
251256
Definition::Error(def) => Some(def),
@@ -254,6 +259,7 @@ impl Definition {
254259
}
255260

256261
/// Retrieve the inner event definition
262+
#[must_use]
257263
pub fn as_event(self) -> Option<EventDefinition> {
258264
match self {
259265
Definition::Event(def) => Some(def),
@@ -262,6 +268,7 @@ impl Definition {
262268
}
263269

264270
/// Retrieve the inner function definition
271+
#[must_use]
265272
pub fn as_function(self) -> Option<FunctionDefinition> {
266273
match self {
267274
Definition::Function(def) => Some(def),
@@ -270,6 +277,7 @@ impl Definition {
270277
}
271278

272279
/// Retrieve the inner modifier definition
280+
#[must_use]
273281
pub fn as_modifier(self) -> Option<ModifierDefinition> {
274282
match self {
275283
Definition::Modifier(def) => Some(def),
@@ -278,6 +286,7 @@ impl Definition {
278286
}
279287

280288
/// Retrieve the inner struct definition
289+
#[must_use]
281290
pub fn as_struct(self) -> Option<StructDefinition> {
282291
match self {
283292
Definition::Struct(def) => Some(def),
@@ -286,6 +295,7 @@ impl Definition {
286295
}
287296

288297
/// Retrieve the inner variable declaration
298+
#[must_use]
289299
pub fn as_variable(self) -> Option<VariableDeclaration> {
290300
match self {
291301
Definition::Variable(def) => Some(def),
@@ -348,7 +358,8 @@ pub fn find_items(cursor: Cursor) -> Vec<Definition> {
348358
/// Extract parameters from a function-like source item.
349359
///
350360
/// 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> {
352363
let mut cursor = cursor.spawn();
353364
let mut out = Vec::new();
354365
while cursor.go_to_next_nonterminal_with_kind(kind) {
@@ -376,8 +387,8 @@ pub fn extract_params(cursor: Cursor, kind: NonterminalKind) -> Vec<Identifier>
376387
out
377388
}
378389

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>> {
381392
let mut cursor = cursor.spawn();
382393
let mut items = Vec::new();
383394
while cursor.go_to_next() {
@@ -445,7 +456,8 @@ pub fn extract_comment(cursor: Cursor, returns: &[Identifier]) -> Result<Option<
445456
}
446457

447458
/// 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> {
449461
let mut cursor = cursor.spawn().with_edges();
450462
let mut out = Vec::new();
451463
while cursor.go_to_next_terminal_with_kind(TerminalKind::Identifier) {
@@ -457,13 +469,14 @@ pub fn extract_identifiers(cursor: Cursor) -> Vec<Identifier> {
457469
out.push(Identifier {
458470
name: Some(cursor.node().unparse().trim().to_string()),
459471
span: cursor.text_range(),
460-
})
472+
});
461473
}
462474
out
463475
}
464476

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
466478
/// diagnostic for each missing one or if there are more than 1 entry per param.
479+
#[must_use]
467480
pub fn check_params(natspec: &NatSpec, params: &[Identifier]) -> Vec<Diagnostic> {
468481
let mut res = Vec::new();
469482
for param in params {
@@ -472,22 +485,24 @@ pub fn check_params(natspec: &NatSpec, params: &[Identifier]) -> Vec<Diagnostic>
472485
continue;
473486
};
474487
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"),
477490
_ => {
478491
continue;
479492
}
480493
};
481494
res.push(Diagnostic {
482495
span: param.span.clone(),
483496
message,
484-
})
497+
});
485498
}
486499
res
487500
}
488501

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
490503
/// diagnostic for each missing one or if there are more than 1 entry per param.
504+
#[allow(clippy::cast_possible_wrap)]
505+
#[must_use]
491506
pub fn check_returns(
492507
natspec: &NatSpec,
493508
returns: &[Identifier],
@@ -497,43 +512,39 @@ pub fn check_returns(
497512
let mut unnamed_returns = 0;
498513
let returns_count = natspec.count_all_returns() as isize;
499514
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"),
504519
_ => {
505520
continue;
506521
}
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;
515529
}
516530
};
517531
res.push(Diagnostic {
518532
span: ret.span.clone(),
519533
message,
520-
})
534+
});
521535
}
522536
if natspec.count_unnamed_returns() > unnamed_returns {
523537
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),
529539
message: "too many unnamed returns".to_string(),
530-
})
540+
});
531541
}
532542
res
533543
}
534544

535545
/// 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 {
537548
let mut cursor = cursor.spawn();
538549
let mut out = Attributes::default();
539550
while cursor.go_to_next_terminal_with_kinds(&[
@@ -561,6 +572,7 @@ pub fn extract_attributes(cursor: Cursor) -> Attributes {
561572
}
562573

563574
/// Find the parent's name (contract, interface, library), if any
575+
#[must_use]
564576
pub fn extract_parent_name(mut cursor: Cursor) -> Option<Parent> {
565577
while cursor.go_to_parent() {
566578
if let Some(parent) = cursor.node().as_nonterminal_with_kinds(&[
@@ -647,7 +659,7 @@ mod tests {
647659
kind: NatSpecKind::Inheritdoc {
648660
parent: "IParserTest".to_string()
649661
},
650-
comment: "".to_string()
662+
comment: String::new()
651663
},
652664
NatSpecItem {
653665
kind: NatSpecKind::Dev,
@@ -672,7 +684,7 @@ mod tests {
672684
kind: NatSpecKind::Inheritdoc {
673685
parent: "IParserTest".to_string()
674686
},
675-
comment: "".to_string()
687+
comment: String::new()
676688
},]
677689
);
678690
}
@@ -692,7 +704,7 @@ mod tests {
692704
kind: NatSpecKind::Inheritdoc {
693705
parent: "IParserTest".to_string()
694706
},
695-
comment: "".to_string()
707+
comment: String::new()
696708
},]
697709
);
698710
}
@@ -993,7 +1005,7 @@ mod tests {
9931005
kind: NatSpecKind::Inheritdoc {
9941006
parent: "IParserTest".to_string()
9951007
},
996-
comment: "".to_string()
1008+
comment: String::new()
9971009
},
9981010
NatSpecItem {
9991011
kind: NatSpecKind::Dev,
@@ -1145,11 +1157,11 @@ mod tests {
11451157
},
11461158
NatSpecItem {
11471159
kind: NatSpecKind::Return { name: None },
1148-
comment: "".to_string()
1160+
comment: String::new()
11491161
},
11501162
NatSpecItem {
11511163
kind: NatSpecKind::Return { name: None },
1152-
comment: "".to_string()
1164+
comment: String::new()
11531165
},
11541166
]
11551167
);

0 commit comments

Comments
 (0)