Skip to content

Commit 6f18164

Browse files
committed
ci: fix lint
1 parent 85b43d6 commit 6f18164

File tree

9 files changed

+33
-34
lines changed

9 files changed

+33
-34
lines changed

.github/actions/lint/action.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ runs:
99

1010
- name: Clippy
1111
shell: bash
12+
# You need to run 'cargo clippy -r' in the local to get the same output with CI
1213
run: cargo clippy -- -D warnings

src/api/auth/session.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn save_pat(pat: &str, path: &Path) -> Result<()> {
2020
}
2121

2222
fn get_cfg_path() -> Result<PathBuf> {
23-
let home = home_dir().ok_or(anyhow!("Can not get home dir"))?;
23+
let home = home_dir().ok_or_else(|| anyhow!("Can not get home dir"))?;
2424
home.join(".cnbrc").into_ok()
2525
}
2626

src/api/ing/mod.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ pub struct Ing {
1414
}
1515

1616
impl Ing {
17-
pub fn new(pat: String) -> Ing {
18-
Ing { pat }
17+
pub const fn new(pat: String) -> Self {
18+
Self { pat }
1919
}
2020
}
2121

@@ -48,14 +48,14 @@ impl TryFrom<usize> for IngSendFrom {
4848

4949
fn try_from(value: usize) -> Result<Self, Self::Error> {
5050
match value {
51-
0 => IngSendFrom::None,
52-
1 => IngSendFrom::Ms,
53-
2 => IngSendFrom::GTalk,
54-
3 => IngSendFrom::Qq,
55-
5 => IngSendFrom::Sms,
56-
6 => IngSendFrom::CellPhone,
57-
8 => IngSendFrom::Web,
58-
9 => IngSendFrom::Code,
51+
0 => Self::None,
52+
1 => Self::Ms,
53+
2 => Self::GTalk,
54+
3 => Self::Qq,
55+
5 => Self::Sms,
56+
6 => Self::CellPhone,
57+
8 => Self::Web,
58+
9 => Self::Code,
5959
u => bail!("Unknown value of ing source: {}", u),
6060
}
6161
.into_ok()
@@ -76,12 +76,13 @@ pub fn fmt_content(content: &str) -> String {
7676
static ref REGEX: Regex =
7777
Regex::new(r#"<a.*href="https://home.cnblogs.com/u/.*?".*>(@.*?)</a>"#).unwrap();
7878
}
79-
if let Some(caps) = REGEX.captures(content) {
80-
let at_user = caps.get(1).unwrap().as_str();
81-
REGEX.replace(content, at_user).to_string()
82-
} else {
83-
content.to_string()
84-
}
79+
REGEX.captures(content).map_or_else(
80+
|| content.to_string(),
81+
|caps| {
82+
let at_user = caps.get(1).unwrap().as_str();
83+
REGEX.replace(content, at_user).to_string()
84+
},
85+
)
8586
}
8687

8788
pub fn rm_ing_at_user_tag(text: &str) -> String {
@@ -97,9 +98,8 @@ pub fn get_ing_at_user_tag_text(text: &str) -> String {
9798
static ref REGEX: Regex =
9899
Regex::new(r#"<a.*href="https://home.cnblogs.com/u/.*?".*>@(.*?)</a>:"#).unwrap();
99100
}
100-
if let Some(caps) = REGEX.captures(text) {
101-
caps.get(1).unwrap().as_str().to_string()
102-
} else {
103-
"".to_string()
104-
}
101+
REGEX.captures(text).map_or_else(
102+
|| "".to_string(),
103+
|caps| caps.get(1).unwrap().as_str().to_string(),
104+
)
105105
}

src/api/post/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct Post {
1212
}
1313

1414
impl Post {
15-
pub fn new(pat: String) -> Post {
16-
Post { pat }
15+
pub const fn new(pat: String) -> Self {
16+
Self { pat }
1717
}
1818
}

src/api/user/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub struct User {
55
}
66

77
impl User {
8-
pub fn new(pat: String) -> User {
9-
User { pat }
8+
pub const fn new(pat: String) -> Self {
9+
Self { pat }
1010
}
1111
}

src/args/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn get_pat(pat: &Option<String>) -> Result<String> {
1515
pat.clone().or_eval_result(session::get_pat)
1616
}
1717

18-
pub fn no_operation(args: &Args) -> bool {
18+
pub const fn no_operation(args: &Args) -> bool {
1919
matches!(
2020
args,
2121
Args {
@@ -97,7 +97,7 @@ pub fn login(args: &Args) -> Option<&String> {
9797
.into_some()
9898
}
9999

100-
pub fn logout(args: &Args) -> bool {
100+
pub const fn logout(args: &Args) -> bool {
101101
matches!(
102102
args,
103103
Args {

src/display/normal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn list_ing(ing_list: &[(IngEntry, Vec<IngCommentEntry>)], rev: bool) {
4949
let star_text = ing_star_tag_to_text(&ing.icons);
5050
print!(" {}⭐", star_text);
5151
}
52-
println!(" # {}", ing.id.to_string());
52+
println!(" # {}", ing.id);
5353
let content = fmt_content(&ing.content);
5454
println!(" {}: {}", ing.user_name, content);
5555

@@ -123,7 +123,7 @@ pub fn show_post_meta(entry: &PostEntry) -> Result<()> {
123123
pub fn list_post(entry_list: &[PostEntry], total_count: usize, rev: bool) {
124124
println!("{}/{}", entry_list.len(), total_count);
125125
entry_list.iter().dyn_rev(rev).for_each(|entry| {
126-
print!("# {}", entry.id.to_string());
126+
print!("# {}", entry.id);
127127
print!(" {}", entry.title);
128128
if entry.is_published {
129129
print!(" Pub");

src/infra/option.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ impl<T> OptionExt<T> for Option<T> {
2222
where
2323
F: FnOnce() -> Result<T, E>,
2424
{
25-
match self {
26-
Some(val) => Ok(val),
27-
_ => f(),
28-
}
25+
self.map_or_else(f, |val| Ok(val))
2926
}
3027
}

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![feature(let_chains)]
44
#![feature(type_name_of_val)]
55
#![feature(iterator_try_collect)]
6+
#![warn(clippy::all, clippy::nursery, clippy::cargo_common_metadata)]
67

78
use crate::api::auth::session;
89
use crate::api::ing::{Ing, IngType};

0 commit comments

Comments
 (0)