Skip to content

Commit

Permalink
Fix lints.
Browse files Browse the repository at this point in the history
Fix more clippy lints.

Fix more clippy lints 3
  • Loading branch information
ralfbiedert committed Jan 31, 2025
1 parent 850ff1f commit 8db89a3
Show file tree
Hide file tree
Showing 36 changed files with 1,260 additions and 1,022 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ pedantic = { level = "deny", priority = -1 }
nursery = { level = "deny", priority = -1 } # We might want to disable this if it causes issues.

option_if_let_else = "allow" # trips up on `darling`
match_same_arms = "allow" # too many
missing_const_for_fn = "allow"
missing_errors_doc = "allow"
missing_panics_doc = "allow"
default_constructed_unit_structs = "allow"
16 changes: 8 additions & 8 deletions crates/backend_c/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ pub enum CNamingStyle {
Uppercase,
/// Names in mixed case starting with lowercase without spacing e.g. 'theTypeName'
LowerCamelCase,
/// Names in mixed case starting with uppercase without spacing e.g. 'TheTypeName'
/// Names in mixed case starting with uppercase without spacing e.g. '`TheTypeName`'
UpperCamelCase,
/// Names in lower case with '_' as spacing e.g. 'the_type_name'
/// Names in lower case with '_' as spacing e.g. '`the_type_name`'
SnakeCase,
/// Names in upper case with '_' as spacing e.g. 'THE_TYPE_NAME'
/// Names in upper case with '_' as spacing e.g. '`THE_TYPE_NAME`'
ShoutySnakeCase,
}

pub(crate) trait ToNamingStyle {
pub trait ToNamingStyle {
fn to_naming_style(&self, style: &CNamingStyle) -> String;
}

Expand Down Expand Up @@ -78,7 +78,7 @@ pub struct Config {
pub directives: bool,
/// Whether to write `#include <>` directives.
pub imports: bool,
/// Additional `#include` lines in the form of `<item.h>`` or `"item.h"`.
/// Additional `#include` lines in the form of `<item.h>` or `"item.h"`.
pub additional_includes: Vec<String>,
/// The `_X` in `#ifndef _X` to be used.
pub ifndef: String,
Expand Down Expand Up @@ -114,9 +114,9 @@ impl Default for Config {
additional_includes: vec![],
file_header_comment: "// Automatically generated by Interoptopus.".to_string(),
ifndef: "interoptopus_generated".to_string(),
custom_defines: "".to_string(),
function_attribute: "".to_string(),
prefix: "".to_string(),
custom_defines: String::new(),
function_attribute: String::new(),
prefix: String::new(),
indentation: CIndentationStyle::Whitesmiths,
documentation: CDocumentationStyle::Inline,
type_naming: CNamingStyle::Lowercase,
Expand Down
25 changes: 13 additions & 12 deletions crates/backend_c/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub struct Converter {
}

impl Converter {
pub fn new(config: Config) -> Self {
#[must_use]
pub const fn new(config: Config) -> Self {
Self { config }
}
}
Expand Down Expand Up @@ -93,17 +94,17 @@ pub trait CTypeConverter {
fn constant_value_to_value(&self, value: &ConstantValue) -> String {
match value {
ConstantValue::Primitive(x) => match x {
PrimitiveValue::Bool(x) => format!("{}", x),
PrimitiveValue::U8(x) => format!("{}", x),
PrimitiveValue::U16(x) => format!("{}", x),
PrimitiveValue::U32(x) => format!("{}", x),
PrimitiveValue::U64(x) => format!("{}", x),
PrimitiveValue::I8(x) => format!("{}", x),
PrimitiveValue::I16(x) => format!("{}", x),
PrimitiveValue::I32(x) => format!("{}", x),
PrimitiveValue::I64(x) => format!("{}", x),
PrimitiveValue::F32(x) => format!("{}", x),
PrimitiveValue::F64(x) => format!("{}", x),
PrimitiveValue::Bool(x) => format!("{x}"),
PrimitiveValue::U8(x) => format!("{x}"),
PrimitiveValue::U16(x) => format!("{x}"),
PrimitiveValue::U32(x) => format!("{x}"),
PrimitiveValue::U64(x) => format!("{x}"),
PrimitiveValue::I8(x) => format!("{x}"),
PrimitiveValue::I16(x) => format!("{x}"),
PrimitiveValue::I32(x) => format!("{x}"),
PrimitiveValue::I64(x) => format!("{x}"),
PrimitiveValue::F32(x) => format!("{x}"),
PrimitiveValue::F64(x) => format!("{x}"),
},
}
}
Expand Down
24 changes: 12 additions & 12 deletions crates/backend_c/src/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ pub struct DocGenerator<W> {
}

impl<W: CWriter> DocGenerator<W> {
pub fn new(inventory: Inventory, w: W) -> Self {
pub const fn new(inventory: Inventory, w: W) -> Self {
Self { inventory, c_writer: w }
}

pub fn inventory(&self) -> &Inventory {
pub const fn inventory(&self) -> &Inventory {
&self.inventory
}

pub fn write_types(&self, w: &mut IndentWriter) -> Result<(), Error> {
indented!(w, r#"# Types "#)?;
indented!(w, r"# Types ")?;

let mut known_function_pointers = vec![];

Expand All @@ -49,23 +49,23 @@ impl<W: CWriter> DocGenerator<W> {
w.newline()?;
w.newline()?;

indented!(w, r#"## {} "#, the_type.name_within_lib())?;
indented!(w, r"## {} ", the_type.name_within_lib())?;
w.newline()?;

for line in meta.documentation().lines() {
indented!(w, r#"{}"#, line.trim())?;
indented!(w, r"{}", line.trim())?;
w.newline()?;
}

indented!(w, r#"```"#)?;
indented!(w, r"```")?;
self.c_writer.write_type_definition(w, the_type, known_function_pointers)?;
indented!(w, r#"```"#)?;
indented!(w, r"```")?;

Ok(())
}

pub fn write_functions(&self, w: &mut IndentWriter) -> Result<(), Error> {
indented!(w, r#"# Functions "#)?;
indented!(w, r"# Functions ")?;

for the_type in self.inventory().functions() {
self.write_function(w, the_type)?;
Expand All @@ -75,19 +75,19 @@ impl<W: CWriter> DocGenerator<W> {
}

fn write_function(&self, w: &mut IndentWriter, function: &Function) -> Result<(), Error> {
indented!(w, r#"## {} "#, function.name())?;
indented!(w, r"## {} ", function.name())?;

for line in function.meta().documentation().lines() {
if line.trim().starts_with('#') {
write!(w.writer(), "##")?;
}
indented!(w, r#"{}"#, line.trim())?;
indented!(w, r"{}", line.trim())?;
w.newline()?;
}

indented!(w, r#"```"#)?;
indented!(w, r"```")?;
self.c_writer.write_function_declaration(w, function, 80)?;
indented!(w, r#"```"#)?;
indented!(w, r"```")?;

w.newline()?;

Expand Down
1 change: 1 addition & 0 deletions crates/backend_c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pub struct Generator {
}

impl Generator {
#[must_use]
pub fn new(config: Config, inventory: Inventory) -> Self {
Self {
config: config.clone(),
Expand Down
78 changes: 38 additions & 40 deletions crates/backend_c/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ pub trait CWriter {
}

fn write_imports(&self, w: &mut IndentWriter) -> Result<(), Error> {
indented!(w, r#"#include <stdint.h>"#)?;
indented!(w, r#"#include <stdbool.h>"#)?;
indented!(w, r"#include <stdint.h>")?;
indented!(w, r"#include <stdbool.h>")?;

// Write any user supplied includes into the file.
for include in &self.config().additional_includes {
Expand Down Expand Up @@ -62,7 +62,7 @@ pub trait CWriter {
self.write_documentation(w, constant.meta().documentation())?;
}

indented!(w, r#"const {} {} = {};"#, the_type, name, self.converter().constant_value_to_value(constant.value()))?;
indented!(w, r"const {} {} = {};", the_type, name, self.converter().constant_value_to_value(constant.value()))?;

Ok(())
}
Expand Down Expand Up @@ -99,7 +99,7 @@ pub trait CWriter {

let mut params = Vec::new();

for p in function.signature().params().iter() {
for p in function.signature().params() {
match p.the_type() {
CType::Array(a) => {
params.push(format!(
Expand All @@ -120,16 +120,16 @@ pub trait CWriter {
}

// Test print line to see if we need to break it
let line = format!(r#"{}{} {}({});"#, attr, rval, name, params.join(", "));
let line = format!(r"{}{} {}({});", attr, rval, name, params.join(", "));

if line.len() <= max_line {
indented!(w, r#"{}{} {}({});"#, attr, rval, name, params.join(", "))?
indented!(w, r"{}{} {}({});", attr, rval, name, params.join(", "))?;
} else {
indented!(w, r#"{}{} {}("#, attr, rval, name)?;
indented!(w, r"{}{} {}(", attr, rval, name)?;
for p in params {
indented!(w, [_], r#"{}"#, p)?;
indented!(w, [()], r"{}", p)?;
}
indented!(w, [_], r#");"#)?
indented!(w, [()], r");")?;
}

Ok(())
Expand All @@ -142,7 +142,7 @@ pub trait CWriter {

let mut params = Vec::new();

for p in function.signature().params().iter() {
for p in function.signature().params() {
match p.the_type() {
CType::Array(a) => {
params.push(format!("{} [{}]", self.converter().to_type_specifier(a.array_type()), a.len(),));
Expand All @@ -152,14 +152,14 @@ pub trait CWriter {
}
}
}
indented!(w, r#"typedef {} (*{})({});"#, rval, name, params.join(", "))?;
indented!(w, r"typedef {} (*{})({});", rval, name, params.join(", "))?;

Ok(())
}

fn write_documentation(&self, w: &mut IndentWriter, documentation: &Documentation) -> Result<(), Error> {
for line in documentation.lines() {
indented!(w, r#"///{}"#, line)?;
indented!(w, r"///{}", line)?;
}

Ok(())
Expand Down Expand Up @@ -260,7 +260,7 @@ pub trait CWriter {
let name = self.converter().named_callback_to_typename(the_type);

let mut params = Vec::new();
for param in the_type.fnpointer().signature().params().iter() {
for param in the_type.fnpointer().signature().params() {
params.push(format!(
"{} {}",
self.converter().to_type_specifier(param.the_type()),
Expand All @@ -280,7 +280,7 @@ pub trait CWriter {
self.write_documentation(w, the_type.meta().documentation())?;
}

self.write_braced_declaration_opening(w, format!("typedef enum {}", name))?;
self.write_braced_declaration_opening(w, format!("typedef enum {name}"))?;

for variant in the_type.variants() {
self.write_type_definition_enum_variant(w, variant, the_type)?;
Expand All @@ -294,10 +294,10 @@ pub trait CWriter {
let variant_value = variant.value();

if self.config().documentation == CDocumentationStyle::Inline {
self.write_documentation(w, variant.documentation())?
self.write_documentation(w, variant.documentation())?;
}

indented!(w, r#"{} = {},"#, variant_name, variant_value)
indented!(w, r"{} = {},", variant_name, variant_value)
}

fn write_type_definition_opaque(&self, w: &mut IndentWriter, the_type: &OpaqueType) -> Result<(), Error> {
Expand All @@ -316,7 +316,7 @@ pub trait CWriter {

fn write_type_definition_opaque_body(&self, w: &mut IndentWriter, the_type: &OpaqueType) -> Result<(), Error> {
let name = self.converter().opaque_to_typename(the_type);
indented!(w, r#"typedef struct {} {};"#, name, name)
indented!(w, r"typedef struct {} {};", name, name)
}

fn write_type_definition_composite(&self, w: &mut IndentWriter, the_type: &CompositeType) -> Result<(), Error> {
Expand All @@ -328,7 +328,7 @@ pub trait CWriter {

if the_type.is_empty() {
// C doesn't allow us writing empty structs.
indented!(w, r#"typedef struct {} {};"#, name, name)?;
indented!(w, r"typedef struct {} {};", name, name)?;
Ok(())
} else {
self.write_type_definition_composite_body(w, the_type)
Expand All @@ -343,7 +343,7 @@ pub trait CWriter {
indented!(w, "#pragma pack(push, {})", align)?;
}

self.write_braced_declaration_opening(w, format!(r#"typedef struct {}"#, name))?;
self.write_braced_declaration_opening(w, format!(r"typedef struct {name}"))?;

for field in the_type.fields() {
self.write_type_definition_composite_body_field(w, field, the_type)?;
Expand All @@ -362,52 +362,50 @@ pub trait CWriter {
self.write_documentation(w, field.documentation())?;
}

match field.the_type() {
CType::Array(x) => {
let field_name = field.name();
let type_name = self.converter().to_type_specifier(x.array_type());
indented!(w, r#"{} {}[{}];"#, type_name, field_name, x.len())
}
_ => {
let field_name = field.name();
let type_name = self.converter().to_type_specifier(field.the_type());
indented!(w, r#"{} {};"#, type_name, field_name)
}
let field_name = field.name();

if let CType::Array(x) = field.the_type() {
let type_name = self.converter().to_type_specifier(x.array_type());
indented!(w, r"{} {}[{}];", type_name, field_name, x.len())
} else {
let field_name = field.name();
let type_name = self.converter().to_type_specifier(field.the_type());
indented!(w, r"{} {};", type_name, field_name)
}
}

fn write_ifndef(&self, w: &mut IndentWriter, f: impl FnOnce(&mut IndentWriter) -> Result<(), Error>) -> Result<(), Error> {
if self.config().directives {
indented!(w, r#"#ifndef {}"#, self.config().ifndef)?;
indented!(w, r#"#define {}"#, self.config().ifndef)?;
indented!(w, r"#ifndef {}", self.config().ifndef)?;
indented!(w, r"#define {}", self.config().ifndef)?;
w.newline()?;
}

f(w)?;

if self.config().directives {
w.newline()?;
indented!(w, r#"#endif /* {} */"#, self.config().ifndef)?;
indented!(w, r"#endif /* {} */", self.config().ifndef)?;
}

Ok(())
}

fn write_ifdefcpp(&self, w: &mut IndentWriter, f: impl FnOnce(&mut IndentWriter) -> Result<(), Error>) -> Result<(), Error> {
if self.config().directives {
indented!(w, r#"#ifdef __cplusplus"#)?;
indented!(w, r"#ifdef __cplusplus")?;
indented!(w, r#"extern "C" {{"#)?;
indented!(w, r#"#endif"#)?;
indented!(w, r"#endif")?;
w.newline()?;
}

f(w)?;

if self.config().directives {
w.newline()?;
indented!(w, r#"#ifdef __cplusplus"#)?;
indented!(w, r#"}}"#)?;
indented!(w, r#"#endif"#)?;
indented!(w, r"#ifdef __cplusplus")?;
indented!(w, r"}}")?;
indented!(w, r"#endif")?;
}
Ok(())
}
Expand Down Expand Up @@ -461,7 +459,7 @@ pub trait CWriter {
}
CIndentationStyle::Whitesmiths => {
indented!(w, "{}", definition)?;
indented!(w, [_], "{{")?;
indented!(w, [()], "{{")?;
w.indent();
}
}
Expand All @@ -481,7 +479,7 @@ pub trait CWriter {
}
CIndentationStyle::Whitesmiths => {
w.unindent();
indented!(w, [_], "}} {};", name)?;
indented!(w, [()], "}} {};", name)?;
}
}

Expand Down
Loading

0 comments on commit 8db89a3

Please sign in to comment.