Skip to content

Commit

Permalink
Fixed various clippy lints (#1383)
Browse files Browse the repository at this point in the history
Fix clippy lints
  • Loading branch information
pentamassiv committed Oct 10, 2022
1 parent b5068ed commit b3147f2
Show file tree
Hide file tree
Showing 43 changed files with 218 additions and 256 deletions.
2 changes: 1 addition & 1 deletion src/analysis/bounds.rs
Expand Up @@ -177,7 +177,7 @@ impl Bounds {
}
}
if (!need_is_into_check || !*par.nullable) && par.c_type != "GDestroyNotify" {
self.add_parameter(&par.name, &type_string, bound_type, r#async)
self.add_parameter(&par.name, &type_string, bound_type, r#async);
}
}
} else if par.instance_parameter {
Expand Down
6 changes: 1 addition & 5 deletions src/analysis/child_properties.rs
Expand Up @@ -44,11 +44,7 @@ pub fn analyze(
return properties;
}
let config = config.unwrap();
let child_name = config
.child_name
.as_ref()
.map(|s| &s[..])
.unwrap_or("child");
let child_name = config.child_name.as_ref().map_or("child", |s| s.as_str());
let child_type = config
.child_type
.as_ref()
Expand Down
4 changes: 2 additions & 2 deletions src/analysis/class_hierarchy.rs
Expand Up @@ -35,7 +35,7 @@ fn get_node<'a>(
let direct_supers: Vec<TypeId> = match library.type_(tid) {
Type::Class(Class {
parent, implements, ..
}) => parent.iter().chain(implements.iter()).cloned().collect(),
}) => parent.iter().chain(implements.iter()).copied().collect(),
Type::Interface(Interface { prerequisites, .. }) => prerequisites.clone(),
_ => return None,
};
Expand Down Expand Up @@ -64,7 +64,7 @@ fn get_node<'a>(
impl Info {
pub fn subtypes<'a>(&'a self, tid: TypeId) -> Box<dyn Iterator<Item = TypeId> + 'a> {
match self.hier.get(&tid) {
Some(node) => Box::new(node.subs.iter().cloned()),
Some(node) => Box::new(node.subs.iter().copied()),
None => Box::new(iter::empty()),
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/analysis/conversion_type.rs
Expand Up @@ -32,6 +32,8 @@ impl Default for ConversionType {

impl ConversionType {
pub fn of(env: &env::Env, type_id: TypeId) -> ConversionType {
use crate::library::{Basic::*, Type::*};

let library = &env.library;

if let Some(conversion_type) = env
Expand All @@ -43,7 +45,6 @@ impl ConversionType {
return conversion_type;
}

use crate::library::{Basic::*, Type::*};
match library.type_(type_id) {
Basic(fund) => match fund {
Boolean => ConversionType::Scalar,
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/ffi_type.rs
Expand Up @@ -181,7 +181,7 @@ fn fix_name(env: &Env, type_id: TypeId, name: &str) -> Result {
let sys_crate_name = if sys_crate_name == "gobject_ffi" {
use_glib_type(env, "gobject_ffi")
} else if type_id.ns_id == MAIN_NAMESPACE {
sys_crate_name.to_owned()
sys_crate_name.clone()
} else {
format!(
"{}::{}",
Expand Down
13 changes: 4 additions & 9 deletions src/analysis/functions.rs
Expand Up @@ -175,7 +175,7 @@ pub fn analyze<F: Borrow<library::Function>>(
let func = func.borrow();
let configured_functions = obj.functions.matched(&func.name);
let mut status = obj.status;
for f in configured_functions.iter() {
for f in &configured_functions {
match f.status {
GStatus::Ignore => continue 'func,
GStatus::Manual => {
Expand Down Expand Up @@ -923,7 +923,7 @@ fn analyze_function(
async_future,
callbacks,
destroys,
remove_params: cross_user_data_check.values().cloned().collect::<Vec<_>>(),
remove_params: cross_user_data_check.values().copied().collect::<Vec<_>>(),
commented,
hidden: false,
ns_id,
Expand All @@ -935,12 +935,7 @@ pub fn is_carray_with_direct_elements(env: &Env, typ: library::TypeId) -> bool {
match *env.library.type_(typ) {
Type::CArray(inner_tid) => {
use super::conversion_type::ConversionType;
match env.library.type_(inner_tid) {
Type::Basic(..) if ConversionType::of(env, inner_tid) == ConversionType::Direct => {
true
}
_ => false,
}
matches!(env.library.type_(inner_tid), Type::Basic(..) if ConversionType::of(env, inner_tid) == ConversionType::Direct)
}
_ => false,
}
Expand Down Expand Up @@ -1157,7 +1152,7 @@ fn analyze_callback(
}
});
}
for p in parameters.rust_parameters.iter() {
for p in &parameters.rust_parameters {
if let Ok(rust_type) = RustType::builder(env, p.typ)
.direction(p.direction)
.nullable(p.nullable)
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/imports.rs
Expand Up @@ -312,7 +312,7 @@ impl<'a> ImportsWithDefault<'a> {

impl Drop for ImportsWithDefault<'_> {
fn drop(&mut self) {
self.imports.reset_defaults()
self.imports.reset_defaults();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/analysis/namespaces.rs
Expand Up @@ -46,7 +46,7 @@ pub fn run(gir: &library::Library) -> Info {
for (ns_id, ns) in gir.namespaces.iter().enumerate() {
let ns_id = ns_id as NsId;
let crate_name = nameutil::crate_name(&ns.name);
let (sys_crate_name, higher_crate_name) = match &crate_name[..] {
let (sys_crate_name, higher_crate_name) = match crate_name.as_str() {
"gobject" => ("gobject_ffi".to_owned(), "glib".to_owned()),
_ => ("ffi".to_owned(), crate_name.clone()),
};
Expand All @@ -58,7 +58,7 @@ pub fn run(gir: &library::Library) -> Info {
package_name: ns.package_name.clone(),
symbol_prefixes: ns.symbol_prefixes.clone(),
shared_libs: ns.shared_library.clone(),
versions: ns.versions.iter().cloned().collect(),
versions: ns.versions.iter().copied().collect(),
});
if ns.name == "GLib" {
glib_ns_id = Some(ns_id);
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/out_parameters.rs
Expand Up @@ -150,7 +150,7 @@ fn analyze_type_imports(env: &Env, typ: TypeId, caller_allocates: bool, imports:
Type::Alias(alias) => analyze_type_imports(env, alias.typ, caller_allocates, imports),
Type::Bitfield(..) | Type::Enumeration(..) => imports.add("std::mem"),
Type::Basic(fund) if !matches!(fund, Basic::Utf8 | Basic::OsString | Basic::Filename) => {
imports.add("std::mem")
imports.add("std::mem");
}
_ if !caller_allocates => match ConversionType::of(env, typ) {
ConversionType::Direct
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/properties.rs
Expand Up @@ -302,7 +302,7 @@ fn analyze_property(
name: format!("notify::{}", name),
parameters: Vec::new(),
ret: library::Parameter {
name: "".into(),
name: String::new(),
typ: env
.library
.find_type(library::INTERNAL_NAMESPACE, "none")
Expand Down
27 changes: 13 additions & 14 deletions src/analysis/record.rs
Expand Up @@ -176,20 +176,19 @@ pub fn new(env: &Env, obj: &GObject) -> Option<Info> {
|| !specials.has_trait(special_functions::Type::Free))
{
if let Some((_, get_type_version)) = glib_get_type {
if get_type_version > version {
// FIXME: Ideally we would update it here but that requires fixing *all* the
// versions of functions in this and other types that use this type somewhere in
// the signature. Similar code exists for before the analysis already but that
// doesn't apply directly here.
//
// As the get_type function only has a version if explicitly configured let's just
// panic here. It's easy enough for the user to move the version configuration from
// the function to the type.
panic!(
"Have to use get_type function for {} but version is higher than for the type ({:?} > {:?})",
full_name, get_type_version, version
);
}
// FIXME: Ideally we would update it here but that requires fixing *all* the
// versions of functions in this and other types that use this type somewhere in
// the signature. Similar code exists for before the analysis already but that
// doesn't apply directly here.
//
// As the get_type function only has a version if explicitly configured let's just
// panic here. It's easy enough for the user to move the version configuration from
// the function to the type.
assert!(
get_type_version <= version,
"Have to use get_type function for {} but version is higher than for the type ({:?} > {:?})",
full_name, get_type_version, version
);
} else {
error!("Missing memory management functions for {}", full_name);
}
Expand Down
3 changes: 2 additions & 1 deletion src/analysis/ref_mode.rs
Expand Up @@ -18,6 +18,8 @@ impl RefMode {
tid: library::TypeId,
direction: library::ParameterDirection,
) -> RefMode {
use crate::library::Type::*;

let library = &env.library;

if let Some(&GObject {
Expand All @@ -32,7 +34,6 @@ impl RefMode {
}
}

use crate::library::Type::*;
match library.type_(tid) {
Basic(library::Basic::Utf8 | library::Basic::Filename | library::Basic::OsString)
| Class(..)
Expand Down
32 changes: 18 additions & 14 deletions src/analysis/rust_type.rs
Expand Up @@ -35,14 +35,14 @@ impl RustType {
RustTypeBuilder::new(env, type_id)
}

fn new_and_use(rust_type: impl ToString) -> Self {
fn new_and_use(rust_type: &impl ToString) -> Self {
RustType {
inner: rust_type.to_string(),
used_types: vec![rust_type.to_string()],
}
}

fn new_with_uses(rust_type: impl ToString, uses: &[impl ToString]) -> Self {
fn new_with_uses(rust_type: &impl ToString, uses: &[impl ToString]) -> Self {
RustType {
inner: rust_type.to_string(),
used_types: uses.iter().map(ToString::to_string).collect(),
Expand All @@ -52,7 +52,7 @@ impl RustType {
fn check(
env: &Env,
type_id: library::TypeId,
type_name: impl ToString,
type_name: &impl ToString,
) -> result::Result<String, TypeError> {
let mut type_name = type_name.to_string();

Expand All @@ -76,9 +76,11 @@ impl RustType {
}

fn try_new_and_use(env: &Env, type_id: library::TypeId) -> Result {
Self::check(env, type_id, env.library.type_(type_id).get_name()).map(|type_name| RustType {
inner: type_name.clone(),
used_types: vec![type_name],
Self::check(env, type_id, &env.library.type_(type_id).get_name()).map(|type_name| {
RustType {
inner: type_name.clone(),
used_types: vec![type_name],
}
})
}

Expand All @@ -87,7 +89,7 @@ impl RustType {
type_id: library::TypeId,
type_name: impl ToString,
) -> Result {
Self::check(env, type_id, type_name).map(|type_name| RustType {
Self::check(env, type_id, &type_name).map(|type_name| RustType {
inner: type_name.clone(),
used_types: vec![type_name],
})
Expand Down Expand Up @@ -246,7 +248,7 @@ impl<'env> RustTypeBuilder<'env> {
pub fn try_build(self) -> Result {
use crate::library::{Basic::*, Type::*};
let ok = |s: &str| Ok(RustType::from(s));
let ok_and_use = |s: &str| Ok(RustType::new_and_use(s));
let ok_and_use = |s: &str| Ok(RustType::new_and_use(&s));
let err = |s: &str| Err(TypeError::Unimplemented(s.into()));
let mut skip_option = false;
let type_ = self.env.library.type_(self.type_id);
Expand Down Expand Up @@ -438,7 +440,7 @@ impl<'env> RustTypeBuilder<'env> {
}
let mut params = Vec::with_capacity(f.parameters.len());
let mut err = false;
for p in f.parameters.iter() {
for p in &f.parameters {
if p.closure.is_some() {
continue;
}
Expand Down Expand Up @@ -571,14 +573,14 @@ impl<'env> RustTypeBuilder<'env> {
if self.direction == ParameterDirection::In {
rust_type = rust_type.map_any(|rust_type| {
RustType::new_with_uses(
format!("impl Into<{}>", &rust_type.as_str()),
&format!("impl Into<{}>", &rust_type.as_str()),
&[&rust_type.as_str()],
)
});
} else {
rust_type = rust_type.map_any(|_| {
RustType::new_with_uses(
format!("Result<{}, {}>", &ok_type, &err_type),
&format!("Result<{}, {}>", &ok_type, &err_type),
&[ok_type, err_type],
)
});
Expand Down Expand Up @@ -611,9 +613,11 @@ impl<'env> RustTypeBuilder<'env> {
use crate::library::Type::*;
let type_ = self.env.library.type_(self.type_id);

if self.direction == ParameterDirection::None {
panic!("undefined direction for parameter with type {:?}", type_);
}
assert!(
self.direction != ParameterDirection::None,
"undefined direction for parameter with type {:?}",
type_
);

let rust_type = RustType::builder(self.env, self.type_id)
.direction(self.direction)
Expand Down
4 changes: 2 additions & 2 deletions src/analysis/special_functions.rs
Expand Up @@ -247,7 +247,7 @@ pub fn analyze_imports(specials: &Infos, imports: &mut Imports) {
use self::Type::*;
match type_ {
Copy if info.first_parameter_mut => {
imports.add_with_version("glib::translate::*", info.version)
imports.add_with_version("glib::translate::*", info.version);
}
Compare => imports.add_with_version("std::cmp", info.version),
Display => imports.add_with_version("std::fmt", info.version),
Expand All @@ -259,7 +259,7 @@ pub fn analyze_imports(specials: &Infos, imports: &mut Imports) {
for info in specials.functions().values() {
match info.type_ {
FunctionType::StaticStringify => {
imports.add_with_version("std::ffi::CStr", info.version)
imports.add_with_version("std::ffi::CStr", info.version);
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/analysis/symbols.rs
Expand Up @@ -43,10 +43,11 @@ impl Symbol {
}

fn make_in_prelude(&mut self) {
if self.module_name.replace("prelude".to_string()).is_some() {
// .expect_none is not stabilized yet
panic!("{:?} already had a module name set!", self)
}
assert!(
self.module_name.replace("prelude".to_string()).is_none(),
"{:?} already had a module name set!",
self
);
}

/// Convert this symbol into a trait
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/child_properties.rs
Expand Up @@ -105,14 +105,14 @@ fn declaration(env: &Env, prop: &ChildProperty, is_get: bool) -> String {
.into_string();
format!(" -> {}", ret_type)
} else {
"".to_string()
String::new()
};
format!(
"fn {}<{}>(&self, item: &T{}){}",
func_name,
bounds,
if is_get {
"".to_owned()
String::new()
} else {
format!(", {}", prop.set_params)
},
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/doc/gi_docgen.rs
Expand Up @@ -597,7 +597,7 @@ mod tests {
namespace: Some("Gtk".to_string()),
name: "MapListModelMapFunc".to_string()
})
)
);
}

#[test]
Expand Down

0 comments on commit b3147f2

Please sign in to comment.