Skip to content

Commit

Permalink
chore: replace some allow with expect and add reason
Browse files Browse the repository at this point in the history
The expect attribute is same as allow, but it will throw a warning if the allow is not necessary.
  • Loading branch information
wangl-cc committed Nov 16, 2024
1 parent 8b7b213 commit ee4b780
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 34 deletions.
10 changes: 8 additions & 2 deletions maa-cli/src/cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ pub trait PathProvider {
/// deleted. This method and `should_keep` determine whether an entry should be deleted.
/// If this method returns true and `should_keep` returns false, the entry will be deleted.
/// Otherwise, the entry will not be deleted.
#[allow(unused_variables)]
#[expect(
unused_variables,
reason = "This is default implementation, the variable may used by other implementations"
)]
fn should_delete(&self, entry: &DirEntry) -> bool {
true
}
Expand All @@ -33,7 +36,10 @@ pub trait PathProvider {
/// This method and `should_delete` determine whether an entry should be deleted.
/// If `should_delete` returns true and this method returns false, the entry will be deleted.
/// Otherwise, the entry will not be deleted.
#[allow(unused_variables)]
#[expect(
unused_variables,
reason = "This is default implementation, the variable may used by other implementations"
)]
fn should_keep(&self, entry: &DirEntry) -> bool {
false
}
Expand Down
7 changes: 3 additions & 4 deletions maa-cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use crate::{cleanup, config, log, run};

#[derive(Parser)]
#[command(name = "maa", author, version = env!("MAA_VERSION"), about = "A tool for Arknights.")]
#[allow(clippy::upper_case_acronyms)]
pub(crate) struct CLI {
pub(crate) struct Cli {
#[command(subcommand)]
pub(crate) command: Command,
/// Enable batch mode
Expand Down Expand Up @@ -300,12 +299,12 @@ pub(crate) enum Dir {
}

#[cfg(test)]
pub(crate) fn parse_from<I, T>(args: I) -> CLI
pub(crate) fn parse_from<I, T>(args: I) -> Cli
where
I: IntoIterator<Item = T>,
T: Into<std::ffi::OsString> + Clone,
{
CLI::parse_from(args)
Cli::parse_from(args)
}

#[cfg(test)]
Expand Down
34 changes: 21 additions & 13 deletions maa-cli/src/config/asst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,11 @@ impl ConnectionConfig {

#[cfg_attr(test, derive(Debug, PartialEq))]
#[derive(Default, Clone, Copy)]
#[allow(clippy::upper_case_acronyms)]
pub enum Preset {
MuMuPro,
PlayCover,
#[default]
ADB,
Adb,
}

impl<'de> Deserialize<'de> for Preset {
Expand All @@ -148,10 +147,10 @@ impl<'de> Deserialize<'de> for Preset {
match value {
"MuMuPro" => Ok(Preset::MuMuPro),
"PlayCover" | "PlayTools" => Ok(Preset::PlayCover),
"ADB" => Ok(Preset::ADB),
"ADB" | "Adb" | "adb" => Ok(Preset::Adb),
_ => {
warn!("Unknown connection preset: {}, ignoring", value);
Ok(Preset::ADB)
Ok(Preset::Adb)
}
}
}
Expand All @@ -166,15 +165,15 @@ impl Preset {
match self {
Preset::MuMuPro => "/Applications/MuMuPlayer.app/Contents/MacOS/MuMuEmulator.app/Contents/MacOS/tools/adb",
Preset::PlayCover => "",
Preset::ADB => "adb",
Preset::Adb => "adb",
}
}

fn default_address(self, adb_path: &str) -> Cow<'static, str> {
match self {
Preset::MuMuPro => "127.0.0.1:16384".into(),
Preset::PlayCover => "127.0.0.1:1717".into(),
Preset::ADB => std::process::Command::new(adb_path)
Preset::Adb => std::process::Command::new(adb_path)
.arg("devices")
.output()
.ok()
Expand Down Expand Up @@ -534,7 +533,7 @@ mod tests {

assert_eq!(config, AsstConfig {
connection: ConnectionConfig {
preset: Preset::ADB,
preset: Preset::Adb,
adb_path: Some(String::from("adb")),
address: Some(String::from("emulator-5554")),
config: Some(String::from("CompatMac")),
Expand Down Expand Up @@ -571,7 +570,7 @@ mod tests {

assert_de_tokens(
&ConnectionConfig {
preset: Preset::ADB,
preset: Preset::Adb,
..Default::default()
},
&[
Expand All @@ -584,7 +583,7 @@ mod tests {

assert_de_tokens(
&ConnectionConfig {
preset: Preset::ADB,
preset: Preset::Adb,
..Default::default()
},
&[
Expand All @@ -610,7 +609,7 @@ mod tests {

assert_de_tokens(
&ConnectionConfig {
preset: Preset::ADB,
preset: Preset::Adb,
adb_path: Some(String::from("/path/to/adb")),
address: Some(String::from("127.0.0.1:5555")),
config: Some(String::from("SomeConfig")),
Expand Down Expand Up @@ -743,6 +742,15 @@ mod tests {
);
}

#[test]
fn preset() {
assert_de_tokens(&Preset::Adb, &[Token::Str("ADB")]);
assert_de_tokens(&Preset::Adb, &[Token::Str("Adb")]);
assert_de_tokens(&Preset::Adb, &[Token::Str("adb")]);

assert_de_tokens(&Preset::MuMuPro, &[Token::Str("MuMuPro")]);
}

#[test]
fn asst_config() {
assert_de_tokens(
Expand Down Expand Up @@ -804,7 +812,7 @@ mod tests {
#[test]
fn default() {
assert_matches!(ConnectionConfig::default(), ConnectionConfig {
preset: Preset::ADB,
preset: Preset::Adb,
adb_path: None,
address: None,
config: None,
Expand All @@ -814,7 +822,7 @@ mod tests {
#[cfg(target_os = "macos")]
#[test]
fn preset() {
assert_eq!(ConnectionConfig::default().preset(), Preset::ADB);
assert_eq!(ConnectionConfig::default().preset(), Preset::Adb);

assert_eq!(
ConnectionConfig {
Expand Down Expand Up @@ -889,7 +897,7 @@ mod tests {

args_eq(
ConnectionConfig {
preset: Preset::ADB,
preset: Preset::Adb,
adb_path: Some("/path/to/adb".to_owned()),
address: Some("127.0.0.1:11111".to_owned()),
config: Some("SomeConfig".to_owned()),
Expand Down
8 changes: 4 additions & 4 deletions maa-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ mod value;
use anyhow::{Context, Result};
use clap::{CommandFactory, Parser};

use crate::command::{Command, Component, Dir, CLI};
use crate::command::{Cli, Command, Component, Dir};

fn main() -> Result<()> {
let cli = command::CLI::parse();
let cli = command::Cli::parse();

cli.log.init_logger()?;

Expand Down Expand Up @@ -120,15 +120,15 @@ fn main() -> Result<()> {
config_type,
} => config::import(&path, force, &config_type)?,
Command::Complete { shell } => {
clap_complete::generate(shell, &mut CLI::command(), "maa", &mut std::io::stdout());
clap_complete::generate(shell, &mut Cli::command(), "maa", &mut std::io::stdout());
}
Command::Init {
name,
format,
force,
} => config::init::init(name, format, force)?,
Command::Mangen { path } => {
clap_mangen::generate_to(CLI::command(), path)?;
clap_mangen::generate_to(Cli::command(), path)?;
}
}

Expand Down
16 changes: 8 additions & 8 deletions maa-dirs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ use dunce::canonicalize;
macro_rules! str_join {
($($e:expr),*) => {{
const LEN: usize = 0 $(+ $e.len())*;
#[allow(unused_assignments)]
#[expect(
unused_assignments,
reason = "The last assignment will not be used, but in macro, we can't avoid it."
)]
const BYTES: [u8; LEN] = {
let mut dest: [u8; LEN] = [0; LEN];
let mut offset = 0;
Expand Down Expand Up @@ -467,9 +470,7 @@ where
/// # Panics
///
/// Panics if the given str is a string containing path separator.
///
/// This function only called at compile time, so it's dead code in release build.
#[allow(dead_code)]
#[allow(dead_code, reason = "This function is only called at compile time")]
fn ensure_name(name: &str) -> &str {
assert!(
!name.contains(std::path::is_separator),
Expand Down Expand Up @@ -828,10 +829,9 @@ mod tests {

std::fs::File::create(&test_file).unwrap();

assert_eq!(
global_path([&test_dir1, &test_dir2], "test"),
vec![test_file.clone()]
);
assert_eq!(global_path([&test_dir1, &test_dir2], "test"), vec![
test_file.clone()
]);
assert_eq!(
global_path([&test_dir1, &test_dir2], "not_exist"),
Vec::<PathBuf>::new()
Expand Down
6 changes: 3 additions & 3 deletions maa-sys/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ macro_rules! link {
) => (
use libloading::{Library, Symbol};

#[allow(non_snake_case)]
#[expect(non_snake_case, reason = "FFI functions are named in PascalCase")]
struct SharedLibrary {
_handle: Library,
$(
Expand All @@ -33,7 +33,7 @@ macro_rules! link {
}

$(
#[allow(non_snake_case)]
#[allow(non_snake_case, reason = "FFI functions are named in PascalCase")]
pub fn $name(&self, $($pname: $pty), *) $(-> $ret)* {
(self.$name)($($pname), *)
}
Expand Down Expand Up @@ -73,7 +73,7 @@ macro_rules! link {
/// # Panics
///
/// This function will panic if the shared library is not loaded in this thread.
#[allow(non_snake_case)]
#[allow(non_snake_case, reason = "FFI functions are named in PascalCase")]
pub unsafe fn $name($($pname: $pty), *) $(-> $ret)* {
match SHARED_LIBRARY.read().expect("Failed to lock shared library").as_ref() {
Some(lib) => lib.$name($($pname), *),
Expand Down

0 comments on commit ee4b780

Please sign in to comment.