summaryrefslogtreecommitdiffstats
AgeCommit message (Collapse)Author
2021-09-30Use strip_{prefix,suffix} for code that's easier to followLars Wirzenius
Instead of: if text.starts_with(prefix) { &text[prefix.len()..] } else { &text } use this: if let Some(rest) = text.strip_prefix(prefix) { rest } else { &text } The strip_prefix is easier to understand, and also removes the dependency between the if condition and the computation to get the slice after the prefix. The dependency is reasonably clear, but the compiler does not understand it so it's plausible that a future change might change the condition but not the slice. The approach using strip_prefix avoids that. This was found by the clippy lint manual_strip: https://rust-lang.github.io/rust-clippy/master/index.html#manual_strip
2021-09-30Use match for tri-state conditionLars Wirzenius
A chain of if statements is not guaranteed to test every possible case, the way match does. Thus it seems better to use match here, when there are three possible results from a comparison. Found by clippy lint comparison_chain: https://rust-lang.github.io/rust-clippy/master/index.html#comparison_chain
2021-09-30Allow a pointer-to-pointer transmutationLars Wirzenius
We should arguably use a different approach, but I don't understand the code enough to suggest how. Found by clippy lint transmute_ptr_to_ptr: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_ptr_to_ptr
2021-09-30Tell clippy loops that never loop are OKLars Wirzenius
We have two loops where the last statement is a return or a break, without there being a continue. This means the loops will never loop. This is a symptom of the code being hard to understand. We should probably rewrite the logic without a loop, but I did not feel confident doing that yet. Found by clippy lint never_loop: https://rust-lang.github.io/rust-clippy/master/index.html#never_loop
2021-09-30Allow complex types without namesLars Wirzenius
A complex type without a name tends to be harder to understand. We have a few of them. Using "type" to give them a name would hopefully help, but I don't understand the code well enough to come up with meaningful names, and meaningless names won't help. Also, arguably, a struct or enum would often be the better approach, but would change the API. Thus, I'm only configuring clippy to allow complex types. We should later revisit this and improve the code, when we're making other API changes anyway. Found by clippy lint type_complexity: https://rust-lang.github.io/rust-clippy/master/index.html#type_complexity
2021-09-30Allow up to ten arguments to functionsLars Wirzenius
By default, clippy allows up to seven arguments to a function. Anything more and it will complain. https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments Seven is already a very large number. However, changing the number of arguments in public functions would change the API of the library, so I've upped the limit to ten, which is enough to placate clippy. Arguably, when we are changing the API anyway, we should consider ways to remove the number of arguments to functions, possibly by using the builder pattern.
2021-09-30Allow short single-character argument and variable namesLars Wirzenius
Generally speaking, single-character names are not great for the person reading the code later. They don't usually act as a cognitive aid to understand the code. However, this in code implementing cryptographic operations that implements mathematical formulas that canonically use single-letter names it's clearer to use the same name in the code. Thus, I only tell clippy those names are OK in these cases. Found by clippy lint many_single_char_names: https://rust-lang.github.io/rust-clippy/master/index.html#many_single_char_names
2021-09-30Allow enum variants up to 512 bytesLars Wirzenius
clippy warns if one of the variants of an enum is significantly larger than the others, as that can cause surprising memory use: an enum uses as much space as its largest variant. We have some such enums, but changing them to have variants of similar size would change the API so I've chosen to allow variants up to 512 bytes. Found by clippy lint large_enum_variant: https://rust-lang.github.io/rust-clippy/master/index.html#large_enum_variant
2021-09-30Allow if conditions that use complex codeLars Wirzenius
An if condition is an expression and can be as complex as the programmer wants. However, the more complex a condition is, the harder it tends to be to understand. I marked functions with such if conditions so that clippy won't complain about the code. I probably should have simplified the code, perhaps by extracting the condition to its own function, but it would have been much harder to do, so I didn't. Found by clippy lint blocks_in_if_conditions: https://rust-lang.github.io/rust-clippy/master/index.html#blocks_in_if_conditions
2021-09-30Avoid casting a usize to isize in the argument to pointer::offsetLars Wirzenius
Arguably this code should be rewritten to avoid needing "unsafe", but this is the minimal change for reassuring clippy. Found by clippy lint ptr_offset_with_cast: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_offset_with_cast
2021-09-30Be explicit about giving Ok() the unit valueLars Wirzenius
Instead of giving Ok() the return value of a function that returns no value, i.e., the unit value (), give it explicitly. This is clearer and easier to follow for the reader. Found by clippy lint unit_arg: https://rust-lang.github.io/rust-clippy/master/index.html#unit_arg
2021-09-30Avoid calling .map with function returning the unit typeLars Wirzenius
It seems clearer to write this with "if let". Found by clippy lint option_map_unit_fn: https://rust-lang.github.io/rust-clippy/master/index.html#option_map_unit_fn
2021-09-30Remove evaluation order dependenceLars Wirzenius
In this code: self.user_attributes.retain(|_| (keep[i], i += 1).0); it can be unclear to the reader that the increment of i actually happens before keep is indexed. Especially so for people who've been inflicted by C and its many surprising reasons for undefined behavior. It seems better to write this using an iterator. Found by clippy lint eval_order_dependence: https://rust-lang.github.io/rust-clippy/master/index.html#eval_order_dependence
2021-09-30Use as_derefLars Wirzenius
Instead of this: foo.as_ref().map(|x| x.as_slice()) do this: foo.as_deref() It's shorter and more to the point, and should thus be easier to understand. Found by clippy lint option_as_ref_deref: https://rust-lang.github.io/rust-clippy/master/index.html#option_as_ref_deref
2021-09-30Tell clippy it's OK not to implement traitsLars Wirzenius
Certain method names are typically provided by traits, and it can be confusing to readers when a method uses that name without the type implementing the trait. Mark the cases we have to tell clippy these cases are OK. Implementing the corresponding traits would have changed API so I opted not to do that. Found by clippy lint new_without_default: https://rust-lang.github.io/rust-clippy/master/index.html#new_without_default
2021-09-30Allow new() without default()Lars Wirzenius
It is customary in Rust to implement the Default trait for types that can have a new method that takes no arguments. However, that's not always wanted. I've marked all the structures that have a new without arguments but don't implement Default, so that if we get more of them, clippy will warn. Found by clippy lint new_without_default: https://rust-lang.github.io/rust-clippy/master/index.html#new_without_default
2021-09-30Allow ::new to not return SelfLars Wirzenius
It is Rust custom that the new method for a type returns an instance of that type. However, sometimes that's not wanted. Tell clippy that these cases are OK. I opted to not do this globally, because that would prevent clippy from catching future cases. Found by clippy warning new_ret_no_self: https://rust-lang.github.io/rust-clippy/master/index.html#new_ret_no_self
2021-09-30Add an is_empty method when len is thereLars Wirzenius
It is customary for Rust data container structures to have both is_empty and len, methods, because is_empty can both more to the point and also faster to implement. Sometimes is_empty makes no sense, as it doesn't for openpgp::types::Curve, which isn't a container structure. Arguably it would be better to rename Curve::len to something like Curve::bits, but as that would change the API, I've opted to tell clippy that not having is_empty in this case is OK. Found by clippy lint len_without_is_empty: https://rust-lang.github.io/rust-clippy/master/index.html#len_without_is_empty
2021-09-30Return a value without first assigning it to a variableLars Wirzenius
Found by clippy lint let_and_return: https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return
2021-09-30Drop unnecessary to_string() methodsLars Wirzenius
The methods were shadowing the implementation of the same function via the Display trait. One implementation is enough. Found by the clippy trait inherent_to_string_shadow_display: https://rust-lang.github.io/rust-clippy/master/index.html#inherent_to_string_shadow_display
2021-09-30Join nested if statements with logical and into one statementLars Wirzenius
Instead of this: if foo { if bar { ... } } do this: if foo && bar { ... } Nesting statements implies a more complicated code structure than it really is. Thus it's arguably simpler to write a combined condition by joining the two conditions with a logical and operation. Found by clippy lint collapsible_if: https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_if
2021-09-30Rename variable to be clearerLars Wirzenius
Instead of using a variable named foo, which usually a placeholder, use a more descriptive name.
2021-09-30Avoid redundant closuresLars Wirzenius
If a closure just passes the argument it gets to a function, the closure is usually redundant and you can just use the function instead of the closure. Thus, instead of this: iter().map(|x| foo(x)) you can write this: iter().map(foo) This is shorter and simpler. Sometimes it doesn't work and the closure is necessary. Such locations can be marked with `#[allow(clippy::redundant_closure)]`. Found by clippy lint redundant_closure: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure
2021-09-30Use .sort_unstable for speedLars Wirzenius
A stable sort is one where values that compare equal are in the same order relative to each other in the output as they were in the input. Thus, if input has values a0, b, and a1, with a0==a1, a0<b, a1<b, then the output of a stable sort is guaranteed to be be [a0, a1, b]. For an unstable sort, the output may also be [a1, a0, b]. This can matter if, for example, the values are structs and only one field is used for comparison. On data where an unstable sort (.sort_unstable) works, it's bad to use a stable sort (.sort), because it uses more CPU without affecting the end result. If the types of values in a vector are primitive types, as they are in the cases changed by this commit, an unstable sort will do just fine. Found by clippy lint stable_sort_primitive: https://rust-lang.github.io/rust-clippy/master/index.html#stable_sort_primitive
2021-09-30Replace .filter_map with just .filter when possibleLars Wirzenius
If the function or closure passed into .filter_map doesn't do any filtering or mapping. the method call can be replaced with a simple .map or .filter. The clippy lint suggests which. In this change, we can always replace .filter_map with .filter. We also need to change the closure to return a boolean value, since that's what .filter expects. Found by clippy lint unnecessary_filter_map: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_filter_map
2021-09-30Simplify how to create a vector of cloned valuesLars Wirzenius
Instead of this: something.iter().cloned().collect() do this: something.to_vec() It's shorter, simpler, and more to the point, and thus easier to understand. Found by clippy lint iter_cloned_collect: https://rust-lang.github.io/rust-clippy/master/index.html#iter_cloned_collect
2021-09-30Drop unnecessary main function from doc testLars Wirzenius
Test code embedded in document comments do not need to have a main function, the Rust test infrastructure provides one. Keeping the test code shorter is more idiomatic and shorter, both of which make it easier to understand. Found by clippy lint needless_doctest_main: https://rust-lang.github.io/rust-clippy/master/index.html#needless_doctest_main
2021-09-30Use .iter() instead of .into_iter()Lars Wirzenius
Just call the .iter() method instead of .into_iter() when either is OK. The shorter form is shorter and should thus be easier to read. Found by clippy lint into_iter_on_ref: https://rust-lang.github.io/rust-clippy/master/index.html#into_iter_on_ref
2021-09-30Simplify tests for OKLars Wirzenius
Instead of this: if let Some(()) = key.secret_mut().decrypt_in_place(algo, &p).ok() { ... } use this: if key.secret_mut().decrypt_in_place(algo, &p).is_ok() { ... } It's more to the point and easier to understand.
2021-09-30Use .find() in an iteratorLars Wirzenius
Instead of iter().filter(|x| pred(x)).next() use this: iter().find(|x| pred(x)) This is more to the point, and thus easier to understand. Found by clippy lint filter_next: https://rust-lang.github.io/rust-clippy/master/index.html#filter_next
2021-09-30Use .starts_with instead of .chars().next()Lars Wirzenius
Instead of this: name.chars().next() == Some('!') use this: name.starts_with('!') It's more to the point, and does not require the reader to reason about iterators and possible values returneed by .next(). Found by clippy lint chars_next_cmp: https://rust-lang.github.io/rust-clippy/master/index.html#chars_next_cmp
2021-09-30Use Result::map or ::map_err instead of ::and_thenLars Wirzenius
Instead of: foo().and_then(|x| Ok(x.transmogrify()) use this: foo().map(|x| x.transmogrify) because it's shorter and simpler to understand. Suggested by clippy lint bind_instead_of_map: https://rust-lang.github.io/rust-clippy/master/index.html#bind_instead_of_map
2021-09-30Drop pointless @_ match pattern bindingsLars Wirzenius
In a match arm, instead of binding the matched value to the name "_", just don't bind. Its shorter and easier to understand Found by clippy lint redundant_patter: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern
2021-09-30Express calculation for seconds-in-week more clearlyLars Wirzenius
Rather than using magic numbers, use names to express the various values.
2021-09-30Avoid naming field setting it from variable of the same nameLars Wirzenius
When creating a struct with a field foo, using a variable also named foo, it's not necessary to name the field explicitly. Thus, instead of: Self { foo: foo } use this: Self { foo } The shorter form is more idiomatic and thus less confusing to experienced Rust programmers. This was found by the clippy lint redundant_field_names: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names Sponsored-by: author
2021-09-30Disable clippy::same_item_pushLars Wirzenius
This code can probably be rewritten to not cause the warning, but as it's crypto code, I fear making a mistake, so I'm just disabling the warning here. Found by clippy lint same_item_push: https://rust-lang.github.io/rust-clippy/master/index.html#same_item_push
2021-09-30Use .cloned() on iterators, instead of .map(|x| x.clone())Lars Wirzenius
The dedicated method on iterators is shorter and more to the point, and should thus be easier to understand. Found by clippy lint map_clone: https://rust-lang.github.io/rust-clippy/master/index.html#map_clone
2021-09-30Simplify writing out a literal {}Lars Wirzenius
It's arguably simpler to write a format string that doesn't take arguments than one with an argument that looks like a format string. Found by clippy lint write_literal: https://rust-lang.github.io/rust-clippy/master/index.html#write_literal
2021-09-30Drop unnecessary mut when passing a reference to a functionLars Wirzenius
Reduce this: foo(&mut bar): into foo(&bar); when the function does not expect a mutable reference, just a read-only reference. The extra mut serves no function, but can confuse the reader. Found by clippy lint unnecessary_mut_passed: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_mut_passed
2021-09-30Drop unnecessary &mut on a sliceLars Wirzenius
Reduce &mut foo[...].copy_from_slice(bar); to just foo[...].copy_from_slice(bar); since the &mut does nothing except obfuscate the code. Found by clippy lint unnecessary_operation: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation
2021-09-30Drop unnecessary conversions with .into() to the same typeLars Wirzenius
The code used to do foo.into(), but the result was of the same type as foo already was. Thus the extra call is redundant and likely to confuse a reader. Found by clippy lint useless_conversion: https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion
2021-09-30Unpack value with if let instead of is_some/unwrapLars Wirzenius
The if let is more idiomatic and thus should be easier for Rust programmers to understand. It also avoids to implicit dependency between the if condition and the unwrap. Found by clippy lint unnecessary_unrap: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_unwrap
2021-09-30Simplify &foo == &bar into foo == barLars Wirzenius
This was found by clippy lint op_ref: https://rust-lang.github.io/rust-clippy/master/index.html#op_ref
2021-09-30Use the now-idiomatic option? syntaxLars Wirzenius
This works in Rust now: foo?; as a replacement for: if foo.is_none() { return None; } It's similar to ? for error handling and can only be used in functions that return an Option. The instance of this in a macro caused the problem to be reported a lot of time, once per time the macro was used, but luckily it can could be fixed in only one place. Magic! Found by clippy lint question_mark: https://rust-lang.github.io/rust-clippy/master/index.html#question_mark
2021-09-30Improve error message for a malformed packetLars Wirzenius
Suggested by Neal Walfield. Found by clippy lint useless_format: https://rust-lang.github.io/rust-clippy/master/index.html#useless_format
2021-09-30When returning an error, use "return" instead of "?"Lars Wirzenius
Instead of Err(...)?; which is correct but not idiomatic, write this: return Err(...); This was found by clippy lintt try_err: https://rust-lang.github.io/rust-clippy/master/index.html#try_err Sponsored-by: author
2021-09-30Annotate function so that clippy accepts nonminimal_boolLars Wirzenius
Found by clippy lint nonminimal_bool: https://rust-lang.github.io/rust-clippy/master/index.html#nonminimal_bool
2021-09-30Use std::mem::take instead of std::mem::replace, for clarityLars Wirzenius
Replace let bar = std::mem::replace(&foo, Default::Default()); with let bar = std::mem::take(&foo); The new version seems a little clearer. Found by clippy: https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_with_default Sponsored-by: author
2021-09-30Drop unnecessary return statementsLars Wirzenius
Instead of "return foo" as the last statement executed in a function, just use "foo", which is more idiomatic style. This was found by the clippy lint needless_return: https://rust-lang.github.io/rust-clippy/master/index.html#needless_return
2021-09-30Use .is_empty() for clarity, instead of .len() == 0Lars Wirzenius
This was found by clippy lint len_zero: https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy