Clippy cleanup
MODIFY
binding-generator/src/comment.rs
binding-generator/src/comment.rs
@@ -37,14 +37,12 @@ pub fn strip_comment_markers(comment: &str) -> String {
singleline_delimited = true;
asterisk_indented = false;
}
- } else {
- if let Some(line_clean) = line_clean.strip_str_prefix(MULTILINE_PREFIX) {
+ } else if let Some(line_clean) = line_clean.strip_str_prefix(MULTILINE_PREFIX) {
line = line_clean
.trim_start_matches(DETAIL)
.trim_start_matches(MULTILINE_CONT)
.trim_start();
}
- }
if singleline_delimited && line_clean.starts_with(SINGLELINE) {
line = &line_clean[SINGLELINE.len()..];
if let Some(new_line) = line.strip_str_prefix(SINGLELINE_DETAIL) {
@@ -118,7 +116,7 @@ pub fn strip_comment_markers(comment: &str) -> String {
}
} else {
let indent_len = common_indent.unwrap_or_default().len;
- if line.len() >= indent_len + 1 {
+ if line.len() > indent_len {
line = &line[indent_len..];
} else {
line = "\n";

MODIFY
binding-generator/src/constant.rs
binding-generator/src/constant.rs
@@ -30,6 +30,7 @@ pub fn render_constant_rust<'f>(tokens: impl IntoIterator<Item=Token<'f>>) -> Op
TokenKind::Identifier => {
let spelling = t.get_spelling();
if let Some(entity) = t.get_location().get_entity() {
+ #[allow(clippy::single_match)] // using the single pattern for future extensibility
match entity.get_kind() {
EntityKind::MacroExpansion => {
let cnst = Const::new(entity);

MODIFY
binding-generator/src/element.rs
binding-generator/src/element.rs
@@ -311,7 +311,7 @@ pub fn module_from_path(path: &Path) -> Option<&str> {
opencv_module_component(path)
.and_then(|m| m.to_str())
.map(|m| m.split('.')
- .nth(0)
+ .next()
.unwrap()
)

MODIFY
binding-generator/src/field.rs
binding-generator/src/field.rs
@@ -82,11 +82,9 @@ impl<'tu> Field<'tu> {
let mut children = vec![];
let mut skipping_typeref = true;
self.entity.visit_children(|c, _| {
- if skipping_typeref {
- if c.get_kind() != EntityKind::TypeRef {
+ if skipping_typeref && c.get_kind() != EntityKind::TypeRef {
skipping_typeref = false;
}
- }
if !skipping_typeref {
children.push(c);
EntityVisitResult::Recurse

MODIFY
binding-generator/src/func.rs
binding-generator/src/func.rs
@@ -143,15 +143,13 @@ impl<'tu> Func<'tu> {
);
if self.entity.is_static_method() {
Kind::StaticMethod(class)
- } else {
- if let Some(operator) = self.entity.cpp_localname().strip_str_prefix(OPERATOR) {
+ } else if let Some(operator) = self.entity.cpp_localname().strip_str_prefix(OPERATOR) {
let arg_count = self.entity.get_arguments().map_or(0, |v| v.len());
Kind::InstanceOperator(class, OperatorKind::new(operator.trim(), arg_count))
} else {
Kind::InstanceMethod(class)
}
}
- }
EntityKind::FieldDecl | EntityKind::VarDecl => {
Kind::FieldAccessor(Field::new(self.entity, self.gen_env).parent())
}
@@ -367,13 +365,13 @@ impl<'tu> Func<'tu> {
}
if let Some(slice_arg) = settings::SLICE_ARGUMENT.get(&(func_name.as_ref(), args_len)) {
- match slice_arg {
- &SliceHint::ForceSlice(arg) => {
+ match *slice_arg {
+ SliceHint::ForceSlice(arg) => {
if arg == a.rust_leafname().as_ref() {
return Field::new_ext(a, FieldTypeHint::Slice, self.gen_env)
}
},
- &SliceHint::ConvertSlice(ptr_arg, len_arg, len_div) => {
+ SliceHint::ConvertSlice(ptr_arg, len_arg, len_div) => {
let arg_name = a.rust_leafname();
if ptr_arg == arg_name.as_ref() {
return Field::new_ext(a, FieldTypeHint::Slice, self.gen_env)
@@ -575,7 +573,8 @@ impl Element for Func<'_> {
};
let rust_name = if let Some(cls) = self.as_constructor() {
let args = self.arguments();
- 'ctor_name: loop { // fixme use named block when stable
+ #[allow(clippy::never_loop)] // fixme use named block when stable
+ 'ctor_name: loop {
if args.is_empty() {
break 'ctor_name "default";
} else if args.len() == 1 {

MODIFY
binding-generator/src/generator.rs
binding-generator/src/generator.rs
@@ -302,12 +302,10 @@ impl<V: for<'gtu> GeneratorVisitor<'gtu>> Drop for OpenCVWalker<'_, V> {
let mut found_module_comment = false;
let mut defgroup_found = false;
line_reader(f, |line| {
- if !found_module_comment {
- if line.trim_start().starts_with("/**") {
+ if !found_module_comment && line.trim_start().starts_with("/**") {
found_module_comment = true;
defgroup_found = false;
}
- }
if found_module_comment {
if comment.contains("@defgroup") {
defgroup_found = true;

MODIFY
binding-generator/src/lib.rs
binding-generator/src/lib.rs
// fixme returning MatAllocator (trait) by reference is bad, check knearestneighbour
// fixme VectorOfMat::get allows to mutate?
+#![allow(clippy::nonminimal_bool)] // pattern `!type_ref.as_vector().is_some()` used for more clarity
+
use std::{
borrow::Cow,
env,

MODIFY
binding-generator/src/string_ext.rs
binding-generator/src/string_ext.rs
@@ -56,7 +56,7 @@ impl StringExt for String {
let mut out = Vec::with_capacity(10);
let mut last_idx = 0;
for (idx, _) in rep.match_indices('$') {
- if let Some((mut next_idx, next_char)) = rep[idx..].char_indices().skip(1).next() {
+ if let Some((mut next_idx, next_char)) = rep[idx..].char_indices().nth(1) {
next_idx += idx;
if next_char == '$' {
out.push(Elem::Literal(&rep[last_idx..next_idx]));
@@ -353,6 +353,7 @@ impl StrExt for str {
static R5: Lazy<Regex> = Lazy::new(|| Regex::new(r#"Open_(CL|Gl|VX)"#).expect("Can't compile regex"));
let out = R5.replace_all(&out, "Open$1");
+ #[allow(clippy::trivial_regex)]
static R6: Lazy<Regex> = Lazy::new(|| Regex::new(r#"U_Mat"#).expect("Can't compile regex"));
let out = R6.replace_all(&out, "UMat");

MODIFY
binding-generator/src/type_ref.rs
binding-generator/src/type_ref.rs
@@ -14,12 +14,12 @@ use regex::{Captures, Regex};
use crate::{
Class,
DefinitionLocation,
+ DependentType,
Element,
EntityExt,
Enum,
Field,
Function,
- DependentType,
GeneratorEnv,
IteratorExt,
ReturnTypeWrapper,
@@ -308,13 +308,12 @@ impl<'tu> TypeRef<'tu> {
} else {
Kind::Class(Class::new(decl, self.gen_env))
}
- } else {
- if let TypeRefTypeHint::Specialized(typ) = self.type_hint {
+ } else if let TypeRefTypeHint::Specialized(typ) = self.type_hint {
TypeRef::new_ext(typ, self.type_hint, self.parent_entity, self.gen_env).kind()
} else {
let mut generic_type = self.type_ref.get_display_name();
// workaround for clang6, FunctionPrototype is seen as Unexposed
- if generic_type.contains("(") && generic_type.contains(")") {
+ if generic_type.contains('(') && generic_type.contains(')') {
return Kind::Function(
Function::new(self.type_ref, self.parent_entity.expect("Can't get parent entity in function prototype"), self.gen_env)
);
@@ -323,7 +322,6 @@ impl<'tu> TypeRef<'tu> {
Kind::Generic(generic_type)
}
}
- }
TypeKind::Typedef => {
let decl = self.type_ref.get_declaration().expect("Can't get typedef declaration");
@@ -994,6 +992,7 @@ impl<'tu> TypeRef<'tu> {
fn rust_type_ref(&self, form: Form) -> Cow<str> {
if self.is_string() {
+ #[allow(clippy::if_same_then_else)]
return if self.is_const() {
"String" // todo implement receiving const str's
} else {
@@ -1163,6 +1162,7 @@ impl<'tu> TypeRef<'tu> {
}
pub fn rust_extern_with_const(&self, constness: Constness) -> Cow<str> {
+ #[allow(clippy::never_loop)] // fixme use named block when stable
'typ: loop {
if self.is_string() {
break 'typ if constness.is_const(self) {
@@ -1227,6 +1227,7 @@ impl<'tu> TypeRef<'tu> {
}
pub fn rust_arg_func_decl(&self, name: &str) -> String {
+ #[allow(clippy::never_loop)] // fixme use named block when stable
let typ = 'decl_type: loop {
if self.is_string() {
if self.is_output() {
@@ -1483,7 +1484,7 @@ impl<'tu> TypeRef<'tu> {
);
let def_location = match def_location {
DefinitionLocation::Type => DefinitionLocation::Custom(self.rust_module().into_owned()),
- dl @ _ => dl
+ dl => dl
};
D::from_return_type_wrapper(ReturnTypeWrapper::new(type_ref, self.gen_env, def_location))
} else {

MODIFY
binding-generator/src/writer/rust_native/func.rs
binding-generator/src/writer/rust_native/func.rs
@@ -52,7 +52,7 @@ fn gen_rust_with_name(f: &Func, name: &str, opencv_version: &str) -> String {
let type_ref = arg.type_ref();
if arg.is_user_data() {
pre_post_arg_handle(
- type_ref.rust_userdata_pre_call(&name, callback_arg_name.as_ref().map(|x| x.as_str()).expect("Can't get name of the callback arg")),
+ type_ref.rust_userdata_pre_call(&name, callback_arg_name.as_deref().expect("Can't get name of the callback arg")),
&mut pre_call_args,
);
} else {

MODIFY
binding-generator/src/writer/rust_native/mod.rs
binding-generator/src/writer/rust_native/mod.rs
@@ -107,12 +107,15 @@ impl<'s> RustNativeBindingWriter<'s> {
}
fn emit_debug_log(&mut self, obj: &impl Debug) {
- if self.debug && false {
+ #[allow(clippy::collapsible_if)]
+ if false {
+ if self.debug {
let mut f = OpenOptions::new().append(true).open(&self.debug_path).expect("Can't open debug file");
writeln!(f, "{:#?}", obj).expect("Can't write debug info");
}
}
}
+}
impl<'tu> GeneratorVisitor<'tu> for RustNativeBindingWriter<'_> {
type D = DepType<'tu>;

MODIFY
binding-generator/src/writer/rust_native/vector.rs
binding-generator/src/writer/rust_native/vector.rs
@@ -96,7 +96,7 @@ impl RustNativeGeneratedElement for Vector<'_> {
"cpp_extern_return" => vec_type.cpp_extern_return(),
"inner_cpp_full" => element_type.cpp_full(),
"inner_cpp_func_decl" => element_type.cpp_arg_func_decl("val").into(),
- "inner_cpp_func_call" => element_type.cpp_arg_func_call("val").into(),
+ "inner_cpp_func_call" => element_type.cpp_arg_func_call("val"),
"inner_cpp_extern_return" => element_type.cpp_extern_return(),
"inner_cpp_extern_return_wrapper" => element_type.cpp_extern_return_wrapper_full(),
};

Clippy cleanup