Skip to content

Commit f433db3

Browse files
committed
count clippy fixes
1 parent 2edf7d6 commit f433db3

File tree

2 files changed

+60
-24
lines changed

2 files changed

+60
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## Change Log
2-
- [ ] Count fixes
2+
- [x] Count clippy fixes
33
- [x] Cache computed data by serialized/deserialize diff hunks
44
- [x] Print pair pragma with ## so it is easier to differentiate it with the pragma inside warning hints
55
- [x] Swap the order of diagnostic folder name from `$id/diagnostics` to `diagnostics/$id`

src/main.rs

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,7 @@ fn to_diagnostic(map: &mut BTreeMap<String, Vec<Warning>>, args: Vec<String>) {
219219
let json_filename = format!("{diagnostics_folder}/diagnostics.json");
220220
let p = std::path::Path::new(json_filename.as_str());
221221
if !p.exists() {
222-
let mut cargo = "cargo";
223-
if std::path::Path::new("x.py").exists() {
224-
cargo = "./x.py";
225-
} else if std::path::Path::new("miri").exists() {
226-
cargo = "./miri";
227-
}
222+
let cargo = get_cargo();
228223
if let Ok(mut command) = Command::new(cargo)
229224
.args(args)
230225
.stdout(Stdio::piped())
@@ -289,6 +284,16 @@ fn to_diagnostic(map: &mut BTreeMap<String, Vec<Warning>>, args: Vec<String>) {
289284
}
290285
}
291286

287+
fn get_cargo() -> &'static str {
288+
let mut cargo = "cargo";
289+
if std::path::Path::new("x.py").exists() {
290+
cargo = "./x.py";
291+
} else if std::path::Path::new("miri").exists() {
292+
cargo = "./miri";
293+
}
294+
cargo
295+
}
296+
292297
fn get_folder() -> String {
293298
let mut folder = ".".to_string();
294299
let v = get_args();
@@ -389,6 +394,35 @@ fn diagnose_all_warnings(flags: Vec<String>) -> BTreeMap<String, Vec<Warning>> {
389394
map
390395
}
391396

397+
fn count(map: BTreeMap<String, Vec<Warning>>) -> usize
398+
{
399+
let mut sum: usize = 0;
400+
map.iter().for_each(|(k,v)|{
401+
sum = sum.saturating_add(v.len());
402+
});
403+
sum
404+
}
405+
406+
fn clippy_fix() -> usize
407+
{
408+
let folder = get_folder();
409+
let manifest = format!("{folder}/Cargo.toml");
410+
let args = vec![
411+
"clippy".to_string(),
412+
"--message-format=json".to_string(),
413+
"--manifest-path".to_string(),
414+
manifest,
415+
"--fix".to_string(),
416+
"--allow-dirty".to_string(),
417+
"--allow-no-vcs".to_string(),
418+
"--broken-code".to_string(),
419+
"--".to_string(),
420+
];
421+
let mut map: BTreeMap<String, Vec<Warning>> = BTreeMap::new();
422+
to_diagnostic(&mut map, args);
423+
count(map)
424+
}
425+
392426
// run the following bash commands
393427
// ```bash
394428
// git checkout $commit_id
@@ -502,8 +536,10 @@ fn fprint_warning_count(file: String, all_warnings: BTreeMap<String, Vec<Warning
502536
all_warnings.iter().for_each(|(_k, v)| {
503537
count = count.saturating_add(v.len());
504538
});
539+
let remained = clippy_fix();
505540
if let Ok(mut file) = open_file_to_write(file) {
506-
file.write_all(format!("There are {} warnings in {} files.\n", count, all_warnings.len()).as_bytes()).ok();
541+
file.write_all(format!("There are {} warnings in {} files, {} has been fixed.\n",
542+
count, all_warnings.len(), count.saturating_sub(remained)).as_bytes()).ok();
507543
}
508544
}
509545

@@ -1177,7 +1213,7 @@ fn main() {
11771213
let diagnostics_folder = get_diagnostics_folder();
11781214
if let Ok(s) = read_to_string(format!("{diagnostics_folder}/diagnostics.log")) {
11791215
assert_eq!( s,
1180-
r###"There are 1 warnings in 1 files.
1216+
r###"There are 1 warnings in 1 files, 0 has been fixed.
11811217
##[Warning(clippy::unwrap_used)
11821218
@@ -3,2 +3,3 @@ fn main() {
11831219
- let s = std::fs::read_to_string("Cargo.toml").unwrap();
@@ -1240,7 +1276,7 @@ fn main() {
12401276
if let Ok(s) = read_to_string(format!("{diagnostics_folder}/diagnostics.log")) {
12411277
assert_eq!(
12421278
s,
1243-
r###"There are 1 warnings in 1 files.
1279+
r###"There are 1 warnings in 1 files, 0 has been fixed.
12441280
##[Warning(clippy::unwrap_used)
12451281
@@ -3,2 +3,3 @@ fn main() {
12461282
let s = std::fs::read_to_string("Cargo.toml").unwrap();
@@ -1386,7 +1422,7 @@ fn main() {
13861422
}
13871423
}
13881424
"#,
1389-
r###"There are 1 warnings in 1 files.
1425+
r###"There are 1 warnings in 1 files, 0 has been fixed.
13901426
##[Warning(clippy::unwrap_used)
13911427
fn main() {
13921428
@@ -1430,7 +1466,7 @@ fn main() {
14301466
}
14311467
}
14321468
"#,
1433-
r###"There are 1 warnings in 1 files.
1469+
r###"There are 1 warnings in 1 files, 0 has been fixed.
14341470
##[Warning(clippy::unwrap_used)
14351471
fn main() {
14361472
@@ -1470,7 +1506,7 @@ fn main() {
14701506
}
14711507
}
14721508
"#,
1473-
r###"There are 1 warnings in 1 files.
1509+
r###"There are 1 warnings in 1 files, 0 has been fixed.
14741510
##[Warning(clippy::unwrap_used)
14751511
fn main() {
14761512
@@ -1511,7 +1547,7 @@ fn main() {
15111547
}
15121548
}
15131549
"#,
1514-
r###"There are 1 warnings in 1 files.
1550+
r###"There are 1 warnings in 1 files, 0 has been fixed.
15151551
##[Warning(clippy::unwrap_used)
15161552
fn main() {
15171553
@@ -1576,7 +1612,7 @@ fn main() {
15761612
if let Ok(s) = read_to_string(format!("{diagnostics_folder}/diagnostics.log")) {
15771613
assert_eq!(
15781614
s,
1579-
r###"There are 1 warnings in 1 files.
1615+
r###"There are 1 warnings in 1 files, 0 has been fixed.
15801616
"###
15811617
);
15821618
}
@@ -1699,7 +1735,7 @@ fn main() {
16991735
"2468ad1e3c0183f4a94859bcc5cea04ee3fc4ab1",
17001736
rd_run
17011737
),
1702-
"There are 30 warnings in 1 files.\n"
1738+
"There are 30 warnings in 1 files, 0 has been fixed.\n"
17031739
);
17041740
}
17051741

@@ -1711,7 +1747,7 @@ fn main() {
17111747
patch: Some("375981bb06cf819332c202cdd09d5a8c48e296db".to_string()),
17121748
flags: vec![], confirm: true, pair: false, function: false, single: true, location: false, mixed: false},
17131749
"512236bac29f09ca798c93020ce377c30a4ed2a5", rd_run), @r###"
1714-
There are 30 warnings in 1 files.
1750+
There are 30 warnings in 1 files, 0 has been fixed.
17151751
##[Warning(clippy::len_zero)
17161752
@@ -107 +107 @@ fn remove_previously_generated_files() {
17171753
- if output.len() != 0 {
@@ -1721,7 +1757,7 @@ fn main() {
17211757
patch: Some("375981bb06cf819332c202cdd09d5a8c48e296db".to_string()),
17221758
flags: vec![], confirm: true, pair: true, function: false, single: true, location: false, mixed: false},
17231759
"512236bac29f09ca798c93020ce377c30a4ed2a5", rd_run), @r###"
1724-
There are 30 warnings in 1 files.
1760+
There are 30 warnings in 1 files, 0 has been fixed.
17251761
##[Warning(clippy::len_zero)
17261762
@@ -107 +107 @@ fn remove_previously_generated_files() {
17271763
if output.len() != 0 {
@@ -1732,7 +1768,7 @@ fn main() {
17321768
patch: Some("375981bb06cf819332c202cdd09d5a8c48e296db".to_string()),
17331769
flags: vec![], confirm: true, pair: true, function: true, single: true, location: false, mixed: false},
17341770
"512236bac29f09ca798c93020ce377c30a4ed2a5", rd_run), @r###"
1735-
There are 30 warnings in 1 files.
1771+
There are 30 warnings in 1 files, 0 has been fixed.
17361772
##[Warning(clippy::len_zero)
17371773
fn remove_previously_generated_files() {
17381774
let command = Command::new("find")
@@ -1788,7 +1824,7 @@ fn main() {
17881824
patch: Some("375981bb06cf819332c202cdd09d5a8c48e296db".to_string()),
17891825
flags: vec![], confirm: true, pair: false, function: false, single: true, location: false, mixed: false},
17901826
"512236bac29f09ca798c93020ce377c30a4ed2a5", rd_run), @r###"
1791-
There are 30 warnings in 1 files.
1827+
There are 30 warnings in 1 files, 0 has been fixed.
17921828
##[Warning(clippy::len_zero)
17931829
@@ -107 +107 @@ fn remove_previously_generated_files() {
17941830
- if output.len() != 0 {
@@ -1798,7 +1834,7 @@ fn main() {
17981834
patch: Some("375981bb06cf819332c202cdd09d5a8c48e296db".to_string()),
17991835
flags: vec![], confirm: true, pair: true, function: false, single: true, location: false, mixed: false},
18001836
"512236bac29f09ca798c93020ce377c30a4ed2a5", rd_run), @r###"
1801-
There are 30 warnings in 1 files.
1837+
There are 30 warnings in 1 files, 0 has been fixed.
18021838
##[Warning(clippy::len_zero)
18031839
@@ -107 +107 @@ fn remove_previously_generated_files() {
18041840
if output.len() != 0 {
@@ -1809,7 +1845,7 @@ fn main() {
18091845
patch: Some("375981bb06cf819332c202cdd09d5a8c48e296db".to_string()),
18101846
flags: vec![], confirm: true, pair: true, function: true, single: true, location: false, mixed: false},
18111847
"512236bac29f09ca798c93020ce377c30a4ed2a5", rd_run), @r###"
1812-
There are 30 warnings in 1 files.
1848+
There are 30 warnings in 1 files, 0 has been fixed.
18131849
##[Warning(clippy::len_zero)
18141850
fn remove_previously_generated_files() {
18151851
let command = Command::new("find")
@@ -1866,13 +1902,13 @@ fn main() {
18661902
patch: Some("035ef892fa57fe644ef76065849ebd025869614d".to_string()),
18671903
flags: vec![], confirm: false, pair: false, function: false, single: true, location: false, mixed: false},
18681904
"375981bb06cf819332c202cdd09d5a8c48e296db", rd_run), @r###"
1869-
There are 27 warnings in 1 files.
1905+
There are 27 warnings in 1 files, 0 has been fixed.
18701906
"###);
18711907
insta::assert_snapshot!(rd_setup(temp_dir.clone(), Args { folder: Some(temp_dir),
18721908
patch: Some("035ef892fa57fe644ef76065849ebd025869614d".to_string()),
18731909
flags: vec![], confirm: true, pair: true, function: true, single: true, location: false, mixed: false},
18741910
"375981bb06cf819332c202cdd09d5a8c48e296db", rd_run), @r###"
1875-
There are 27 warnings in 1 files.
1911+
There are 27 warnings in 1 files, 0 has been fixed.
18761912
"###);
18771913
}
18781914

0 commit comments

Comments
 (0)