Skip to content

Commit 676e913

Browse files
committed
Auto merge of #6829 - matthiaskrgr:lintcheck_unclippy, r=<try>
lintcheck: clippy fixes, test on ci fixes clippy warnings in lintcheck and adds a small test (run on 3 small crates) to gha ci changelog: none
2 parents ece3543 + d2a9917 commit 676e913

File tree

3 files changed

+110
-62
lines changed

3 files changed

+110
-62
lines changed

.github/workflows/clippy_dev.yml

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ on:
88
pull_request:
99
# Only run on paths, that get checked by the clippy_dev tool
1010
paths:
11-
- 'CHANGELOG.md'
12-
- 'README.md'
13-
- '**.stderr'
14-
- '**.rs'
11+
- "CHANGELOG.md"
12+
- "README.md"
13+
- "**.stderr"
14+
- "**.rs"
1515

1616
env:
1717
RUST_BACKTRACE: 1
@@ -21,35 +21,49 @@ jobs:
2121
runs-on: ubuntu-latest
2222

2323
steps:
24-
# Setup
25-
- name: Checkout
26-
uses: actions/[email protected]
27-
28-
- name: remove toolchain file
29-
run: rm rust-toolchain
30-
31-
- name: rust-toolchain
32-
uses: actions-rs/[email protected]
33-
with:
34-
toolchain: nightly
35-
target: x86_64-unknown-linux-gnu
36-
profile: minimal
37-
components: rustfmt
38-
default: true
39-
40-
# Run
41-
- name: Build
42-
run: cargo build --features deny-warnings
43-
working-directory: clippy_dev
44-
45-
- name: Test limit_stderr_length
46-
run: cargo dev limit_stderr_length
47-
48-
- name: Test update_lints
49-
run: cargo dev update_lints --check
50-
51-
- name: Test fmt
52-
run: cargo dev fmt --check
24+
# Setup
25+
- name: Checkout
26+
uses: actions/[email protected]
27+
28+
# To test formatting, we need to temporarily disable the rust-toolchain file
29+
- name: remove toolchain file
30+
run: rm rust-toolchain
31+
32+
- name: rust-toolchain
33+
uses: actions-rs/[email protected]
34+
with:
35+
toolchain: nightly
36+
target: x86_64-unknown-linux-gnu
37+
profile: minimal
38+
components: rustfmt
39+
default: true
40+
41+
- name: Test fmt
42+
run: cargo dev fmt --check
43+
44+
# Restore the rust-toolchain-file
45+
- name: Restore rust-toolchain-file
46+
run: git checkout rust-toolchain
47+
48+
# Run
49+
- name: Build
50+
run: cargo build --features deny-warnings
51+
working-directory: clippy_dev
52+
53+
- name: Test limit_stderr_length
54+
run: cargo dev limit_stderr_length
55+
56+
- name: Test update_lints
57+
run: cargo dev update_lints --check
58+
59+
# Lintcheck
60+
- name: build lintcheck
61+
run: cargo build --features lintcheck,deny-warnings
62+
working-directory: clippy_dev
63+
64+
- name: test lintcheck
65+
run: cargo test --features lintcheck,deny-warnings
66+
working-directory: clippy_dev
5367

5468
# These jobs doesn't actually test anything, but they're only used to tell
5569
# bors the build completed, as there is no practical way to detect when a

clippy_dev/ci_test_sources.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[crates]
2+
cc = {name = "cc", versions = ['1.0.67']}
3+
home = {name = "home", git_url = "https://github.com/brson/home", git_hash = "32044e53dfbdcd32bafad3109d1fbab805fc0f40"}
4+
rustc_tools_util = {name = "rustc_tools_util", versions = ['0.2.0']}

clippy_dev/src/lintcheck.rs

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
// positives.
66

77
#![cfg(feature = "lintcheck")]
8-
#![allow(clippy::filter_map)]
8+
#![allow(clippy::filter_map, clippy::collapsible_else_if)]
99

1010
use crate::clippy_project_root;
1111

1212
use std::collections::HashMap;
1313
use std::process::Command;
1414
use std::sync::atomic::{AtomicUsize, Ordering};
15-
use std::{env, fmt, fs::write, path::PathBuf};
15+
use std::{
16+
env, fmt,
17+
fs::write,
18+
path::{Path, PathBuf},
19+
};
1620

1721
use clap::ArgMatches;
1822
use rayon::prelude::*;
@@ -196,11 +200,9 @@ impl CrateSource {
196200
if !crate_root.exists() {
197201
println!("Copying {} to {}", path.display(), copy_dest.display());
198202

199-
dir::copy(path, &copy_dest, &dir::CopyOptions::new()).expect(&format!(
200-
"Failed to copy from {}, to {}",
201-
path.display(),
202-
crate_root.display()
203-
));
203+
dir::copy(path, &copy_dest, &dir::CopyOptions::new()).unwrap_or_else(|_| {
204+
panic!("Failed to copy from {}, to {}", path.display(), crate_root.display())
205+
});
204206
} else {
205207
println!(
206208
"Not copying {} to {}, destination already exists",
@@ -225,7 +227,7 @@ impl Crate {
225227
/// issued
226228
fn run_clippy_lints(
227229
&self,
228-
cargo_clippy_path: &PathBuf,
230+
cargo_clippy_path: &Path,
229231
target_dir_index: &AtomicUsize,
230232
thread_limit: usize,
231233
total_crates_to_lint: usize,
@@ -308,13 +310,13 @@ impl LintcheckConfig {
308310
// first, check if we got anything passed via the LINTCHECK_TOML env var,
309311
// if not, ask clap if we got any value for --crates-toml <foo>
310312
// if not, use the default "clippy_dev/lintcheck_crates.toml"
311-
let sources_toml = env::var("LINTCHECK_TOML").unwrap_or(
313+
let sources_toml = env::var("LINTCHECK_TOML").unwrap_or_else(|_| {
312314
clap_config
313315
.value_of("crates-toml")
314316
.clone()
315317
.unwrap_or("clippy_dev/lintcheck_crates.toml")
316-
.to_string(),
317-
);
318+
.to_string()
319+
});
318320

319321
let sources_toml_path = PathBuf::from(sources_toml);
320322

@@ -328,9 +330,8 @@ impl LintcheckConfig {
328330
// by default use a single thread
329331
let max_jobs = match clap_config.value_of("threads") {
330332
Some(threads) => {
331-
let threads: usize = threads
332-
.parse()
333-
.expect(&format!("Failed to parse '{}' to a digit", threads));
333+
let err_msg = format!("Failed to parse '{}' to a digit", threads);
334+
let threads: usize = threads.parse().expect(&err_msg);
334335
if threads == 0 {
335336
// automatic choice
336337
// Rayon seems to return thread count so half that for core count
@@ -387,7 +388,7 @@ fn build_clippy() {
387388
}
388389

389390
/// Read a `toml` file and return a list of `CrateSources` that we want to check with clippy
390-
fn read_crates(toml_path: &PathBuf) -> Vec<CrateSource> {
391+
fn read_crates(toml_path: &Path) -> Vec<CrateSource> {
391392
let toml_content: String =
392393
std::fs::read_to_string(&toml_path).unwrap_or_else(|_| panic!("Failed to read {}", toml_path.display()));
393394
let crate_list: SourceList =
@@ -499,7 +500,10 @@ fn gather_stats(clippy_warnings: &[ClippyWarning]) -> (String, HashMap<&String,
499500

500501
/// check if the latest modification of the logfile is older than the modification date of the
501502
/// clippy binary, if this is true, we should clean the lintchec shared target directory and recheck
502-
fn lintcheck_needs_rerun(lintcheck_logs_path: &PathBuf) -> bool {
503+
fn lintcheck_needs_rerun(lintcheck_logs_path: &Path) -> bool {
504+
if !lintcheck_logs_path.exists() {
505+
return true;
506+
}
503507
let clippy_modified: std::time::SystemTime = {
504508
let mut times = [CLIPPY_DRIVER_PATH, CARGO_CLIPPY_PATH].iter().map(|p| {
505509
std::fs::metadata(p)
@@ -533,15 +537,13 @@ pub fn run(clap_config: &ArgMatches) {
533537
// refresh the logs
534538
if lintcheck_needs_rerun(&config.lintcheck_results_path) {
535539
let shared_target_dir = "target/lintcheck/shared_target_dir";
536-
match std::fs::metadata(&shared_target_dir) {
537-
Ok(metadata) => {
538-
if metadata.is_dir() {
539-
println!("Clippy is newer than lint check logs, clearing lintcheck shared target dir...");
540-
std::fs::remove_dir_all(&shared_target_dir)
541-
.expect("failed to remove target/lintcheck/shared_target_dir");
542-
}
543-
},
544-
Err(_) => { /* dir probably does not exist, don't remove anything */ },
540+
// if we get an Err here, the shared target dir probably does simply not exist
541+
if let Ok(metadata) = std::fs::metadata(&shared_target_dir) {
542+
if metadata.is_dir() {
543+
println!("Clippy is newer than lint check logs, clearing lintcheck shared target dir...");
544+
std::fs::remove_dir_all(&shared_target_dir)
545+
.expect("failed to remove target/lintcheck/shared_target_dir");
546+
}
545547
}
546548
}
547549

@@ -660,11 +662,10 @@ pub fn run(clap_config: &ArgMatches) {
660662
}
661663

662664
/// read the previous stats from the lintcheck-log file
663-
fn read_stats_from_file(file_path: &PathBuf) -> HashMap<String, usize> {
665+
fn read_stats_from_file(file_path: &Path) -> HashMap<String, usize> {
664666
let file_content: String = match std::fs::read_to_string(file_path).ok() {
665667
Some(content) => content,
666668
None => {
667-
eprintln!("RETURND");
668669
return HashMap::new();
669670
},
670671
};
@@ -678,9 +679,9 @@ fn read_stats_from_file(file_path: &PathBuf) -> HashMap<String, usize> {
678679
let stats_lines = &lines[start + 1..=end - 1];
679680

680681
stats_lines
681-
.into_iter()
682+
.iter()
682683
.map(|line| {
683-
let mut spl = line.split(" ").into_iter();
684+
let mut spl = line.split(' ');
684685
(
685686
spl.next().unwrap().to_string(),
686687
spl.next().unwrap().parse::<usize>().unwrap(),
@@ -733,3 +734,32 @@ fn print_stats(old_stats: HashMap<String, usize>, new_stats: HashMap<&String, us
733734
println!("{} {} => 0", old_key, old_value);
734735
});
735736
}
737+
738+
#[test]
739+
fn lintcheck_test() {
740+
let args = [
741+
"run",
742+
"--target-dir",
743+
"clippy_dev/target",
744+
"--package",
745+
"clippy_dev",
746+
"--bin",
747+
"clippy_dev",
748+
"--manifest-path",
749+
"clippy_dev/Cargo.toml",
750+
"--features",
751+
"lintcheck",
752+
"--",
753+
"lintcheck",
754+
//"--",
755+
"--crates-toml",
756+
//"--",
757+
"clippy_dev/ci_test_sources.toml",
758+
];
759+
let status = std::process::Command::new("cargo")
760+
.args(&args)
761+
.current_dir("../" /* repo root */)
762+
.status();
763+
764+
assert!(status.unwrap().success());
765+
}

0 commit comments

Comments
 (0)