Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Josh preparations #12759

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[package]
name = "clippy"
# begin autogenerated version
version = "0.1.84"
# end autogenerated version
description = "A bunch of helpful lints to avoid common pitfalls in Rust"
repository = "https://github.com/rust-lang/rust-clippy"
readme = "README.md"
Expand Down
4 changes: 2 additions & 2 deletions book/src/development/adding_lints.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ need to ensure that the MSRV configured for the project is >= the MSRV of the
required Rust feature. If multiple features are required, just use the one with
a lower MSRV.

First, add an MSRV alias for the required feature in [`clippy_config::msrvs`].
First, add an MSRV alias for the required feature in [`clippy_utils::msrvs`].
This can be accessed later as `msrvs::STR_STRIP_PREFIX`, for example.

```rust
Expand Down Expand Up @@ -517,7 +517,7 @@ define_Conf! {
}
```

[`clippy_config::msrvs`]: https://doc.rust-lang.org/nightly/nightly-rustc/clippy_config/msrvs/index.html
[`clippy_utils::msrvs`]: https://doc.rust-lang.org/nightly/nightly-rustc/clippy_config/msrvs/index.html

Afterwards update the documentation for the book as described in [Adding configuration to a lint](#adding-configuration-to-a-lint).

Expand Down
4 changes: 4 additions & 0 deletions clippy_config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
[package]
name = "clippy_config"
# begin autogenerated version
version = "0.1.84"
# end autogenerated version
edition = "2021"
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clippy_utils = { path = "../clippy_utils" }
itertools = "0.12"
serde = { version = "1.0", features = ["derive"] }
toml = "0.7.3"
Expand Down
2 changes: 1 addition & 1 deletion clippy_config/src/conf.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::ClippyConfiguration;
use crate::msrvs::Msrv;
use crate::types::{
DisallowedPath, MacroMatcher, MatchLintBehaviour, PubUnderscoreFieldsBehaviour, Rename, SourceItemOrdering,
SourceItemOrderingCategory, SourceItemOrderingModuleItemGroupings, SourceItemOrderingModuleItemKind,
SourceItemOrderingTraitAssocItemKind, SourceItemOrderingTraitAssocItemKinds,
};
use clippy_utils::msrvs::Msrv;
use rustc_errors::Applicability;
use rustc_session::Session;
use rustc_span::edit_distance::edit_distance;
Expand Down
8 changes: 2 additions & 6 deletions clippy_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@
rustc::untranslatable_diagnostic
)]

extern crate rustc_ast;
extern crate rustc_attr;
#[allow(unused_extern_crates)]
extern crate rustc_driver;
extern crate rustc_errors;
extern crate rustc_hir;
extern crate rustc_middle;
extern crate rustc_session;
extern crate rustc_span;
extern crate smallvec;

mod conf;
mod metadata;
pub mod msrvs;
pub mod types;

pub use conf::{Conf, get_configuration_metadata, lookup_conf_file, sanitize_explanation};
Expand Down
15 changes: 15 additions & 0 deletions clippy_config/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use clippy_utils::def_path_def_ids;
use rustc_hir::def_id::DefIdMap;
use rustc_middle::ty::TyCtxt;
use serde::de::{self, Deserializer, Visitor};
use serde::{Deserialize, Serialize, ser};
use std::collections::HashMap;
Expand Down Expand Up @@ -31,6 +34,18 @@ impl DisallowedPath {
}
}

/// Creates a map of disallowed items to the reason they were disallowed.
pub fn create_disallowed_map(
tcx: TyCtxt<'_>,
disallowed: &'static [DisallowedPath],
) -> DefIdMap<(&'static str, Option<&'static str>)> {
disallowed
.iter()
.map(|x| (x.path(), x.path().split("::").collect::<Vec<_>>(), x.reason()))
.flat_map(|(name, path, reason)| def_path_def_ids(tcx, &path).map(move |id| (id, (name, reason))))
.collect()
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub enum MatchLintBehaviour {
AllTypes,
Expand Down
3 changes: 3 additions & 0 deletions clippy_dev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ edition = "2021"

[dependencies]
aho-corasick = "1.0"
chrono = { version = "0.4.38", default-features = false, features = ["clock"] }
clap = { version = "4.4", features = ["derive"] }
directories = "5"
indoc = "1.0"
itertools = "0.12"
opener = "0.7"
shell-escape = "0.1"
walkdir = "2.3"
xshell = "0.2"

[package.metadata.rust-analyzer]
# This package uses #[feature(rustc_private)]
Expand Down
2 changes: 1 addition & 1 deletion clippy_dev/src/dogfood.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{clippy_project_root, exit_if_err};
use crate::utils::{clippy_project_root, exit_if_err};
use std::process::Command;

/// # Panics
Expand Down
2 changes: 1 addition & 1 deletion clippy_dev/src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::clippy_project_root;
use crate::utils::clippy_project_root;
use itertools::Itertools;
use rustc_lexer::{TokenKind, tokenize};
use shell_escape::escape;
Expand Down
62 changes: 3 additions & 59 deletions clippy_dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,69 +14,13 @@
extern crate rustc_driver;
extern crate rustc_lexer;

use std::io;
use std::path::PathBuf;
use std::process::{self, ExitStatus};

pub mod dogfood;
pub mod fmt;
pub mod lint;
pub mod new_lint;
pub mod release;
pub mod serve;
pub mod setup;
pub mod sync;
pub mod update_lints;

#[cfg(not(windows))]
static CARGO_CLIPPY_EXE: &str = "cargo-clippy";
#[cfg(windows)]
static CARGO_CLIPPY_EXE: &str = "cargo-clippy.exe";

/// Returns the path to the `cargo-clippy` binary
///
/// # Panics
///
/// Panics if the path of current executable could not be retrieved.
#[must_use]
pub fn cargo_clippy_path() -> PathBuf {
let mut path = std::env::current_exe().expect("failed to get current executable name");
path.set_file_name(CARGO_CLIPPY_EXE);
path
}

/// Returns the path to the Clippy project directory
///
/// # Panics
///
/// Panics if the current directory could not be retrieved, there was an error reading any of the
/// Cargo.toml files or ancestor directory is the clippy root directory
#[must_use]
pub fn clippy_project_root() -> PathBuf {
let current_dir = std::env::current_dir().unwrap();
for path in current_dir.ancestors() {
let result = std::fs::read_to_string(path.join("Cargo.toml"));
if let Err(err) = &result {
if err.kind() == io::ErrorKind::NotFound {
continue;
}
}

let content = result.unwrap();
if content.contains("[package]\nname = \"clippy\"") {
return path.to_path_buf();
}
}
panic!("error: Can't determine root of project. Please run inside a Clippy working dir.");
}

/// # Panics
/// Panics if given command result was failed.
pub fn exit_if_err(status: io::Result<ExitStatus>) {
match status.expect("failed to run command").code() {
Some(0) => {},
Some(n) => process::exit(n),
None => {
eprintln!("Killed by signal");
process::exit(1);
},
}
}
pub mod utils;
2 changes: 1 addition & 1 deletion clippy_dev/src/lint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{cargo_clippy_path, exit_if_err};
use crate::utils::{cargo_clippy_path, exit_if_err};
use std::process::{self, Command};
use std::{env, fs};

Expand Down
73 changes: 69 additions & 4 deletions clippy_dev/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#![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::{dogfood, fmt, lint, new_lint, release, serve, setup, sync, update_lints, utils};
use std::convert::Infallible;

fn main() {
Expand All @@ -23,9 +23,9 @@ fn main() {
if print_only {
update_lints::print_lints();
} else if check {
update_lints::update(update_lints::UpdateMode::Check);
update_lints::update(utils::UpdateMode::Check);
} else {
update_lints::update(update_lints::UpdateMode::Change);
update_lints::update(utils::UpdateMode::Change);
}
},
DevCommand::NewLint {
Expand All @@ -35,7 +35,7 @@ fn main() {
r#type,
msrv,
} => match new_lint::create(&pass, &name, &category, r#type.as_deref(), msrv) {
Ok(()) => update_lints::update(update_lints::UpdateMode::Change),
Ok(()) => update_lints::update(utils::UpdateMode::Change),
Err(e) => eprintln!("Unable to create lint: {e}"),
},
DevCommand::Setup(SetupCommand { subcommand }) => match subcommand {
Expand Down Expand Up @@ -75,6 +75,19 @@ fn main() {
uplift,
} => update_lints::rename(&old_name, new_name.as_ref().unwrap_or(&old_name), uplift),
DevCommand::Deprecate { name, reason } => update_lints::deprecate(&name, &reason),
DevCommand::Sync(SyncCommand { subcommand }) => match subcommand {
SyncSubcommand::Pull => sync::rustc_pull(),
SyncSubcommand::Push {
repo_path,
user,
branch,
force,
} => sync::rustc_push(repo_path, &user, &branch, force),
},
DevCommand::Release(ReleaseCommand { subcommand }) => match subcommand {
ReleaseSubcommand::BumpVersion => release::bump_version(),
ReleaseSubcommand::Commit { repo_path, branch } => release::rustc_clippy_commit(repo_path, branch),
},
}
}

Expand Down Expand Up @@ -225,6 +238,10 @@ enum DevCommand {
/// The reason for deprecation
reason: String,
},
/// Sync between the rust repo and the Clippy repo
Sync(SyncCommand),
/// Manage Clippy releases
Release(ReleaseCommand),
}

#[derive(Args)]
Expand Down Expand Up @@ -291,3 +308,51 @@ enum RemoveSubcommand {
/// Remove the tasks added with 'cargo dev setup vscode-tasks'
VscodeTasks,
}

#[derive(Args)]
struct SyncCommand {
#[command(subcommand)]
subcommand: SyncSubcommand,
}

#[derive(Subcommand)]
enum SyncSubcommand {
/// Pull changes from rustc and update the toolchain
Pull,
/// Push changes to rustc
Push {
/// The path to a rustc repo that will be used for pushing changes
repo_path: String,
#[arg(long)]
/// The GitHub username to use for pushing changes
user: String,
#[arg(long, short, default_value = "clippy-subtree-update")]
/// The branch to push to
///
/// This is mostly for experimentation and usually the default should be used.
branch: String,
#[arg(long, short)]
/// Force push changes
force: bool,
},
}

#[derive(Args)]
struct ReleaseCommand {
#[command(subcommand)]
subcommand: ReleaseSubcommand,
}

#[derive(Subcommand)]
enum ReleaseSubcommand {
#[command(name = "bump_version")]
/// Bump the version in the Cargo.toml files
BumpVersion,
/// Print the Clippy commit in the rustc repo for the specified branch
Commit {
/// The path to a rustc repo to look for the commit
repo_path: String,
/// For which branch to print the commit
branch: release::Branch,
},
}
25 changes: 5 additions & 20 deletions clippy_dev/src/new_lint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::clippy_project_root;
use crate::utils::{clippy_project_root, clippy_version};
use indoc::{formatdoc, writedoc};
use std::fmt;
use std::fmt::Write as _;
Expand Down Expand Up @@ -186,23 +186,8 @@ fn to_camel_case(name: &str) -> String {
}

pub(crate) fn get_stabilization_version() -> String {
fn parse_manifest(contents: &str) -> Option<String> {
let version = contents
.lines()
.filter_map(|l| l.split_once('='))
.find_map(|(k, v)| (k.trim() == "version").then(|| v.trim()))?;
let Some(("0", version)) = version.get(1..version.len() - 1)?.split_once('.') else {
return None;
};
let (minor, patch) = version.split_once('.')?;
Some(format!(
"{}.{}.0",
minor.parse::<u32>().ok()?,
patch.parse::<u32>().ok()?
))
}
let contents = fs::read_to_string("Cargo.toml").expect("Unable to read `Cargo.toml`");
parse_manifest(&contents).expect("Unable to find package version in `Cargo.toml`")
let (minor, patch) = clippy_version();
format!("{minor}.{patch}.0")
}

fn get_test_file_contents(lint_name: &str, msrv: bool) -> String {
Expand Down Expand Up @@ -273,7 +258,7 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
result.push_str(&if enable_msrv {
formatdoc!(
r"
use clippy_config::msrvs::{{self, Msrv}};
use clippy_utils::msrvs::{{self, Msrv}};
use clippy_config::Conf;
{pass_import}
use rustc_lint::{{{context_import}, {pass_type}, LintContext}};
Expand Down Expand Up @@ -399,7 +384,7 @@ fn create_lint_for_ty(lint: &LintData<'_>, enable_msrv: bool, ty: &str) -> io::R
let _: fmt::Result = writedoc!(
lint_file_contents,
r#"
use clippy_config::msrvs::{{self, Msrv}};
use clippy_utils::msrvs::{{self, Msrv}};
use rustc_lint::{{{context_import}, LintContext}};

use super::{name_upper};
Expand Down
Loading
Loading