Skip to content

Commit

Permalink
Auto merge of #13359 - blyxyas:declare_clippy_macro, r=Alexendoo
Browse files Browse the repository at this point in the history
Turn declare_clippy_lint into a declarative macro

Ease of development, and hopefully compile times (the dependencies are still there because of ui-test). The procedural macro was doing just some very basic processing (like assigning a lint level to each category), so it didn't have a reason to stay IMO

changelog: None
  • Loading branch information
bors committed Oct 11, 2024
2 parents 47903db + 13e2633 commit 8125cd5
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 203 deletions.
24 changes: 24 additions & 0 deletions clippy_config/src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,30 @@ impl ConfError {
}
}

// Remove code tags and code behind '# 's, as they are not needed for the lint docs and --explain
pub fn sanitize_explanation(raw_docs: &str) -> String {
// Remove tags and hidden code:
let mut explanation = String::with_capacity(128);
let mut in_code = false;
for line in raw_docs.lines().map(str::trim) {
if let Some(lang) = line.strip_prefix("```") {
let tag = lang.split_once(',').map_or(lang, |(left, _)| left);
if !in_code && matches!(tag, "" | "rust" | "ignore" | "should_panic" | "no_run" | "compile_fail") {
explanation += "```rust\n";
} else {
explanation += line;
explanation.push('\n');
}
in_code = !in_code;
} else if !(in_code && line.starts_with("# ")) {
explanation += line;
explanation.push('\n');
}
}

explanation
}

macro_rules! wrap_option {
() => {
None
Expand Down
2 changes: 1 addition & 1 deletion clippy_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ mod metadata;
pub mod msrvs;
pub mod types;

pub use conf::{Conf, get_configuration_metadata, lookup_conf_file};
pub use conf::{Conf, get_configuration_metadata, lookup_conf_file, sanitize_explanation};
pub use metadata::ClippyConfiguration;
1 change: 0 additions & 1 deletion clippy_lints/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ arrayvec = { version = "0.7", default-features = false }
cargo_metadata = "0.18"
clippy_config = { path = "../clippy_config" }
clippy_utils = { path = "../clippy_utils" }
declare_clippy_lint = { path = "../declare_clippy_lint" }
itertools = "0.12"
quine-mc_cluskey = "0.2"
regex-syntax = "0.8"
Expand Down
162 changes: 162 additions & 0 deletions clippy_lints/src/declare_clippy_lint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#[macro_export]
#[allow(clippy::crate_in_macro_def)]
macro_rules! declare_clippy_lint {
(@
$(#[doc = $lit:literal])*
pub $lint_name:ident,
$category:ident,
$lintcategory:expr,
$desc:literal,
$version_expr:expr,
$version_lit:literal
) => {
rustc_session::declare_tool_lint! {
$(#[doc = $lit])*
#[clippy::version = $version_lit]
pub clippy::$lint_name,
$category,
$desc,
report_in_external_macro:true
}

pub(crate) static ${concat($lint_name, _INFO)}: &'static crate::LintInfo = &crate::LintInfo {
lint: &$lint_name,
category: $lintcategory,
explanation: concat!($($lit,"\n",)*),
location: concat!(file!(), "#L", line!()),
version: $version_expr
};
};
(
$(#[doc = $lit:literal])*
#[clippy::version = $version:literal]
pub $lint_name:ident,
restriction,
$desc:literal
) => {
declare_clippy_lint! {@
$(#[doc = $lit])*
pub $lint_name, Allow, crate::LintCategory::Restriction, $desc,
Some($version), $version
}
};
(
$(#[doc = $lit:literal])*
#[clippy::version = $version:literal]
pub $lint_name:ident,
style,
$desc:literal
) => {
declare_clippy_lint! {@
$(#[doc = $lit])*
pub $lint_name, Warn, crate::LintCategory::Style, $desc,
Some($version), $version

}
};
(
$(#[doc = $lit:literal])*
#[clippy::version = $version:literal]
pub $lint_name:ident,
correctness,
$desc:literal
) => {
declare_clippy_lint! {@
$(#[doc = $lit])*
pub $lint_name, Deny, crate::LintCategory::Correctness, $desc,
Some($version), $version

}
};
(
$(#[doc = $lit:literal])*
#[clippy::version = $version:literal]
pub $lint_name:ident,
perf,
$desc:literal
) => {
declare_clippy_lint! {@
$(#[doc = $lit])*
pub $lint_name, Warn, crate::LintCategory::Perf, $desc,
Some($version), $version
}
};
(
$(#[doc = $lit:literal])*
#[clippy::version = $version:literal]
pub $lint_name:ident,
complexity,
$desc:literal
) => {
declare_clippy_lint! {@
$(#[doc = $lit])*
pub $lint_name, Warn, crate::LintCategory::Complexity, $desc,
Some($version), $version
}
};
(
$(#[doc = $lit:literal])*
#[clippy::version = $version:literal]
pub $lint_name:ident,
suspicious,
$desc:literal
) => {
declare_clippy_lint! {@
$(#[doc = $lit])*
pub $lint_name, Warn, crate::LintCategory::Suspicious, $desc,
Some($version), $version
}
};
(
$(#[doc = $lit:literal])*
#[clippy::version = $version:literal]
pub $lint_name:ident,
nursery,
$desc:literal
) => {
declare_clippy_lint! {@
$(#[doc = $lit])*
pub $lint_name, Allow, crate::LintCategory::Nursery, $desc,
Some($version), $version
}
};
(
$(#[doc = $lit:literal])*
#[clippy::version = $version:literal]
pub $lint_name:ident,
pedantic,
$desc:literal
) => {
declare_clippy_lint! {@
$(#[doc = $lit])*
pub $lint_name, Allow, crate::LintCategory::Pedantic, $desc,
Some($version), $version
}
};
(
$(#[doc = $lit:literal])*
#[clippy::version = $version:literal]
pub $lint_name:ident,
cargo,
$desc:literal
) => {
declare_clippy_lint! {@
$(#[doc = $lit])*
pub $lint_name, Allow, crate::LintCategory::Cargo, $desc,
Some($version), $version
}
};

(
$(#[doc = $lit:literal])*
pub $lint_name:ident,
internal,
$desc:literal
) => {
declare_clippy_lint! {@
$(#[doc = $lit])*
pub $lint_name, Allow, crate::LintCategory::Internal, $desc,
None, "0.0.0"
}
};
}
11 changes: 7 additions & 4 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![feature(array_windows)]
#![feature(binary_heap_into_iter_sorted)]
#![feature(box_patterns)]
#![feature(macro_metavar_expr_concat)]
#![feature(control_flow_enum)]
#![feature(f128)]
#![feature(f16)]
Expand Down Expand Up @@ -59,9 +60,10 @@ extern crate rustc_trait_selection;
extern crate thin_vec;

#[macro_use]
extern crate clippy_utils;
mod declare_clippy_lint;

#[macro_use]
extern crate declare_clippy_lint;
extern crate clippy_utils;

#[cfg_attr(feature = "internal", allow(clippy::missing_clippy_version_attribute))]
mod utils;
Expand Down Expand Up @@ -394,7 +396,7 @@ mod zero_sized_map_values;
mod zombie_processes;
// end lints modules, do not remove this comment, it’s used in `update_lints`

use clippy_config::{Conf, get_configuration_metadata};
use clippy_config::{Conf, get_configuration_metadata, sanitize_explanation};
use clippy_utils::macros::FormatArgsStorage;
use rustc_data_structures::fx::FxHashSet;
use rustc_lint::{Lint, LintId};
Expand Down Expand Up @@ -522,8 +524,9 @@ impl LintInfo {

pub fn explain(name: &str) -> i32 {
let target = format!("clippy::{}", name.to_ascii_uppercase());

if let Some(info) = declared_lints::LINTS.iter().find(|info| info.lint.name == target) {
println!("{}", info.explanation);
println!("{}", sanitize_explanation(info.explanation));
// Check if the lint has configuration
let mut mdconf = get_configuration_metadata();
let name = name.to_ascii_lowercase();
Expand Down
2 changes: 1 addition & 1 deletion clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![feature(f128)]
#![feature(f16)]
#![feature(if_let_guard)]
#![feature(macro_metavar_expr_concat)]
#![feature(let_chains)]
#![feature(never_type)]
#![feature(rustc_private)]
Expand Down Expand Up @@ -129,7 +130,6 @@ use crate::consts::{ConstEvalCtxt, Constant, mir_to_const};
use crate::higher::Range;
use crate::ty::{adt_and_variant_of_res, can_partially_move_ty, expr_sig, is_copy, is_recursively_primitive_type};
use crate::visitors::for_each_expr_without_closures;

use rustc_middle::hir::nested_filter;

#[macro_export]
Expand Down
13 changes: 0 additions & 13 deletions declare_clippy_lint/Cargo.toml

This file was deleted.

Loading

0 comments on commit 8125cd5

Please sign in to comment.