Skip to content

Commit f35345d

Browse files
committed
Auto merge of #6887 - xFrednet:4310-internal-metadata-extraction-lint, r=xFrednet
A metadata collection monster This PR introduces a metadata collection lint as discussed in #4310. It currently collects: * The lint ID * The lint declaration file and location (for #1303) * The lint group * The documentation * The applicability (if resolvable) * If the suggestion is a multi-part-suggestion This data has a slightly different structure than the current [lints.json](https://github.com/rust-lang/rust-clippy/blob/gh-pages/master/lints.json) and doesn't include depreciated lints yet. I plan to adapt the website to the new format and include depreciated lints in a follow-up PR :). The current collected json looks like this: [metadata_collection.json](https://gist.github.com/xFrednet/6b9e2c3f725f476ba88db9563f67e119) The entire implementation is guarded behind the `metadata-collector-lint` feature and the `ENABLE_METADATA_COLLECTION` environment value to prevent default collection. You can test the implementation via: ```sh $ ENABLE_METADATA_COLLECTION=1 cargo test --test dogfood --all-features ``` changelog: none --- The size of this PR sadly also grew into a small monster, sorry! I definitely plan to improve on this! And it's totally okay if you take your time with this :) r? `@phansch` cc: `@flip1995`
2 parents 0baf6bf + e0eb29c commit f35345d

File tree

13 files changed

+688
-19
lines changed

13 files changed

+688
-19
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ out
2929

3030
# gh pages docs
3131
util/gh-pages/lints.json
32+
**/metadata_collection.json
3233

3334
# rustfmt backups
3435
*.rs.bk

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ rustc_tools_util = { version = "0.2.0", path = "rustc_tools_util" }
5252
deny-warnings = []
5353
integration = ["tempfile"]
5454
internal-lints = ["clippy_lints/internal-lints"]
55+
metadata-collector-lint = ["internal-lints", "clippy_lints/metadata-collector-lint"]
5556

5657
[package.metadata.rust-analyzer]
5758
# This package uses #[feature(rustc_private)]

clippy_lints/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pulldown-cmark = { version = "0.8", default-features = false }
2020
quine-mc_cluskey = "0.2.2"
2121
regex-syntax = "0.6"
2222
serde = { version = "1.0", features = ["derive"] }
23+
serde_json = { version = "1.0", optional = true }
2324
toml = "0.5.3"
2425
unicode-normalization = "0.1"
2526
semver = "0.11"
@@ -32,6 +33,7 @@ url = { version = "2.1.0", features = ["serde"] }
3233
deny-warnings = []
3334
# build clippy with internal lints enabled, off by default
3435
internal-lints = ["clippy_utils/internal-lints"]
36+
metadata-collector-lint = ["serde_json", "clippy_utils/metadata-collector-lint"]
3537

3638
[package.metadata.rust-analyzer]
3739
# This crate uses #[feature(rustc_private)]

clippy_lints/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,13 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10041004
store.register_late_pass(|| box utils::internal_lints::MatchTypeOnDiagItem);
10051005
store.register_late_pass(|| box utils::internal_lints::OuterExpnDataPass);
10061006
}
1007+
#[cfg(feature = "metadata-collector-lint")]
1008+
{
1009+
if std::env::var("ENABLE_METADATA_COLLECTION").eq(&Ok("1".to_string())) {
1010+
store.register_late_pass(|| box utils::internal_lints::metadata_collector::MetadataCollector::default());
1011+
}
1012+
}
1013+
10071014
store.register_late_pass(|| box utils::author::Author);
10081015
store.register_late_pass(|| box await_holding_invalid::AwaitHolding);
10091016
store.register_late_pass(|| box serde_api::SerdeApi);

clippy_lints/src/slow_vector_initialization.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_ast::ast::LitKind;
77
use rustc_errors::Applicability;
88
use rustc_hir::intravisit::{walk_block, walk_expr, walk_stmt, NestedVisitorMap, Visitor};
99
use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, HirId, PatKind, QPath, Stmt, StmtKind};
10-
use rustc_lint::{LateContext, LateLintPass, Lint};
10+
use rustc_lint::{LateContext, LateLintPass};
1111
use rustc_middle::hir::map::Map;
1212
use rustc_session::{declare_lint_pass, declare_tool_lint};
1313
use rustc_span::symbol::sym;
@@ -157,26 +157,16 @@ impl SlowVectorInit {
157157
vec_alloc: &VecAllocation<'_>,
158158
) {
159159
match initialization {
160-
InitializationType::Extend(e) | InitializationType::Resize(e) => Self::emit_lint(
161-
cx,
162-
e,
163-
vec_alloc,
164-
"slow zero-filling initialization",
165-
SLOW_VECTOR_INITIALIZATION,
166-
),
160+
InitializationType::Extend(e) | InitializationType::Resize(e) => {
161+
Self::emit_lint(cx, e, vec_alloc, "slow zero-filling initialization")
162+
},
167163
};
168164
}
169165

170-
fn emit_lint<'tcx>(
171-
cx: &LateContext<'tcx>,
172-
slow_fill: &Expr<'_>,
173-
vec_alloc: &VecAllocation<'_>,
174-
msg: &str,
175-
lint: &'static Lint,
176-
) {
166+
fn emit_lint<'tcx>(cx: &LateContext<'tcx>, slow_fill: &Expr<'_>, vec_alloc: &VecAllocation<'_>, msg: &str) {
177167
let len_expr = Sugg::hir(cx, vec_alloc.len_expr, "len");
178168

179-
span_lint_and_then(cx, lint, slow_fill.span, msg, |diag| {
169+
span_lint_and_then(cx, SLOW_VECTOR_INITIALIZATION, slow_fill.span, msg, |diag| {
180170
diag.span_suggestion(
181171
vec_alloc.allocation_expr.span,
182172
"consider replace allocation with",

clippy_lints/src/utils/internal_lints.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ use rustc_typeck::hir_ty_to_ty;
3232

3333
use std::borrow::{Borrow, Cow};
3434

35+
#[cfg(feature = "metadata-collector-lint")]
36+
pub mod metadata_collector;
37+
3538
declare_clippy_lint! {
3639
/// **What it does:** Checks for various things we like to keep tidy in clippy.
3740
///

0 commit comments

Comments
 (0)