Skip to content

Commit

Permalink
WIP: lint version check
Browse files Browse the repository at this point in the history
  • Loading branch information
samueltardieu committed Oct 5, 2024
1 parent 753629b commit a61a355
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 1 deletion.
88 changes: 88 additions & 0 deletions .github/workflows/versioncheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
name: New lint stabilization version check

on: [pull_request, push]

env:
RUST_BACKTRACE: 1
CARGO_INCREMENTAL: 0

concurrency:
# For a given workflow, if we push to the same PR, cancel all previous builds on that PR.
group: "${{ github.workflow }}-${{ github.event.pull_request.number}}"
cancel-in-progress: true

jobs:
# Collect lint metadata on the PR's target branch and stores the results as an artifact
base:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 2

# HEAD is the generated merge commit `refs/pull/N/merge` between the PR and `master`, `HEAD^`
# being the commit from `master` that is the base of the merge
- name: Checkout base
run: git checkout HEAD^

- name: Cache metadata
id: cache-metadata
uses: actions/cache@v4
with:
path: util/gh-pages/lints.json
key: metadata-bin-${{ hashfiles('clippy_lints/**') }}

- name: Collect metadata
if: steps.cache-metadata.outputs.cache-hit != 'true'
run: cargo collect-metadata

- name: Upload base JSON
uses: actions/upload-artifact@v4
with:
name: base
path: util/gh-pages/lints.json

head:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 2

- name: Cache metadata
id: cache-metadata
uses: actions/cache@v4
with:
path: util/gh-pages/lints.json
key: metadata-bin-${{ hashfiles('clippy_lints/**') }}

- name: Collect metadata
if: steps.cache-metadata.outputs.cache-hit != 'true'
run: cargo collect-metadata

- name: Upload base JSON
uses: actions/upload-artifact@v4
with:
name: head
path: util/gh-pages/lints.json

# Retrieves the head and base JSON results and prints the diff to the GH actions step summary
diff:
runs-on: ubuntu-latest

needs: [base, head]

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Download JSON
uses: actions/download-artifact@v4

- name: Diff results
run: |
cargo dev check_new_lints_version base/lints.json head/lints.json
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5417,6 +5417,7 @@ Released 2018-09-13
[`drop_copy`]: https://rust-lang.github.io/rust-clippy/master/index.html#drop_copy
[`drop_non_drop`]: https://rust-lang.github.io/rust-clippy/master/index.html#drop_non_drop
[`drop_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#drop_ref
[`dummy_lint`]: https://rust-lang.github.io/rust-clippy/master/index.html#dummy_lint
[`duplicate_mod`]: https://rust-lang.github.io/rust-clippy/master/index.html#duplicate_mod
[`duplicate_underscore_argument`]: https://rust-lang.github.io/rust-clippy/master/index.html#duplicate_underscore_argument
[`duplicated_attributes`]: https://rust-lang.github.io/rust-clippy/master/index.html#duplicated_attributes
Expand Down
2 changes: 2 additions & 0 deletions clippy_dev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ clap = { version = "4.4", features = ["derive"] }
indoc = "1.0"
itertools = "0.12"
opener = "0.6"
serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
shell-escape = "0.1"
walkdir = "2.3"

Expand Down
44 changes: 44 additions & 0 deletions clippy_dev/src/check_lint_version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::collections::HashMap;
use std::fs::File;
use std::path::Path;
use std::process;

use crate::new_lint::get_stabilization_version;

#[derive(serde::Deserialize)]
struct Lint {
id: String,
version: String,
}

pub fn load_metadata(metadata: &Path) -> HashMap<String, String> {
let lints: Vec<Lint> = serde_json::from_reader(File::open(metadata).unwrap()).unwrap();
lints.into_iter().map(|lint| (lint.id, lint.version)).collect()
}

pub fn check_lint_version(old_metadata: &Path, new_metadata: &Path) {
let stabilization_version = get_stabilization_version();
let old_lints = load_metadata(old_metadata);
let mut new_lints = load_metadata(new_metadata)
.into_iter()
.filter(|(name, _)| !old_lints.contains_key(name))
.collect::<Vec<_>>();
if new_lints.is_empty() {
println!("No new lints");
return;
}
new_lints.sort_unstable();
let mut error = false;
println!("New lints:");
for (name, version) in new_lints {
if version == stabilization_version {
println!(" - {name}");
} else {
println!(" - {name}: lint declares version {version}, stabilization version is {stabilization_version}");
error = true;
}
}
if error {
process::exit(1);
}
}
1 change: 1 addition & 0 deletions clippy_dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::io;
use std::path::PathBuf;
use std::process::{self, ExitStatus};

pub mod check_lint_version;
pub mod dogfood;
pub mod fmt;
pub mod lint;
Expand Down
15 changes: 14 additions & 1 deletion clippy_dev/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
#![warn(rust_2018_idioms, unused_lifetimes)]

use clap::{Args, Parser, Subcommand};
use clippy_dev::{dogfood, fmt, lint, new_lint, serve, setup, update_lints};
use clippy_dev::{check_lint_version, dogfood, fmt, lint, new_lint, serve, setup, update_lints};
use std::convert::Infallible;
use std::path::PathBuf;

fn main() {
let dev = Dev::parse();
Expand All @@ -13,6 +14,10 @@ fn main() {
DevCommand::Bless => {
eprintln!("use `cargo bless` to automatically replace `.stderr` and `.fixed` files as tests are being run");
},
DevCommand::CheckNewLintsVersion {
old_metadata,
new_metadata,
} => check_lint_version::check_lint_version(&old_metadata, &new_metadata),
DevCommand::Dogfood {
fix,
allow_dirty,
Expand Down Expand Up @@ -89,6 +94,14 @@ struct Dev {
enum DevCommand {
/// Bless the test output changes
Bless,
/// Check that new lints agree with the stabilization version
#[command(name = "check_new_lints_version")]
CheckNewLintsVersion {
/// Original metadata
old_metadata: PathBuf,
/// New metadata
new_metadata: PathBuf,
},
/// Runs the dogfood test
Dogfood {
#[arg(long)]
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
crate::drop_forget_ref::DROP_NON_DROP_INFO,
crate::drop_forget_ref::FORGET_NON_DROP_INFO,
crate::drop_forget_ref::MEM_FORGET_INFO,
crate::dummy_lint::DUMMY_LINT_INFO,
crate::duplicate_mod::DUPLICATE_MOD_INFO,
crate::else_if_without_else::ELSE_IF_WITHOUT_ELSE_INFO,
crate::empty_drop::EMPTY_DROP_INFO,
Expand Down
25 changes: 25 additions & 0 deletions clippy_lints/src/dummy_lint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use rustc_lint::LateLintPass;
use rustc_session::declare_lint_pass;

declare_clippy_lint! {
/// ### What it does
///
/// ### Why is this bad?
///
/// ### Example
/// ```no_run
/// // example code where clippy issues a warning
/// ```
/// Use instead:
/// ```no_run
/// // example code which does not raise clippy warning
/// ```
#[clippy::version = "1.71.0"]
pub DUMMY_LINT,
suspicious,
"precise lint description"
}

declare_lint_pass!(DummyLint => [DUMMY_LINT]);

impl LateLintPass<'_> for DummyLint {}
2 changes: 2 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ mod disallowed_types;
mod doc;
mod double_parens;
mod drop_forget_ref;
mod dummy_lint;
mod duplicate_mod;
mod else_if_without_else;
mod empty_drop;
Expand Down Expand Up @@ -944,5 +945,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
store.register_late_pass(|_| Box::new(manual_is_power_of_two::ManualIsPowerOfTwo));
store.register_late_pass(|_| Box::new(non_zero_suggestions::NonZeroSuggestions));
store.register_late_pass(move |_| Box::new(unused_trait_names::UnusedTraitNames::new(conf)));
store.register_late_pass(|_| Box::new(dummy_lint::DummyLint));
// add lints here, do not remove this comment, it's used in `new_lint`
}
5 changes: 5 additions & 0 deletions tests/ui/dummy_lint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![warn(clippy::dummy_lint)]

fn main() {
// test code goes here
}

0 comments on commit a61a355

Please sign in to comment.