Skip to content

Commit ca8e811

Browse files
committed
Fix more clippy lints.
1 parent 80a0a09 commit ca8e811

14 files changed

+61
-46
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
"cSpell.words": [
44
"castable",
55
"casted",
6+
"cfgs",
67
"cleanups",
78
"clippy",
89
"downcastable",
910
"downcasting",
1011
"downcasts",
12+
"extern",
1113
"impls",
1214
"intertrait",
1315
"Kokusnuss",
1416
"mopa",
17+
"nonstandard",
1518
"onestacked",
1619
"Peekable",
1720
"Pointee",

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ option_if_let_else = "allow"
88
# see: https://github.com/bevyengine/bevy/pull/15375#issuecomment-2366966219
99
too_long_first_doc_paragraph = "allow"
1010

11-
nursery = "warn"
12-
pedantic = "warn"
11+
nursery = { priority = -1, level = "warn" }
12+
pedantic = { priority = -1, level = "warn" }
1313
doc_markdown = "warn"
1414
manual_let_else = "warn"
1515
match_same_arms = "warn"
@@ -32,8 +32,8 @@ alloc_instead_of_core = "warn"
3232
nonstandard-style = "warn"
3333
future-incompatible = "warn"
3434
missing_docs = "warn"
35-
unused = "warn"
36-
rust_2018_idioms = "warn"
35+
unused = { priority = -1, level = "warn" }
36+
rust_2018_idioms = { priority = -1, level = "warn" }
3737
rust-2024-compatibility = "warn"
3838
array-into-iter = "warn"
3939
bare-trait-objects = "warn"

trait_cast_impl_rs/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ use syn::{
1212
Error, ItemEnum, ItemStruct, Token, TypePath,
1313
};
1414

15-
/// Parses a list of TypePaths separated by commas.
15+
/// Parses a list of `TypePath`s separated by commas.
1616
struct TraitCastTargets {
17-
vars: Vec<TypePath>,
17+
targets: Vec<TypePath>,
1818
}
1919

2020
impl Parse for TraitCastTargets {
2121
fn parse(input: ParseStream<'_>) -> parse::Result<Self> {
22-
let vars: Vec<TypePath> = Punctuated::<TypePath, Token![,]>::parse_terminated(input)?
22+
let targets: Vec<TypePath> = Punctuated::<TypePath, Token![,]>::parse_terminated(input)?
2323
.into_iter()
2424
.collect();
25-
Ok(TraitCastTargets { vars: vars })
25+
Ok(TraitCastTargets { targets })
2626
}
2727
}
2828

2929
impl quote::ToTokens for TraitCastTargets {
3030
fn to_tokens(&self, tokens: &mut TokenStream2) {
31-
let vars = &self.vars;
31+
let vars = &self.targets;
3232
tokens.extend(quote!(#(#vars),*));
3333
}
3434
}

trait_cast_rs/examples/manual.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! This example demonstrates how to manually implement the `TraitcastableAny` trait for a non generic struct `HybridPet`.
2+
#![allow(clippy::undocumented_unsafe_blocks, clippy::use_self)]
13
#![expect(
24
unsafe_code,
35
reason = "Manual traitcast implementations require unsafe code."
@@ -17,19 +19,19 @@ struct HybridPet {
1719
}
1820
impl TraitcastableTo<dyn Dog> for HybridPet {
1921
const METADATA: ::core::ptr::DynMetadata<dyn Dog> = {
20-
let ptr: *const HybridPet = ::core::ptr::null::<HybridPet>();
21-
let ptr: *const dyn Dog = ptr as _;
22+
let self_ptr: *const HybridPet = ::core::ptr::null::<HybridPet>();
23+
let dyn_ptr: *const dyn Dog = self_ptr as _;
2224

23-
ptr.to_raw_parts().1
25+
dyn_ptr.to_raw_parts().1
2426
};
2527
}
2628

2729
impl TraitcastableTo<dyn Cat> for HybridPet {
2830
const METADATA: ::core::ptr::DynMetadata<dyn Cat> = {
29-
let ptr: *const HybridPet = ::core::ptr::null::<HybridPet>();
30-
let ptr: *const dyn Cat = ptr as _;
31+
let self_ptr: *const Self = ::core::ptr::null::<Self>();
32+
let dyn_ptr: *const dyn Cat = self_ptr as _;
3133

32-
ptr.to_raw_parts().1
34+
dyn_ptr.to_raw_parts().1
3335
};
3436
}
3537

@@ -44,7 +46,7 @@ unsafe impl TraitcastableAny for HybridPet {
4446
}
4547
impl HybridPet {
4648
fn greet(&self) {
47-
println!("{}: Hi", self.name)
49+
println!("{}: Hi", self.name);
4850
}
4951
}
5052

trait_cast_rs/examples/manual_gen_struct.rs renamed to trait_cast_rs/examples/manual_generic_struct.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
//! This example demonstrates how to manually implement the `TraitcastableAny` trait for a generic struct `HybridPet`.
2+
#![allow(clippy::undocumented_unsafe_blocks, clippy::use_self)]
13
#![expect(
24
unsafe_code,
35
reason = "Manual traitcast implementations require unsafe code."
46
)]
57
#![cfg_attr(feature = "min_specialization", feature(min_specialization))]
68
#![feature(ptr_metadata)]
79

8-
use std::{any::type_name, fmt::Display};
10+
use core::{any::type_name, fmt::Display};
911

1012
use trait_cast_rs::{TraitcastTarget, TraitcastableAny, TraitcastableAnyInfra, TraitcastableTo};
1113

@@ -14,7 +16,7 @@ struct HybridPet<T: Display> {
1416
}
1517
impl<T: Display> HybridPet<T> {
1618
fn greet(&self) {
17-
println!("{}: Hi {}", self.name, type_name::<T>())
19+
println!("{}: Hi {}", self.name, type_name::<T>());
1820
}
1921
}
2022

@@ -39,21 +41,21 @@ trait Cat<T: Display + ?Sized> {
3941

4042
impl<T: Display + 'static> TraitcastableTo<dyn Dog> for HybridPet<T> {
4143
const METADATA: ::core::ptr::DynMetadata<dyn Dog> = {
42-
let ptr: *const HybridPet<T> = ::core::ptr::null::<HybridPet<T>>();
43-
let ptr: *const dyn Dog = ptr as _;
44+
let self_ptr: *const HybridPet<T> = ::core::ptr::null::<HybridPet<T>>();
45+
let dyn_ptr: *const dyn Dog = self_ptr as _;
4446

45-
ptr.to_raw_parts().1
47+
dyn_ptr.to_raw_parts().1
4648
};
4749
}
4850

4951
impl<T: Display + 'static, V: Display + 'static + ?Sized> TraitcastableTo<dyn Cat<V>>
5052
for HybridPet<T>
5153
{
5254
const METADATA: ::core::ptr::DynMetadata<dyn Cat<V>> = {
53-
let ptr: *const HybridPet<T> = ::core::ptr::null::<HybridPet<T>>();
54-
let ptr: *const dyn Cat<V> = ptr as _;
55+
let self_ptr: *const HybridPet<T> = ::core::ptr::null::<HybridPet<T>>();
56+
let dyn_ptr: *const dyn Cat<V> = self_ptr as _;
5557

56-
ptr.to_raw_parts().1
58+
dyn_ptr.to_raw_parts().1
5759
};
5860
}
5961

trait_cast_rs/examples/with_decl_macro.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
//! This example demonstrates how to use the `make_trait_castable_decl` macro to declare a type as traitcastable.
12
#![cfg_attr(feature = "min_specialization", feature(min_specialization))]
23
#![cfg_attr(feature = "downcast_unchecked", feature(downcast_unchecked))]
34
#![feature(trait_upcasting)]
45
#![allow(incomplete_features)]
56
#![feature(ptr_metadata)]
67

7-
use std::any::Any;
8+
use core::any::Any;
89

910
use trait_cast_rs::{make_trait_castable_decl, TraitcastableAny, TraitcastableAnyInfra};
1011

@@ -21,7 +22,7 @@ make_trait_castable_decl! {
2122

2223
impl HybridPet {
2324
fn greet(&self) {
24-
println!("{}: Hi", self.name)
25+
println!("{}: Hi", self.name);
2526
}
2627
}
2728

trait_cast_rs/examples/with_decl_macro_gen_struct.rs renamed to trait_cast_rs/examples/with_decl_macro_generic_struct.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
//! This example demonstrates how to implement the `TraitcastableAny` trait for a generic struct `HybridPet` using the `make_trait_castable_decl` macro.
12
#![cfg_attr(feature = "min_specialization", feature(min_specialization))]
23
#![feature(ptr_metadata)]
34

4-
use std::{any::type_name, fmt::Display};
5+
use core::{any::type_name, fmt::Display};
56

67
use trait_cast_rs::{make_trait_castable_decl, TraitcastableAny, TraitcastableAnyInfra};
78

@@ -10,7 +11,7 @@ struct HybridPet<T: Display> {
1011
}
1112
impl<T: Display> HybridPet<T> {
1213
fn greet(&self) {
13-
println!("{}: Hi {}", self.name, type_name::<T>())
14+
println!("{}: Hi {}", self.name, type_name::<T>());
1415
}
1516
}
1617

trait_cast_rs/examples/with_decl_macro_gen.rs renamed to trait_cast_rs/examples/with_decl_macro_struct.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1+
//! This example demonstrates how to implement the `TraitcastableAny` trait for a non generic struct `HybridPet` using the `make_trait_castable_decl` macro.
12
#![cfg_attr(feature = "min_specialization", feature(min_specialization))]
23
#![cfg_attr(feature = "downcast_unchecked", feature(downcast_unchecked))]
34
#![feature(ptr_metadata)]
45

5-
use std::any::type_name;
6+
use core::any::type_name;
67

78
use trait_cast_rs::{make_trait_castable_decl, TraitcastableAny, TraitcastableAnyInfra};
89

910
make_trait_castable_decl! {
10-
HybridPet => (Dog<i32>, Dog<TestStruct<::std::primitive::i32>>, Cat<u128, u32>),
11+
HybridPet => (Dog<i32>, Dog<TestStruct<::core::primitive::i32>>, Cat<u128, u32>),
1112
}
1213
struct HybridPet {
1314
name: String,
1415
}
1516
struct TestStruct<T>(T);
1617
impl HybridPet {
1718
fn greet(&self) {
18-
println!("{}: Hi", self.name)
19+
println!("{}: Hi", self.name);
1920
}
2021
}
2122

trait_cast_rs/examples/with_proc_macro.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! This example shows how to use the `make_trait_castable` macro to make a struct castable to multiple traits.
2+
#![allow(clippy::undocumented_unsafe_blocks)]
13
#![expect(
24
unsafe_code,
35
reason = "The example shows off the unchecked downcast functions which require unsafe code."
@@ -15,7 +17,7 @@ struct HybridPet {
1517
}
1618
impl HybridPet {
1719
fn greet(&self) {
18-
println!("{}: Hi", self.name)
20+
println!("{}: Hi", self.name);
1921
}
2022
}
2123

trait_cast_rs/examples/with_proc_macro_enum.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! This example demonstrates how to use the `make_trait_castable` proc macro with an enum.
12
#![cfg_attr(feature = "min_specialization", feature(min_specialization))]
23
#![cfg_attr(feature = "downcast_unchecked", feature(downcast_unchecked))]
34
#![feature(ptr_metadata)]
@@ -11,20 +12,20 @@ enum HybridPet {
1112
impl HybridPet {
1213
fn greet(&self) {
1314
let Self::Name(name) = self;
14-
println!("{name}: Hi")
15+
println!("{name}: Hi");
1516
}
1617
}
1718

1819
impl Dog for HybridPet {
1920
fn bark(&self) {
2021
let Self::Name(name) = self;
21-
println!("{name}: Woof!")
22+
println!("{name}: Woof!");
2223
}
2324
}
2425
impl Cat for HybridPet {
2526
fn meow(&self) {
2627
let Self::Name(name) = self;
27-
println!("{name}: Meow!")
28+
println!("{name}: Meow!");
2829
}
2930
}
3031

trait_cast_rs/examples/with_proc_macro_gen.rs renamed to trait_cast_rs/examples/with_proc_macro_generic_target.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
//! This example demonstrates how to use the `make_trait_castable` proc macro with a generic traitcast target struct.
12
#![cfg_attr(feature = "min_specialization", feature(min_specialization))]
23
#![cfg_attr(feature = "downcast_unchecked", feature(downcast_unchecked))]
34
#![feature(ptr_metadata)]
45

5-
use std::any::type_name;
6+
use core::any::type_name;
67

78
use trait_cast_rs::{make_trait_castable, TraitcastableAny, TraitcastableAnyInfra};
89

@@ -13,7 +14,7 @@ struct HybridPet {
1314
struct TestStruct<T>(T);
1415
impl HybridPet {
1516
fn greet(&self) {
16-
println!("{}: Hi", self.name)
17+
println!("{}: Hi", self.name);
1718
}
1819
}
1920

trait_cast_rs/examples/with_proc_macro_mut.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! This example demonstrates how to modify a type erased traitcastable object.
12
#![cfg_attr(feature = "min_specialization", feature(min_specialization))]
23
#![cfg_attr(feature = "downcast_unchecked", feature(downcast_unchecked))]
34
#![feature(ptr_metadata)]
@@ -10,7 +11,7 @@ struct HybridPet {
1011
}
1112
impl HybridPet {
1213
fn greet(&self) {
13-
println!("{}: Hi", self.name)
14+
println!("{}: Hi", self.name);
1415
}
1516
}
1617

trait_cast_rs/src/decl_macro.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ macro_rules! make_trait_castable_decl {
1717
$(
1818
impl $crate::TraitcastableTo<dyn $target> for $source {
1919
const METADATA: ::core::ptr::DynMetadata<dyn $target> = {
20-
let ptr: *const $source = ::core::ptr::null::<$source>();
21-
let ptr: *const dyn $target = ptr as _;
20+
let self_ptr: *const $source = ::core::ptr::null::<$source>();
21+
let dyn_ptr: *const dyn $target = self_ptr as _;
2222

23-
ptr.to_raw_parts().1
23+
dyn_ptr.to_raw_parts().1
2424
};
2525
}
2626
)*

trait_cast_rs/src/trait_cast.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub trait TraitcastableTo<Target: 'static + ?Sized>: TraitcastableAny {
2323
pub struct TraitcastTarget {
2424
target_type_id: TypeId,
2525
target_type_name: &'static str,
26-
/// Must point to the DynMetadata<T> (where T is the type in TypeId)
26+
/// Must point to the `DynMetadata<T>` (where T is the type in `TypeId`)
2727
metadata: *const (),
2828
}
2929
impl TraitcastTarget {
@@ -37,7 +37,7 @@ impl TraitcastTarget {
3737
metadata: ptr::from_ref::<DynMetadata<Target>>(&Src::METADATA).cast::<()>(),
3838
}
3939
}
40-
/// Returns the type_id of the type to which can be cast with this instance.
40+
/// Returns the `TypeId` of the type to which can be cast with this instance.
4141
#[must_use]
4242
pub const fn target_type_id(&self) -> TypeId {
4343
self.target_type_id
@@ -50,8 +50,8 @@ impl TraitcastTarget {
5050
/// This should generally not be manually implemented, but generated by the `make_trait_castable` attribute macro.
5151
///
5252
/// # Safety
53-
/// The function `traitcast_targets' must only produce valid TraitcastTarget (That use the metadata associated the the correct source struct).
54-
/// The function `find_traitcast_target` must not return `Some` unless contained value has the correct target TypeId.
53+
/// The function `traitcast_targets` must only produce valid `TraitcastTarget` (That use the metadata associated the the correct source struct).
54+
/// The function `find_traitcast_target` must not return `Some` unless contained value has the correct target `TypeId`.
5555
pub unsafe trait TraitcastableAny: Any {
5656
/// This function returns a list of all the `TraitcastTarget`'s to which a trait object can be cast, this is then used by the implementations of `TraitcastableAnyInfra` to accomplish the traitcast.
5757
/// The function is used to generate debug output for `TraitcastableAny`.
@@ -67,7 +67,7 @@ pub unsafe trait TraitcastableAny: Any {
6767
///
6868
/// Possible strategies:
6969
/// * Unsorted `Vec<TraitcastTarget>` lookup. Hot traits first. - Used by the default implementation.
70-
/// * HashMap
70+
/// * `HashMap`
7171
/// * Temporarily removed without replacement: ~`Vec<TraitcastTarget>` sorted by the `TypeId` and performing a binary search on it - Used if feature `const_sort` is used.~
7272
fn find_traitcast_target(&self, target: TypeId) -> Option<&TraitcastTarget> {
7373
self

0 commit comments

Comments
 (0)