Skip to content

Commit 77b4e44

Browse files
committed
Harden rustc and Clippy lints even more
1 parent d709482 commit 77b4e44

16 files changed

+457
-225
lines changed

.clippy.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Project configuration for Clippy Rust code linter.
2+
# See full lints list at:
3+
# https://rust-lang.github.io/rust-clippy/master/index.html
4+
5+
standard-macro-braces = [
6+
{ name = "format", brace = "(" },
7+
{ name = "format_ident", brace = "(" },
8+
{ name = "quote", brace = "{" },
9+
{ name = "vec", brace = "[" },
10+
]

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ cargo.fmt:
6767
# make cargo.lint
6868

6969
cargo.lint:
70-
cargo clippy --workspace -- -D clippy::pedantic -D warnings
70+
cargo clippy --workspace -- -D warnings
7171

7272

7373

codegen/src/attribute.rs

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,11 @@ impl Step {
6767
match arg_marked_as_step.len() {
6868
0 => Ok(None),
6969
1 => {
70-
// Unwrapping is OK here, because
71-
// `arg_marked_as_step.len() == 1`.
72-
let (ident, _) =
73-
parse_fn_arg(arg_marked_as_step.first().unwrap())?;
70+
let (ident, _) = parse_fn_arg(arg_marked_as_step[0])?;
7471
Ok(Some(ident.clone()))
7572
}
7673
_ => Err(syn::Error::new(
77-
// Unwrapping is OK here, because
78-
// `arg_marked_as_step.len() > 1`.
79-
arg_marked_as_step.get(1).unwrap().span(),
74+
arg_marked_as_step[1].span(),
8075
"Only 1 step argument is allowed",
8176
)),
8277
}
@@ -255,21 +250,23 @@ impl Step {
255250
::std::borrow::Borrow::borrow(&__cucumber_ctx.step);
256251
}
257252
} else {
258-
let ty = match ty {
259-
syn::Type::Path(p) => p,
260-
_ => {
261-
return Err(syn::Error::new(
262-
ty.span(),
263-
"Type path expected",
264-
))
265-
}
253+
let ty = if let syn::Type::Path(p) = ty {
254+
p
255+
} else {
256+
return Err(syn::Error::new(ty.span(), "Type path expected"));
266257
};
267258

268259
let not_found_err = format!("{} not found", ident);
269260
let parsing_err = format!(
270261
"{} can not be parsed to {}",
271262
ident,
272-
ty.path.segments.last().unwrap().ident
263+
ty.path
264+
.segments
265+
.last()
266+
.ok_or_else(|| {
267+
syn::Error::new(ty.path.span(), "Type path expected")
268+
})?
269+
.ident,
273270
);
274271

275272
quote! {
@@ -349,15 +346,13 @@ impl Parse for AttributeArgument {
349346
},
350347
)?);
351348

352-
Ok(AttributeArgument::Regex(str_lit))
349+
Ok(Self::Regex(str_lit))
353350
} else {
354351
Err(syn::Error::new(arg.span(), "Expected regex argument"))
355352
}
356353
}
357354

358-
syn::NestedMeta::Lit(l) => {
359-
Ok(AttributeArgument::Literal(to_string_literal(l)?))
360-
}
355+
syn::NestedMeta::Lit(l) => Ok(Self::Literal(to_string_literal(l)?)),
361356

362357
syn::NestedMeta::Meta(_) => Err(syn::Error::new(
363358
arg.span(),
@@ -391,13 +386,11 @@ fn remove_all_attrs_if_needed<'a>(
391386
.iter_mut()
392387
.filter_map(|arg| {
393388
if has_other_step_arguments {
394-
if let Some(attr) = find_attr(attr_arg, arg) {
395-
return Some((&*arg, attr));
396-
}
397-
} else if let Some(attr) = remove_attr(attr_arg, arg) {
398-
return Some((&*arg, attr));
389+
find_attr(attr_arg, arg)
390+
} else {
391+
remove_attr(attr_arg, arg)
399392
}
400-
None
393+
.map(move |attr| (&*arg, attr))
401394
})
402395
.unzip()
403396
}
@@ -439,8 +432,7 @@ fn remove_attr(attr_arg: &str, arg: &mut syn::FnArg) -> Option<syn::Attribute> {
439432

440433
if removed.len() == 1 {
441434
typed_arg.attrs = other;
442-
// Unwrapping is OK here, because `step_idents.len() == 1`.
443-
return Some(removed.pop().unwrap());
435+
return removed.pop();
444436
}
445437
other.append(&mut removed);
446438
typed_arg.attrs = other;
@@ -462,9 +454,10 @@ fn parse_fn_arg(arg: &syn::FnArg) -> syn::Result<(&syn::Ident, &syn::Type)> {
462454
}
463455
};
464456

465-
let ident = match arg.pat.as_ref() {
466-
syn::Pat::Ident(i) => &i.ident,
467-
_ => return Err(syn::Error::new(arg.span(), "Expected ident")),
457+
let ident = if let syn::Pat::Ident(i) = arg.pat.as_ref() {
458+
&i.ident
459+
} else {
460+
return Err(syn::Error::new(arg.span(), "Expected ident"));
468461
};
469462

470463
Ok((ident, arg.ty.as_ref()))
@@ -478,18 +471,23 @@ fn find_first_slice(sig: &syn::Signature) -> Option<&syn::TypePath> {
478471
syn::FnArg::Receiver(_) => None,
479472
}
480473
.and_then(|typed_arg| {
481-
match typed_arg.ty.as_ref() {
482-
syn::Type::Reference(r) => Some(r),
483-
_ => None,
474+
if let syn::Type::Reference(r) = typed_arg.ty.as_ref() {
475+
Some(r)
476+
} else {
477+
None
484478
}
485479
.and_then(|ty_ref| {
486-
match ty_ref.elem.as_ref() {
487-
syn::Type::Slice(s) => Some(s),
488-
_ => None,
480+
if let syn::Type::Slice(s) = ty_ref.elem.as_ref() {
481+
Some(s)
482+
} else {
483+
None
489484
}
490-
.and_then(|slice| match slice.elem.as_ref() {
491-
syn::Type::Path(ty) => Some(ty),
492-
_ => None,
485+
.and_then(|slice| {
486+
if let syn::Type::Path(ty) = slice.elem.as_ref() {
487+
Some(ty)
488+
} else {
489+
None
490+
}
493491
})
494492
})
495493
})
@@ -505,17 +503,23 @@ fn parse_world_from_args(sig: &syn::Signature) -> syn::Result<&syn::TypePath> {
505503
syn::FnArg::Typed(a) => Ok(a),
506504
syn::FnArg::Receiver(_) => Err(first_arg.span()),
507505
})
508-
.and_then(|typed_arg| match typed_arg.ty.as_ref() {
509-
syn::Type::Reference(r) => Ok(r),
510-
_ => Err(typed_arg.span()),
506+
.and_then(|typed_arg| {
507+
if let syn::Type::Reference(r) = typed_arg.ty.as_ref() {
508+
Ok(r)
509+
} else {
510+
Err(typed_arg.span())
511+
}
511512
})
512513
.and_then(|world_ref| match world_ref.mutability {
513514
Some(_) => Ok(world_ref),
514515
None => Err(world_ref.span()),
515516
})
516-
.and_then(|world_mut_ref| match world_mut_ref.elem.as_ref() {
517-
syn::Type::Path(p) => Ok(p),
518-
_ => Err(world_mut_ref.span()),
517+
.and_then(|world_mut_ref| {
518+
if let syn::Type::Path(p) = world_mut_ref.elem.as_ref() {
519+
Ok(p)
520+
} else {
521+
Err(world_mut_ref.span())
522+
}
519523
})
520524
.map_err(|span| {
521525
syn::Error::new(
@@ -530,8 +534,9 @@ fn parse_world_from_args(sig: &syn::Signature) -> syn::Result<&syn::TypePath> {
530534
/// [`syn::Lit`]: enum@syn::Lit
531535
/// [`syn::LitStr`]: struct@syn::LitStr
532536
fn to_string_literal(l: syn::Lit) -> syn::Result<syn::LitStr> {
533-
match l {
534-
syn::Lit::Str(str) => Ok(str),
535-
_ => Err(syn::Error::new(l.span(), "Expected string literal")),
537+
if let syn::Lit::Str(str) = l {
538+
Ok(str)
539+
} else {
540+
Err(syn::Error::new(l.span(), "Expected string literal"))
536541
}
537542
}

codegen/src/lib.rs

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
)]
1515
#![doc = include_str!("../README.md")]
1616
#![deny(
17+
macro_use_extern_crate,
1718
nonstandard_style,
1819
rust_2018_idioms,
1920
rustdoc::broken_intra_doc_links,
@@ -23,15 +24,73 @@
2324
)]
2425
#![forbid(non_ascii_idents, unsafe_code)]
2526
#![warn(
26-
deprecated_in_future,
27+
clippy::as_conversions,
28+
clippy::branches_sharing_code,
29+
clippy::clone_on_ref_ptr,
30+
clippy::dbg_macro,
31+
clippy::debug_assert_with_mut_call,
32+
clippy::decimal_literal_representation,
33+
clippy::else_if_without_else,
34+
clippy::empty_line_after_outer_attr,
35+
clippy::exit,
36+
clippy::expect_used,
37+
clippy::fallible_impl_from,
38+
clippy::float_cmp_const,
39+
clippy::fn_to_numeric_cast,
40+
clippy::get_unwrap,
41+
clippy::if_then_some_else_none,
42+
clippy::imprecise_flops,
43+
clippy::let_underscore_must_use,
44+
clippy::lossy_float_literal,
45+
clippy::map_err_ignore,
46+
clippy::mem_forget,
47+
clippy::missing_const_for_fn,
48+
clippy::missing_docs_in_private_items,
49+
clippy::multiple_inherent_impl,
50+
clippy::mutex_integer,
51+
clippy::nonstandard_macro_braces,
52+
clippy::option_if_let_else,
53+
clippy::panic_in_result_fn,
54+
clippy::pedantic,
55+
clippy::print_stderr,
56+
clippy::print_stdout,
57+
clippy::rc_buffer,
58+
clippy::rc_mutex,
59+
clippy::rest_pat_in_fully_bound_structs,
60+
clippy::shadow_unrelated,
61+
clippy::str_to_string,
62+
clippy::string_add,
63+
clippy::string_lit_as_bytes,
64+
clippy::string_to_string,
65+
clippy::suboptimal_flops,
66+
clippy::suspicious_operation_groupings,
67+
clippy::todo,
68+
clippy::trivial_regex,
69+
clippy::unimplemented,
70+
clippy::unnecessary_self_imports,
71+
clippy::unneeded_field_pattern,
72+
clippy::unwrap_in_result,
73+
clippy::unwrap_used,
74+
clippy::use_debug,
75+
clippy::use_self,
76+
clippy::useless_let_if_seq,
77+
clippy::wildcard_enum_match_arm,
78+
future_incompatible,
79+
meta_variable_misuse,
2780
missing_copy_implementations,
2881
missing_debug_implementations,
2982
missing_docs,
83+
noop_method_call,
84+
semicolon_in_expressions_from_macros,
3085
unreachable_pub,
86+
unused_crate_dependencies,
87+
unused_extern_crates,
3188
unused_import_braces,
3289
unused_labels,
90+
unused_lifetimes,
3391
unused_qualifications,
34-
unused_results
92+
unused_results,
93+
variant_size_differences
3594
)]
3695

3796
mod attribute;

src/cucumber.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,23 @@ use crate::{
4949
/// [`WorldInit::cucumber()`]: crate::WorldInit::cucumber()
5050
/// [`WorldInit::run()`]: crate::WorldInit::run()
5151
pub struct Cucumber<W, P, I, R, Wr> {
52+
/// [`Parser`] sourcing [`Feature`]s for execution.
53+
///
54+
/// [`Feature`]: gherkin::Feature
5255
parser: P,
56+
57+
/// [`Runner`] executing [`Scenario`]s and producing [`event`]s.
58+
///
59+
/// [`Scenario`]: gherkin::Scenario
5360
runner: R,
61+
62+
/// [`Writer`] outputting [`event`]s to some output.
5463
writer: Wr,
64+
65+
/// Type of the [`World`] this [`Cucumber`] run on.
5566
_world: PhantomData<W>,
67+
68+
/// Type of the input consumed by [`Cucumber::parser`].
5669
_parser_input: PhantomData<I>,
5770
}
5871

@@ -62,7 +75,7 @@ impl<W> Cucumber<W, (), (), (), ()> {
6275
/// Use [`Cucumber::with_parser()`], [`Cucumber::with_runner()`] and
6376
/// [`Cucumber::with_writer()`] to be able to [`Cucumber::run()`] it.
6477
#[must_use]
65-
pub fn custom() -> Self {
78+
pub const fn custom() -> Self {
6679
Self {
6780
parser: (),
6881
runner: (),

src/event.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<World> Clone for Cucumber<World> {
5151
fn clone(&self) -> Self {
5252
match self {
5353
Self::Started => Self::Started,
54-
Self::Feature(f, ev) => Self::Feature(f.clone(), ev.clone()),
54+
Self::Feature(f, ev) => Self::Feature(Arc::clone(f), ev.clone()),
5555
Self::Finished => Self::Finished,
5656
}
5757
}
@@ -144,8 +144,8 @@ impl<World> Clone for Feature<World> {
144144
fn clone(&self) -> Self {
145145
match self {
146146
Self::Started => Self::Started,
147-
Self::Rule(r, ev) => Self::Rule(r.clone(), ev.clone()),
148-
Self::Scenario(sc, ev) => Self::Scenario(sc.clone(), ev.clone()),
147+
Self::Rule(r, ev) => Self::Rule(Arc::clone(r), ev.clone()),
148+
Self::Scenario(s, ev) => Self::Scenario(Arc::clone(s), ev.clone()),
149149
Self::Finished => Self::Finished,
150150
}
151151
}
@@ -176,7 +176,7 @@ impl<World> Clone for Rule<World> {
176176
fn clone(&self) -> Self {
177177
match self {
178178
Self::Started => Self::Started,
179-
Self::Scenario(sc, ev) => Self::Scenario(sc.clone(), ev.clone()),
179+
Self::Scenario(s, ev) => Self::Scenario(Arc::clone(s), ev.clone()),
180180
Self::Finished => Self::Finished,
181181
}
182182
}
@@ -299,7 +299,7 @@ impl<World> Clone for Hook<World> {
299299
match self {
300300
Self::Started => Self::Started,
301301
Self::Passed => Self::Passed,
302-
Self::Failed(w, i) => Self::Failed(w.clone(), i.clone()),
302+
Self::Failed(w, i) => Self::Failed(w.clone(), Arc::clone(i)),
303303
}
304304
}
305305
}
@@ -339,9 +339,9 @@ impl<World> Clone for Scenario<World> {
339339
Self::Started => Self::Started,
340340
Self::Hook(ty, ev) => Self::Hook(*ty, ev.clone()),
341341
Self::Background(bg, ev) => {
342-
Self::Background(bg.clone(), ev.clone())
342+
Self::Background(Arc::clone(bg), ev.clone())
343343
}
344-
Self::Step(st, ev) => Self::Step(st.clone(), ev.clone()),
344+
Self::Step(st, ev) => Self::Step(Arc::clone(st), ev.clone()),
345345
Self::Finished => Self::Finished,
346346
}
347347
}
@@ -352,15 +352,15 @@ impl<World> Scenario<World> {
352352
///
353353
/// [`Scenario`]: gherkin::Scenario
354354
#[must_use]
355-
pub fn hook_started(which: HookType) -> Self {
355+
pub const fn hook_started(which: HookType) -> Self {
356356
Self::Hook(which, Hook::Started)
357357
}
358358

359359
/// Constructs an event of a passed [`Scenario`] hook.
360360
///
361361
/// [`Scenario`]: gherkin::Scenario
362362
#[must_use]
363-
pub fn hook_passed(which: HookType) -> Self {
363+
pub const fn hook_passed(which: HookType) -> Self {
364364
Self::Hook(which, Hook::Passed)
365365
}
366366

0 commit comments

Comments
 (0)