Skip to content

Commit

Permalink
build: add clippy lints (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann authored Dec 28, 2023
1 parent 16821fb commit 5b87d1a
Show file tree
Hide file tree
Showing 194 changed files with 1,997 additions and 1,917 deletions.
79 changes: 79 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,82 @@
gen-syntax = "run --package tools --bin tools -- gen-syntax"
gen-runtime-capi = "run --package tools --bin tools -- gen-runtime-capi"
gen-abi = "run --package tools --bin tools -- gen-abi"

[target.'cfg(all())']
rustflags = [
"-Wclippy::all",
"-Wclippy::await_holding_lock",
"-Wclippy::bool-to-int-with-if",
"-Wclippy::cast_lossless",
"-Wclippy::char_lit_as_u8",
"-Wclippy::checked_conversions",
"-Wclippy::debug_assert_with_mut_call",
"-Wclippy::default_trait_access",
"-Wclippy::doc_markdown",
"-Wclippy::empty_enum",
"-Wclippy::enum_glob_use",
"-Wclippy::expl_impl_clone_on_copy",
"-Wclippy::explicit_deref_methods",
"-Wclippy::explicit_into_iter_loop",
"-Wclippy::fallible_impl_from",
"-Wclippy::filter_map_next",
"-Wclippy::flat_map_option",
"-Wclippy::float_cmp_const",
"-Wclippy::fn_params_excessive_bools",
"-Wclippy::from_iter_instead_of_collect",
"-Wclippy::if-not-else",
"-Wclippy::implicit_clone",
"-Wclippy::imprecise_flops",
"-Wclippy::inconsistent_struct_constructor",
"-Wclippy::inefficient_to_string",
"-Wclippy::invalid_upcast_comparisons",
"-Wclippy::items-after-statements",
"-Wclippy::large_digit_groups",
"-Wclippy::large_stack_arrays",
"-Wclippy::large_types_passed_by_value",
"-Wclippy::let_unit_value",
"-Wclippy::linkedlist",
"-Wclippy::lossy_float_literal",
"-Wclippy::macro_use_imports",
"-Wclippy::manual-assert",
"-Wclippy::manual_ok_or",
"-Wclippy::map_err_ignore",
"-Wclippy::map_flatten",
"-Wclippy::map_unwrap_or",
"-Wclippy::match_on_vec_items",
"-Wclippy::match_same_arms",
"-Wclippy::match_wild_err_arm",
"-Wclippy::match_wildcard_for_single_variants",
"-Wclippy::mem_forget",
"-Wclippy::missing_enforced_import_renames",
"-Wclippy::mut_mut",
"-Wclippy::mutex_integer",
"-Wclippy::needless_borrow",
"-Wclippy::needless_continue",
"-Wclippy::needless_for_each",
"-Wclippy::option_option",
"-Wclippy::path_buf_push_overwrite",
"-Wclippy::ptr_as_ptr",
"-Wclippy::rc_mutex",
"-Wclippy::redundant_closure_for_method_calls",
"-Wclippy::ref_option_ref",
"-Wclippy::rest_pat_in_fully_bound_structs",
"-Wclippy::same_functions_in_if_condition",
"-Wclippy::semicolon_if_nothing_returned",
"-Wclippy::single_match_else",
"-Wclippy::string_add_assign",
"-Wclippy::string_lit_as_bytes",
"-Wclippy::string_to_string",
"-Wclippy::todo",
"-Wclippy::trait_duplication_in_bounds",
"-Wclippy::uninlined_format_args",
"-Wclippy::unnested_or_patterns",
"-Wclippy::unused_self",
"-Wclippy::useless_transmute",
"-Wclippy::verbose_file_reads",
"-Wclippy::wildcard-imports",
"-Wclippy::zero_sized_map_values",
"-Wfuture_incompatible",
"-Wnonstandard_style",
"-Wrust_2018_idioms",
]
4 changes: 2 additions & 2 deletions cpp/include/mun/runtime_capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ typedef MunRawGcPtr MunGcPtr;
/**
* Definition of an external function that is callable from Mun.
*
* The ownership of the contained TypeInfoHandles is considered to lie with this struct.
* The ownership of the contained `TypeInfoHandles` is considered to lie with this struct.
*/
typedef struct MunExternalFunctionDefinition {
/**
Expand Down Expand Up @@ -604,7 +604,7 @@ void mun_string_destroy(const char *string);
*
* # Safety
*
* Only call this function on an ErrorHandle once.
* Only call this function on an [`ErrorHandle`] once.
*/
void mun_error_destroy(struct MunErrorHandle error);

Expand Down
2 changes: 1 addition & 1 deletion crates/mun/src/ops/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn build(args: Args) -> Result<ExitStatus, anyhow::Error> {
)
})?
}
Some(path) => std::fs::canonicalize(Path::new(&path)).map_err(|_| {
Some(path) => std::fs::canonicalize(Path::new(&path)).map_err(|_error| {
anyhow::anyhow!(
"'{}' does not refer to a valid manifest path",
path.display()
Expand Down
5 changes: 3 additions & 2 deletions crates/mun/src/ops/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ version="0.1.0"
/// Shortcut function for creating new directories.
pub fn create_dir(path: impl AsRef<Path>) -> anyhow::Result<()> {
fs::create_dir(&path)
.map_err(|_| anyhow!("failed to create directory `{}`", path.as_ref().display()))
.map_err(|_error| anyhow!("failed to create directory `{}`", path.as_ref().display()))
}

/// Shortcut function for creating new files.
pub fn write(path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> anyhow::Result<()> {
let path = path.as_ref();
fs::write(path, contents.as_ref()).map_err(|_| anyhow!("failed to write `{}`", path.display()))
fs::write(path, contents.as_ref())
.map_err(|_error| anyhow!("failed to write `{}`", path.display()))
}
6 changes: 3 additions & 3 deletions crates/mun/src/ops/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ pub fn start(args: Args) -> anyhow::Result<ExitStatus> {
.invoke(&args.entry, ())
.map_err(|e| anyhow!("{}", e))?;

println!("{result}")
println!("{result}");
} else if return_type.equals::<f64>() {
let result: f64 = runtime
.invoke(&args.entry, ())
.map_err(|e| anyhow!("{}", e))?;

println!("{result}")
println!("{result}");
} else if return_type.equals::<i64>() {
let result: i64 = runtime
.invoke(&args.entry, ())
.map_err(|e| anyhow!("{}", e))?;

println!("{result}")
println!("{result}");
} else if return_type.equals::<()>() {
#[allow(clippy::unit_arg)]
runtime
Expand Down
3 changes: 3 additions & 0 deletions crates/mun/tests/book.rs
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
#![allow(unused_attributes)]

#[allow(clippy::all)]
include!(concat!(env!("OUT_DIR"), "/skeptic-tests.rs"));
2 changes: 1 addition & 1 deletion crates/mun_abi/src/assembly_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ mod tests {

assert_eq!(assembly.dependencies().count(), dependencies.len());
for (lhs, rhs) in assembly.dependencies().zip([FAKE_DEPENDENCY].iter()) {
assert_eq!(lhs, *rhs)
assert_eq!(lhs, *rhs);
}
}
}
4 changes: 2 additions & 2 deletions crates/mun_abi/src/dispatch_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<'a> DispatchTable<'a> {
///
/// This is generally not recommended, use with caution! Calling this method with an
/// out-of-bounds index is _undefined behavior_ even if the resulting reference is not used.
/// For a safe alternative see [get_ptr](#method.get_ptr).
/// For a safe alternative see [`get_ptr`](#method.get_ptr).
///
/// # Safety
///
Expand All @@ -89,7 +89,7 @@ impl<'a> DispatchTable<'a> {
///
/// This is generally not recommended, use with caution! Calling this method with an
/// out-of-bounds index is _undefined behavior_ even if the resulting reference is not used.
/// For a safe alternative see [get_ptr_mut](#method.get_ptr_mut).
/// For a safe alternative see [`get_ptr_mut`](#method.get_ptr_mut).
///
/// # Safety
///
Expand Down
14 changes: 7 additions & 7 deletions crates/mun_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,6 @@ impl Guid {

impl fmt::Display for Guid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let hyphenated = format_hyphenated(&self.0);

// SAFETY: The encoded buffer is ASCII encoded
let hyphenated = unsafe { std::str::from_utf8_unchecked(&hyphenated) };

return f.write_str(hyphenated);

#[inline]
const fn format_hyphenated(src: &[u8; 16]) -> [u8; 36] {
const LUT: [u8; 16] = [
Expand Down Expand Up @@ -99,6 +92,13 @@ impl fmt::Display for Guid {
}
dst
}

let hyphenated = format_hyphenated(&self.0);

// SAFETY: The encoded buffer is ASCII encoded
let hyphenated = unsafe { std::str::from_utf8_unchecked(&hyphenated) };

f.write_str(hyphenated)
}
}

Expand Down
6 changes: 4 additions & 2 deletions crates/mun_abi/src/module_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ mod tests {
use std::{ffi::CString, ptr};

use crate::type_id::HasStaticTypeId;
use crate::StructMemoryKind;
use crate::{
test_utils::{
fake_fn_prototype, fake_module_info, fake_struct_definition, fake_type_definition,
Expand Down Expand Up @@ -121,7 +122,8 @@ mod tests {
let functions = &[fn_info];

let struct_name = CString::new(FAKE_STRUCT_NAME).expect("Invalid fake struct name");
let struct_info = fake_struct_definition(&struct_name, &[], &[], &[], Default::default());
let struct_info =
fake_struct_definition(&struct_name, &[], &[], &[], StructMemoryKind::default());
let type_info =
fake_type_definition(&struct_name, 1, 1, TypeDefinitionData::Struct(struct_info));
let types = [type_info];
Expand All @@ -144,7 +146,7 @@ mod tests {
);
}

let result_types: &[TypeDefinition] = module.types();
let result_types: &[TypeDefinition<'_>] = module.types();
assert_eq!(result_types.len(), types.len());
for (lhs, rhs) in result_types.iter().zip(types.iter()) {
assert_eq!(lhs, rhs);
Expand Down
2 changes: 1 addition & 1 deletion crates/mun_abi/src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ macro_rules! define_primitives {
$(
impl HasStaticTypeId for $ty {
fn type_id() -> &'static $crate::TypeId<'static> {
const TYPE_ID: $crate::TypeId = $crate::TypeId::Concrete(Guid::from_str($name));
const TYPE_ID: $crate::TypeId<'static> = $crate::TypeId::Concrete(Guid::from_str($name));
&TYPE_ID
}
}
Expand Down
7 changes: 4 additions & 3 deletions crates/mun_abi/src/static_type_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ impl<T: 'static> StaticTypeMap<T> {

// Insert the value into the map
let old = map.borrow_mut().insert(TypeId::of::<Type>(), reference);
if old.is_some() {
panic!("StaticTypeMap value was reinitialized. This is a bug.")
}
assert!(
old.is_none(),
"StaticTypeMap value was reinitialized. This is a bug."
);
reference
}
}
10 changes: 5 additions & 5 deletions crates/mun_abi/src/struct_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ impl<'a> serde::Serialize for StructDefinition<'a> {
use itertools::Itertools;
use serde::ser::SerializeStruct;

let mut s = serializer.serialize_struct("StructInfo", 3)?;

#[derive(serde::Serialize)]
struct Field<'a> {
name: &'a str,
r#type: &'a TypeId<'a>,
offset: &'a u16,
}

let mut s = serializer.serialize_struct("StructInfo", 3)?;

s.serialize_field("guid", &self.guid)?;
s.serialize_field(
"fields",
Expand Down Expand Up @@ -143,7 +143,7 @@ mod tests {
field_names,
field_types,
field_offsets,
Default::default(),
StructMemoryKind::default(),
);

assert_eq!(struct_info.field_names().count(), 0);
Expand All @@ -165,12 +165,12 @@ mod tests {
field_names,
field_types,
field_offsets,
Default::default(),
StructMemoryKind::default(),
);

assert_eq!(struct_info.num_fields(), 1);
for (lhs, rhs) in struct_info.field_names().zip([FAKE_FIELD_NAME].iter()) {
assert_eq!(lhs, *rhs)
assert_eq!(lhs, *rhs);
}
assert_eq!(struct_info.field_types(), field_types);
assert_eq!(struct_info.field_offsets(), field_offsets);
Expand Down
2 changes: 1 addition & 1 deletion crates/mun_abi/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{

pub(crate) const FAKE_TYPE_GUID: Guid =
Guid([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
pub(crate) const FAKE_TYPE_ID: TypeId = TypeId::Concrete(FAKE_TYPE_GUID);
pub(crate) const FAKE_TYPE_ID: TypeId<'static> = TypeId::Concrete(FAKE_TYPE_GUID);
pub(crate) const FAKE_DEPENDENCY: &str = "path/to/dependency.munlib";
pub(crate) const FAKE_FIELD_NAME: &str = "field_name";
pub(crate) const FAKE_FN_NAME: &str = "fn_name";
Expand Down
6 changes: 3 additions & 3 deletions crates/mun_abi/src/type_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ impl<'a> fmt::Display for ArrayTypeId<'a> {

/// A trait that defines that for a type we can statically return a `TypeId`.
pub trait HasStaticTypeId {
/// Returns a reference to the TypeInfo for the type
/// Returns a reference to the [`TypeInfo`] for the type
fn type_id() -> &'static TypeId<'static>;
}

impl<T: HasStaticTypeId + 'static> HasStaticTypeId for *const T {
fn type_id() -> &'static TypeId<'static> {
static VALUE: OnceCell<StaticTypeMap<TypeId>> = OnceCell::new();
static VALUE: OnceCell<StaticTypeMap<TypeId<'static>>> = OnceCell::new();
let map = VALUE.get_or_init(Default::default);
map.call_once::<T, _>(|| {
PointerTypeId {
Expand All @@ -107,7 +107,7 @@ impl<T: HasStaticTypeId + 'static> HasStaticTypeId for *const T {

impl<T: HasStaticTypeId + 'static> HasStaticTypeId for *mut T {
fn type_id() -> &'static TypeId<'static> {
static VALUE: OnceCell<StaticTypeMap<TypeId>> = OnceCell::new();
static VALUE: OnceCell<StaticTypeMap<TypeId<'static>>> = OnceCell::new();
let map = VALUE.get_or_init(Default::default);
map.call_once::<T, _>(|| {
PointerTypeId {
Expand Down
17 changes: 10 additions & 7 deletions crates/mun_abi/src/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl<'a> TypeDefinition<'a> {
}

/// Retrieves the type's struct information, if available.
pub fn as_struct(&self) -> Option<&StructDefinition> {
pub fn as_struct(&self) -> Option<&StructDefinition<'_>> {
let TypeDefinitionData::Struct(s) = &self.data;
Some(s)
}
Expand Down Expand Up @@ -147,15 +147,18 @@ impl<'a> TypeDefinitionData<'a> {

/// A trait that defines that for a type we can statically return a type name.
pub trait HasStaticTypeName {
/// Returns a reference to the TypeInfo for the type
/// Returns a reference to the [`TypeInfo`] for the type
fn type_name() -> &'static CStr;
}

#[cfg(test)]
mod tests {
use std::ffi::CString;

use crate::test_utils::{fake_struct_definition, fake_type_definition, FAKE_TYPE_NAME};
use crate::{
test_utils::{fake_struct_definition, fake_type_definition, FAKE_TYPE_NAME},
StructMemoryKind,
};

use super::TypeDefinitionData;

Expand All @@ -170,7 +173,7 @@ mod tests {
field_names,
field_types,
field_offsets,
Default::default(),
StructMemoryKind::default(),
);

let type_definition =
Expand All @@ -189,7 +192,7 @@ mod tests {
field_names,
field_types,
field_offsets,
Default::default(),
StructMemoryKind::default(),
);

let type_definition =
Expand All @@ -211,7 +214,7 @@ mod tests {
field_names,
field_types,
field_offsets,
Default::default(),
StructMemoryKind::default(),
);

let type_definition =
Expand All @@ -230,7 +233,7 @@ mod tests {
field_names,
field_types,
field_offsets,
Default::default(),
StructMemoryKind::default(),
);

let type_definition =
Expand Down
Loading

0 comments on commit 5b87d1a

Please sign in to comment.