Skip to content

Commit 4008f63

Browse files
authored
[ci skip] fix: cargo clippy (#507)
# clippy fixes I think these fixes improves code quality. > [!NOTE] > `unreadable_literal` might also be usable, but I've excluded it for now. command used: ```sh cargo clippy -- \ -W clippy::pedantic \ -A clippy::cast_sign_loss \ -A clippy::cast_possible_wrap \ -A clippy::similar_names \ -A clippy::must_use_candidate \ -A clippy::return_self_not_must_use \ -A clippy::cast_possible_truncation \ -A clippy::default_trait_access \ -A clippy::struct_excessive_bools \ -A clippy::cast_lossless \ -A clippy::wildcard_imports \ -A clippy::cast_precision_loss \ -A clippy::module_name_repetitions \ -A clippy::unreadable_literal ```
2 parents 6724099 + b08d9c7 commit 4008f63

30 files changed

+164
-181
lines changed

build.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
use std::env;
2-
use std::process::Command;
1+
use std::{env, process::Command};
32

43
fn main() {
54
let version = env!("CARGO_PKG_VERSION");
65

76
if Ok("release".to_owned()) == env::var("PROFILE") {
8-
println!("cargo:rustc-env=COTP_VERSION={}", version);
7+
println!("cargo:rustc-env=COTP_VERSION={version}");
98
} else {
109
// Suffix with -DEBUG
1110
// If we can get the last commit hash, let's append that also
1211
if let Some(last_commit) = get_last_commit() {
13-
println!(
14-
"cargo:rustc-env=COTP_VERSION={}-DEBUG-{}",
15-
version, last_commit
16-
);
12+
println!("cargo:rustc-env=COTP_VERSION={version}-DEBUG-{last_commit}");
1713
} else {
18-
println!("cargo:rustc-env=COTP_VERSION={}-DEBUG", version);
14+
println!("cargo:rustc-env=COTP_VERSION={version}-DEBUG");
1915
}
2016
}
2117
}
@@ -27,5 +23,5 @@ fn get_last_commit() -> Option<String> {
2723
.ok()
2824
.filter(|e| e.status.success())
2925
.map(|e| String::from_utf8(e.stdout))
30-
.and_then(|e| e.ok())
26+
.and_then(Result::ok)
3127
}

src/arguments/export.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub struct ExportFormat {
3737
#[arg(short, long = "otp-uri")]
3838
pub otp_uri: bool,
3939

40-
/// Export into the FreeOTP+ database format
40+
/// Export into the `FreeOTP`+ database format
4141
#[arg(short, long = "freeotp-plus")]
4242
pub freeotp_plus: bool,
4343
}

src/arguments/extract.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ impl SubcommandExecutor for ExtractArgs {
3030
.elements
3131
.iter()
3232
.enumerate()
33-
.find(|(index, code)| filter_extract(&self, index, code))
33+
.find(|(index, code)| filter_extract(&self, *index, code))
3434
.map(|(_, code)| code);
3535

3636
if let Some(otp) = first_with_filters {
3737
let code = otp.get_otp_code()?;
38-
println!("{}", code);
38+
println!("{code}");
3939
if self.copy_to_clipboard {
4040
let _ = clipboard::copy_string_to_clipboard(code.as_str())?;
4141
println!("Copied to clipboard");
@@ -47,8 +47,8 @@ impl SubcommandExecutor for ExtractArgs {
4747
}
4848
}
4949

50-
fn filter_extract(args: &ExtractArgs, index: &usize, code: &OTPElement) -> bool {
51-
let match_by_index = args.index.map_or(true, |i| i == *index);
50+
fn filter_extract(args: &ExtractArgs, index: usize, code: &OTPElement) -> bool {
51+
let match_by_index = args.index.map_or(true, |i| i == index);
5252

5353
let match_by_issuer = args.issuer.as_ref().map_or(true, |issuer| {
5454
code.issuer.to_lowercase() == issuer.to_lowercase()

src/arguments/import.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ pub struct BackupType {
4444
#[arg(short = 'k', long = "aegis-encrypted")]
4545
pub aegis_encrypted: bool,
4646

47-
/// Import from FreeOTP+ backup
47+
/// Import from `FreeOTP`+ backup
4848
#[arg(short, long = "freeotp-plus")]
4949
pub freeotp_plus: bool,
5050

51-
/// Import from FreeOTP backup
51+
/// Import from `FreeOTP` backup
5252
#[arg(short = 'r', long)]
5353
pub freeotp: bool,
5454

@@ -60,7 +60,7 @@ pub struct BackupType {
6060
#[arg(short = 't', long)]
6161
pub authy: bool,
6262

63-
/// Import from Authy Database exported following this guide https://gist.github.com/gboudreau/94bb0c11a6209c82418d01a59d958c93
63+
/// Import from Authy Database exported following this guide <https://gist.github.com/gboudreau/94bb0c11a6209c82418d01a59d958c93>
6464
#[arg(short = 'u', long = "authy-exported")]
6565
pub authy_exported: bool,
6666

src/arguments/list.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl SubcommandExecutor for ListArgs {
6464
let json_elements = otp_database
6565
.elements
6666
.iter()
67-
.map(|element| element.try_into())
67+
.map(TryInto::try_into)
6868
.collect::<Result<Vec<JsonOtpList>>>()?;
6969

7070
let stringified = serde_json::to_string_pretty(&json_elements)
@@ -104,13 +104,13 @@ impl SubcommandExecutor for ListArgs {
104104
.repeat(issuer_width - NO_ISSUER_TEXT.chars().count())
105105
.as_str()
106106
} else {
107-
e.issuer.to_owned()
107+
e.issuer.clone()
108108
+ " ".repeat(issuer_width - e.issuer.chars().count()).as_str()
109109
},
110-
e.label.to_owned()
110+
e.label.clone()
111111
+ " ".repeat(label_width - e.label.chars().count()).as_str(),
112112
e.get_otp_code().unwrap_or("ERROR".to_string())
113-
)
113+
);
114114
});
115115
}
116116

src/arguments/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,5 @@ pub fn args_parser(matches: CotpArgs, read_result: OTPDatabase) -> color_eyre::R
7272
#[test]
7373
fn verify_cli() {
7474
use clap::CommandFactory;
75-
CotpArgs::command().debug_assert()
75+
CotpArgs::command().debug_assert();
7676
}

src/crypto/cryptography.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn gen_salt() -> color_eyre::Result<[u8; ARGON2ID_SALT_LENGTH]> {
3131
}
3232

3333
pub fn encrypt_string_with_key(
34-
plain_text: String,
34+
plain_text: &str,
3535
key: &Vec<u8>,
3636
salt: &[u8],
3737
) -> color_eyre::Result<EncryptedDatabase> {
@@ -93,8 +93,7 @@ mod tests {
9393
fn test_encryption() {
9494
let salt = gen_salt().unwrap();
9595
let key = argon_derive_key(b"pa$$w0rd", salt.as_ref()).unwrap();
96-
let encrypted =
97-
encrypt_string_with_key(String::from("Secret data@#[]ò"), &key, salt.as_ref()).unwrap();
96+
let encrypted = encrypt_string_with_key("Secret data@#[]ò", &key, salt.as_ref()).unwrap();
9897
let (decrypted, _key, _salt) =
9998
decrypt_string(&serde_json::to_string(&encrypted).unwrap(), "pa$$w0rd").unwrap();
10099
assert_eq!(String::from("Secret data@#[]ò"), decrypted);

src/exporters/freeotp_plus.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ impl TryFrom<&OTPDatabase> for FreeOTPPlusJson {
1212
otp_database
1313
.elements
1414
.iter()
15-
.map(|e| e.try_into())
15+
.map(TryInto::try_into)
1616
.collect::<Result<Vec<FreeOTPElement>, ErrReport>>()
1717
.map(FreeOTPPlusJson::new)
1818
}
@@ -22,19 +22,19 @@ impl TryFrom<&OTPElement> for FreeOTPElement {
2222
type Error = ErrReport;
2323
fn try_from(otp_element: &OTPElement) -> Result<Self, Self::Error> {
2424
Ok(FreeOTPElement {
25-
secret: decode_secret(otp_element.secret.clone())?,
25+
secret: decode_secret(&otp_element.secret)?,
2626
algo: otp_element.algorithm.to_string(),
2727
counter: otp_element.counter.unwrap_or(0),
2828
digits: otp_element.digits,
2929
issuer_ext: otp_element.issuer.clone(),
30-
_label: otp_element.label.clone(),
30+
label: otp_element.label.clone(),
3131
period: otp_element.period,
32-
_type: otp_element.type_.to_string(),
32+
r#type: otp_element.type_.to_string(),
3333
})
3434
}
3535
}
3636

37-
fn decode_secret(secret: String) -> Result<Vec<i8>> {
37+
fn decode_secret(secret: &str) -> Result<Vec<i8>> {
3838
BASE32_NOPAD
3939
.decode(secret.as_bytes())
4040
.map(|v| v.into_iter().map(|n| n as i8).collect::<Vec<i8>>())

src/exporters/otp_uri.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ pub struct OtpUriList {
88

99
impl<'a> From<&'a OTPDatabase> for OtpUriList {
1010
fn from(value: &'a OTPDatabase) -> Self {
11-
let items: Vec<String> = value.elements.iter().map(|e| e.get_otpauth_uri()).collect();
11+
let items: Vec<String> = value
12+
.elements
13+
.iter()
14+
.map(super::super::otp::otp_element::OTPElement::get_otpauth_uri)
15+
.collect();
1216

1317
OtpUriList { items }
1418
}

src/importers/aegis.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ pub(crate) struct AegisDb {
2323

2424
#[derive(Serialize, Deserialize)]
2525
struct AegisElement {
26-
#[serde(rename = "type")]
27-
_type: String,
26+
r#type: String,
2827
//uuid: String,
2928
name: String,
3029
issuer: String,
@@ -39,7 +38,7 @@ impl From<AegisElement> for OTPElement {
3938
issuer: value.issuer,
4039
label: value.name,
4140
digits: value.info.digits,
42-
type_: OTPType::from(value._type.as_str()),
41+
type_: OTPType::from(value.r#type.as_str()),
4342
algorithm: OTPAlgorithm::from(value.info.algo.as_str()),
4443
period: value.info.period.unwrap_or(30),
4544
counter: value.info.counter,
@@ -52,7 +51,7 @@ impl TryFrom<AegisDb> for Vec<OTPElement> {
5251
type Error = String;
5352

5453
fn try_from(aegis_db: AegisDb) -> Result<Self, Self::Error> {
55-
Ok(aegis_db.entries.into_iter().map(|e| e.into()).collect())
54+
Ok(aegis_db.entries.into_iter().map(Into::into).collect())
5655
}
5756
}
5857

0 commit comments

Comments
 (0)