Skip to content

Commit 0868683

Browse files
authored
Finish setting up rustc/Clippy lints and fix those (#695)
Part of #155 Commit messages have a bit more context on the relevant changes. I've decided to drop very few lints from the EDR list like deny-by-default ones, `inconsistent_struct_constructor` (not that useful) or `implicit_clone` (a lot of false-positives/unidiomatic suggestions; let's wait for Rust 1.74 and rust-lang/rust-clippy#11281 to include it again) or irrelevant ones for us like `suboptimal_flops`.
1 parent 7a3c1fb commit 0868683

File tree

101 files changed

+1248
-1129
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+1248
-1129
lines changed

.cargo/config.toml

+20-5
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,24 @@ lto = true
1010
# https://blog.rust-lang.org/2023/11/16/Rust-1.74.0.html#lint-configuration-through-cargo
1111
rustflags = [
1212
# rustc additional warnings:
13-
"--warn",
14-
"unused_crate_dependencies",
15-
# clippy additional warnings:
16-
"--warn",
17-
"clippy::wildcard_imports",
13+
"-Wunused_crate_dependencies",
14+
# Rust 2018 idioms that are not yet warn-by-default:
15+
"-Welided_lifetimes_in_paths",
16+
"-Wunused_extern_crates",
17+
"-Wexplicit_outlives_requirements",
18+
# 📎 Lints that are enabled (warn/deny) by default
19+
"-Wclippy::all",
20+
# Restriction (optional, neutral lints)
21+
"-Wclippy::exit", # Prefer not `process::exit`ing directly
22+
"-Wclippy::rest_pat_in_fully_bound_structs", # Prefer not to use `..` in fully bound structs
23+
"-Wclippy::verbose_file_reads", # Prefer simpler and more concise `fs::read_to_string`
24+
# Pedantic
25+
"-Wclippy::pedantic", # Warn about pedantic lints, except...
26+
"-Aclippy::implicit_clone", # A lot of false positives, tuned down in Clippy bundled with Rust 1.73
27+
"-Aclippy::match_same_arms", # It's often clearer to have the same arm twice
28+
"-Aclippy::missing_errors_doc", # Most of our code is internal; let's not clutter the docs until...
29+
"-Aclippy::missing_panics_doc", # ... we care about the public documentation in our shipped crates
30+
"-Aclippy::module_name_repetitions", # It seems we prefer it this way; we'd need to discuss that
31+
"-Aclippy::must_use_candidate", # Overzealous, we'd have to `[must_use]` a lot of things
32+
"-Aclippy::redundant_closure_for_method_calls", # Not always clearer, let's not pepper `allow`s whenever needed
1833
]

crates/codegen/ebnf/src/serialization.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,15 @@ impl EbnfSerializer {
8787
}
8888

8989
/// Naive version of formatting for long EBNF statements.
90-
/// Tries to break long lines, which are usually choices of references (PrecedenceParser) or keywords (Scanner).
90+
/// Tries to break long lines, which are usually choices of references
91+
/// ([`PrecedenceParser`](ProductionDefinition::PrecedenceParser)) or keywords ([`Scanner`](ProductionDefinition::Scanner)).
9192
/// Otherwise, prints everything on a single line.
9293
fn serialize_root_node(&mut self, name: &str, root_node: &EbnfNode) -> String {
93-
let choices = match &root_node {
94-
EbnfNode::Choice { nodes } => nodes,
95-
_ => {
96-
// Not a choice: Just flush everything on a single line:
97-
let mut buffer = String::new();
98-
self.serialize_node(root_node, &mut buffer);
99-
return buffer;
100-
}
94+
let EbnfNode::Choice { nodes: choices } = &root_node else {
95+
// Not a choice: Just flush everything on a single line:
96+
let mut buffer = String::new();
97+
self.serialize_node(root_node, &mut buffer);
98+
return buffer;
10199
};
102100

103101
let choices = choices
@@ -202,13 +200,10 @@ impl EbnfSerializer {
202200
fn display_name(&self, name: &str) -> String {
203201
let mut name = name.to_owned();
204202

205-
let production = match self.language.productions.get(&name) {
206-
Some(production) => production,
207-
None => {
208-
// Not a top-level production, so it is an named parser.
209-
// Therefore, it is neither inlined nor a scanner. Return name as-is:
210-
return name;
211-
}
203+
let Some(production) = self.language.productions.get(&name) else {
204+
// Not a top-level production, so it is an named parser.
205+
// Therefore, it is neither inlined nor a scanner. Return name as-is:
206+
return name;
212207
};
213208

214209
if matches!(production.definition, ProductionDefinition::Scanner { .. }) {

crates/codegen/grammar/src/grammar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl Visitable for GrammarElement {
8484
Self::TriviaParserDefinition(trivia_parser) => trivia_parser.accept_visitor(visitor),
8585
Self::ParserDefinition(parser) => parser.accept_visitor(visitor),
8686
Self::PrecedenceParserDefinition(precedence_parser) => {
87-
precedence_parser.accept_visitor(visitor)
87+
precedence_parser.accept_visitor(visitor);
8888
}
8989
}
9090
}

crates/codegen/language/definition/src/compiler/analysis/definitions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ fn calculate_defined_in(analysis: &mut Analysis, item: &SpannedItem) -> VersionS
151151
}
152152
SpannedItem::Token { item } => {
153153
for definition in &item.definitions {
154-
try_add_specifier(&definition.enabled)
154+
try_add_specifier(&definition.enabled);
155155
}
156156
}
157157
SpannedItem::Fragment { item } => {

crates/codegen/language/definition/src/compiler/analysis/reachability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn check_unreachabable_items(analysis: &mut Analysis) {
3333
collect_trivia(&language.leading_trivia, &mut queue);
3434
collect_trivia(&language.trailing_trivia, &mut queue);
3535

36-
let mut visited = queue.iter().cloned().collect::<HashSet<_>>();
36+
let mut visited = queue.iter().copied().collect::<HashSet<_>>();
3737

3838
while let Some(name) = queue.pop() {
3939
for referenced_item in &analysis.metadata[name].referenced_items {

crates/codegen/language/definition/src/compiler/analysis/references.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use crate::{
44
model::{
55
Identifier, SpannedEnumItem, SpannedEnumVariant, SpannedField, SpannedFragmentItem,
66
SpannedItem,
7-
SpannedItemDiscriminants::{self, *},
7+
SpannedItemDiscriminants::{
8+
self, Enum, Fragment, Keyword, Precedence, Repeated, Separated, Struct, Token, Trivia,
9+
},
810
SpannedKeywordDefinition, SpannedKeywordItem, SpannedPrecedenceExpression,
911
SpannedPrecedenceItem, SpannedPrecedenceOperator, SpannedPrimaryExpression,
1012
SpannedRepeatedItem, SpannedScanner, SpannedSeparatedItem, SpannedStructItem,
@@ -363,14 +365,11 @@ fn check_reference(
363365
enablement: &VersionSet,
364366
expected_kinds: &[SpannedItemDiscriminants],
365367
) {
366-
let target = match analysis.metadata.get_mut(&**reference) {
367-
Some(target) => target,
368-
None => {
369-
analysis
370-
.errors
371-
.add(reference, &Errors::UnknownReference(reference));
372-
return;
373-
}
368+
let Some(target) = analysis.metadata.get_mut(&**reference) else {
369+
analysis
370+
.errors
371+
.add(reference, &Errors::UnknownReference(reference));
372+
return;
374373
};
375374

376375
let not_defined_in = enablement.difference(&target.defined_in);

crates/codegen/language/definition/src/internals/errors.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt::Display;
33

44
pub type Result<T> = std::result::Result<T, Error>;
55

6-
/// Our own proxy for [syn::Error] since the latter does not expose the underlying sub-errors.
6+
/// Our own proxy for [`syn::Error`] since the latter does not expose the underlying sub-errors.
77
#[derive(Debug)]
88
pub struct Error {
99
message: String,
@@ -18,7 +18,7 @@ impl Error {
1818
})
1919
}
2020

21-
pub fn from_syn(error: syn::Error) -> Self {
21+
pub fn from_syn(error: &syn::Error) -> Self {
2222
Self {
2323
message: error.to_string(),
2424
span: error.span(),
@@ -34,6 +34,18 @@ impl Error {
3434
}
3535
}
3636

37+
impl From<syn::Error> for Error {
38+
fn from(error: syn::Error) -> Self {
39+
Self::from_syn(&error)
40+
}
41+
}
42+
43+
impl From<Error> for syn::Error {
44+
fn from(error: Error) -> Self {
45+
Error::to_syn(&error)
46+
}
47+
}
48+
3749
#[derive(Debug)]
3850
pub struct ErrorsCollection {
3951
errors: Vec<Error>,
@@ -60,11 +72,7 @@ impl ErrorsCollection {
6072
}
6173

6274
pub fn to_compile_errors(&self) -> TokenStream {
63-
return self
64-
.errors
65-
.iter()
66-
.map(|error| error.to_compile_error())
67-
.collect();
75+
self.errors.iter().map(Error::to_compile_error).collect()
6876
}
6977
}
7078

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
internals::{Error, ErrorsCollection, ParseInputTokens, Result},
2+
internals::{ErrorsCollection, ParseInputTokens, Result},
33
model::SpannedLanguage,
44
};
55
use proc_macro2::TokenStream;
@@ -9,7 +9,7 @@ pub(crate) struct ParseAdapter;
99

1010
impl ParseAdapter {
1111
pub fn parse(input: TokenStream) -> Result<ParseOutput> {
12-
syn::parse2(input).map_err(Error::from_syn)
12+
Ok(syn::parse2(input)?)
1313
}
1414
}
1515

@@ -18,14 +18,13 @@ pub(crate) struct ParseOutput {
1818
pub errors: ErrorsCollection,
1919
}
2020

21-
/// A wrapper around [syn::parse::Parse] to convert to/from our own error types.
21+
/// A wrapper around [`syn::parse::Parse`] to convert to/from our own error types.
2222
impl Parse for ParseOutput {
23-
fn parse(input: ParseStream) -> syn::Result<Self> {
23+
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
2424
let mut errors = ErrorsCollection::new();
2525

26-
match SpannedLanguage::parse_named_value(input, &mut errors) {
27-
Ok(language) => Ok(Self { language, errors }),
28-
Err(error) => Err(error.to_syn()),
29-
}
26+
let language = SpannedLanguage::parse_named_value(input, &mut errors)?;
27+
28+
Ok(Self { language, errors })
3029
}
3130
}

crates/codegen/language/definition/src/internals/parse_input_tokens/external_types.rs

+22-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::internals::{
2-
parse_input_tokens::ParseHelpers, Error, ErrorsCollection, ParseInputTokens, Result, Spanned,
2+
parse_input_tokens::ParseHelpers, ErrorsCollection, ParseInputTokens, Result, Spanned,
33
};
44
use indexmap::{IndexMap, IndexSet};
55
use proc_macro2::Ident;
@@ -8,23 +8,23 @@ use std::{fmt::Debug, rc::Rc};
88
use syn::{parse::ParseStream, LitBool, LitChar, LitStr};
99

1010
impl ParseInputTokens for bool {
11-
fn parse_value(input: ParseStream, _: &mut ErrorsCollection) -> Result<Self> {
11+
fn parse_value(input: ParseStream<'_>, _: &mut ErrorsCollection) -> Result<Self> {
1212
let literal = ParseHelpers::syn::<LitBool>(input)?;
1313

1414
Ok(literal.value())
1515
}
1616
}
1717

1818
impl<T: ParseInputTokens> ParseInputTokens for Box<T> {
19-
fn parse_value(input: ParseStream, errors: &mut ErrorsCollection) -> Result<Self> {
19+
fn parse_value(input: ParseStream<'_>, errors: &mut ErrorsCollection) -> Result<Self> {
2020
let value = T::parse_value(input, errors)?;
2121

2222
Ok(value.into())
2323
}
2424
}
2525

2626
impl ParseInputTokens for char {
27-
fn parse_value(input: ParseStream, _: &mut ErrorsCollection) -> Result<Self> {
27+
fn parse_value(input: ParseStream<'_>, _: &mut ErrorsCollection) -> Result<Self> {
2828
let literal = ParseHelpers::syn::<LitChar>(input)?;
2929

3030
Ok(literal.value())
@@ -34,14 +34,14 @@ impl ParseInputTokens for char {
3434
impl<K: ParseInputTokens + std::hash::Hash + Eq, V: ParseInputTokens> ParseInputTokens
3535
for IndexMap<K, V>
3636
{
37-
fn parse_value(input: ParseStream, errors: &mut ErrorsCollection) -> Result<Self> {
38-
ParseHelpers::map(input, errors)
37+
fn parse_value(input: ParseStream<'_>, errors: &mut ErrorsCollection) -> Result<Self> {
38+
Ok(ParseHelpers::map(input, errors))
3939
}
4040
}
4141

4242
impl<T: ParseInputTokens + std::hash::Hash + Ord> ParseInputTokens for IndexSet<Spanned<T>> {
43-
fn parse_value(input: ParseStream, errors: &mut ErrorsCollection) -> Result<Self> {
44-
let sequence: Vec<Spanned<T>> = ParseHelpers::sequence(input, errors)?;
43+
fn parse_value(input: ParseStream<'_>, errors: &mut ErrorsCollection) -> Result<Self> {
44+
let sequence: Vec<Spanned<T>> = ParseHelpers::sequence(input, errors);
4545

4646
let mut set = Self::new();
4747

@@ -58,15 +58,19 @@ impl<T: ParseInputTokens + std::hash::Hash + Ord> ParseInputTokens for IndexSet<
5858
}
5959

6060
impl<T: ParseInputTokens> ParseInputTokens for Option<T> {
61-
fn parse_value(input: ParseStream, errors: &mut ErrorsCollection) -> Result<Self> {
61+
fn parse_value(input: ParseStream<'_>, errors: &mut ErrorsCollection) -> Result<Self> {
6262
if input.is_empty() {
6363
Ok(None)
6464
} else {
6565
Ok(Some(T::parse_value(input, errors)?))
6666
}
6767
}
6868

69-
fn parse_field(name: &str, input: ParseStream, errors: &mut ErrorsCollection) -> Result<Self> {
69+
fn parse_field(
70+
name: &str,
71+
input: ParseStream<'_>,
72+
errors: &mut ErrorsCollection,
73+
) -> Result<Self> {
7074
match ParseHelpers::syn::<Ident>(&input.fork()) {
7175
Ok(key) if key == name => Ok(Some(ParseHelpers::field(name, input, errors)?)),
7276
_ => Ok(None),
@@ -75,15 +79,15 @@ impl<T: ParseInputTokens> ParseInputTokens for Option<T> {
7579
}
7680

7781
impl<T: ParseInputTokens> ParseInputTokens for Rc<T> {
78-
fn parse_value(input: ParseStream, errors: &mut ErrorsCollection) -> Result<Self> {
82+
fn parse_value(input: ParseStream<'_>, errors: &mut ErrorsCollection) -> Result<Self> {
7983
let value = T::parse_value(input, errors)?;
8084

8185
Ok(value.into())
8286
}
8387
}
8488

8589
impl ParseInputTokens for String {
86-
fn parse_value(input: ParseStream, errors: &mut ErrorsCollection) -> Result<Self> {
90+
fn parse_value(input: ParseStream<'_>, errors: &mut ErrorsCollection) -> Result<Self> {
8791
let literal = ParseHelpers::syn::<LitStr>(input)?;
8892
let value = literal.value();
8993

@@ -96,21 +100,22 @@ impl ParseInputTokens for String {
96100
}
97101

98102
impl ParseInputTokens for usize {
99-
fn parse_value(input: ParseStream, _: &mut ErrorsCollection) -> Result<Self> {
103+
fn parse_value(input: ParseStream<'_>, _: &mut ErrorsCollection) -> Result<Self> {
100104
let literal = ParseHelpers::syn::<syn::LitInt>(input)?;
105+
let value = literal.base10_parse::<usize>()?;
101106

102-
literal.base10_parse::<usize>().map_err(Error::from_syn)
107+
Ok(value)
103108
}
104109
}
105110

106111
impl<T: ParseInputTokens> ParseInputTokens for Vec<T> {
107-
fn parse_value(input: ParseStream, errors: &mut ErrorsCollection) -> Result<Self> {
108-
ParseHelpers::sequence(input, errors)
112+
fn parse_value(input: ParseStream<'_>, errors: &mut ErrorsCollection) -> Result<Self> {
113+
Ok(ParseHelpers::sequence(input, errors))
109114
}
110115
}
111116

112117
impl ParseInputTokens for Version {
113-
fn parse_value(input: ParseStream, errors: &mut ErrorsCollection) -> Result<Self> {
118+
fn parse_value(input: ParseStream<'_>, errors: &mut ErrorsCollection) -> Result<Self> {
114119
let literal = ParseHelpers::syn::<LitStr>(input)?;
115120

116121
match Self::parse(&literal.value()) {

0 commit comments

Comments
 (0)