Skip to content

Commit aa15a54

Browse files
committed
Auto merge of #7197 - xFrednet:4310-depreciated-lints-collection, r=flip1995
Metadata collection monster eating deprecated lints This adds the collection of deprecated lints to the metadata collection monster. The JSON output has the same structure with the *new* lint group "DEPRECATED". Here is one of fourteen examples it was able to dig up in Clippy's code: ```JSON { "id": "assign_op_pattern", "id_span": { "path": "src/assign_ops.rs", "line": 34 }, "group": "clippy::style", "docs": " **What it does:** Checks for `a = a op b` or `a = b commutative_op a` patterns.\n\n **Why is this bad?** These can be written as the shorter `a op= b`.\n\n **Known problems:** While forbidden by the spec, `OpAssign` traits may have\n implementations that differ from the regular `Op` impl.\n\n **Example:**\n ```rust\n let mut a = 5;\n let b = 0;\n // ...\n // Bad\n a = a + b;\n\n // Good\n a += b;\n ```\n", "applicability": { "is_multi_part_suggestion": false, "applicability": "MachineApplicable" } } ``` And you! Yes you! Sir or Madam can get all of this **for free** in Clippy if this PR gets merged. (Sorry for the silliness ^^) --- See: #7172 for the full metadata collection to-do list or to suggest a new feature in connection to it 🙃 --- changelog: none r? `@flip1995`
2 parents 0d4e24e + a988a90 commit aa15a54

File tree

3 files changed

+62
-24
lines changed

3 files changed

+62
-24
lines changed

clippy_lints/src/deprecated_lints.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
/// This struct fakes the `Lint` declaration that is usually created by `declare_lint!`. This
2+
/// enables the simple extraction of the metadata without changing the current deprecation
3+
/// declaration.
4+
pub struct ClippyDeprecatedLint;
5+
16
macro_rules! declare_deprecated_lint {
2-
(pub $name: ident, $_reason: expr) => {
3-
declare_lint!(pub $name, Allow, "deprecated lint")
7+
{ $(#[$attr:meta])* pub $name: ident, $_reason: expr} => {
8+
$(#[$attr])*
9+
#[allow(dead_code)]
10+
pub static $name: ClippyDeprecatedLint = ClippyDeprecatedLint {};
411
}
512
}
613

clippy_lints/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ macro_rules! extract_msrv_attr {
162162
mod consts;
163163
#[macro_use]
164164
mod utils;
165+
#[cfg(feature = "metadata-collector-lint")]
166+
mod deprecated_lints;
165167

166168
// begin lints modules, do not remove this comment, it’s used in `update_lints`
167169
mod absurd_extreme_comparisons;

clippy_lints/src/utils/internal_lints/metadata_collector.rs

+51-22
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
//! during any comparison or mapping. (Please take care of this, it's not fun to spend time on such
99
//! a simple mistake)
1010
11-
// # NITs
12-
// - TODO xFrednet 2021-02-13: Collect depreciations and maybe renames
13-
1411
use if_chain::if_chain;
1512
use rustc_data_structures::fx::FxHashMap;
1613
use rustc_hir::{
@@ -28,7 +25,7 @@ use std::path::Path;
2825

2926
use crate::utils::internal_lints::is_lint_ref_type;
3027
use clippy_utils::{
31-
diagnostics::span_lint, last_path_segment, match_function_call, match_path, paths, ty::match_type,
28+
diagnostics::span_lint, last_path_segment, match_def_path, match_function_call, match_path, paths, ty::match_type,
3229
ty::walk_ptrs_ty_depth,
3330
};
3431

@@ -41,6 +38,8 @@ const BLACK_LISTED_LINTS: [&str; 3] = ["lint_author", "deep_code_inspection", "i
4138
const IGNORED_LINT_GROUPS: [&str; 1] = ["clippy::all"];
4239
/// Lints within this group will be excluded from the collection
4340
const EXCLUDED_LINT_GROUPS: [&str; 1] = ["clippy::internal"];
41+
/// Collected deprecated lint will be assigned to this group in the JSON output
42+
const DEPRECATED_LINT_GROUP_STR: &str = "DEPRECATED";
4443

4544
const LINT_EMISSION_FUNCTIONS: [&[&str]; 7] = [
4645
&["clippy_utils", "diagnostics", "span_lint"],
@@ -66,6 +65,7 @@ const SUGGESTION_FUNCTIONS: [&[&str]; 2] = [
6665
&["clippy_utils", "diagnostics", "multispan_sugg"],
6766
&["clippy_utils", "diagnostics", "multispan_sugg_with_applicability"],
6867
];
68+
const DEPRECATED_LINT_TYPE: [&str; 3] = ["clippy_lints", "deprecated_lints", "ClippyDeprecatedLint"];
6969

7070
/// The index of the applicability name of `paths::APPLICABILITY_VALUES`
7171
const APPLICABILITY_NAME_INDEX: usize = 2;
@@ -225,23 +225,42 @@ impl<'hir> LateLintPass<'hir> for MetadataCollector {
225225
/// }
226226
/// ```
227227
fn check_item(&mut self, cx: &LateContext<'hir>, item: &'hir Item<'_>) {
228-
if_chain! {
229-
// item validation
230-
if let ItemKind::Static(ref ty, Mutability::Not, _) = item.kind;
231-
if is_lint_ref_type(cx, ty);
232-
// blacklist check
233-
let lint_name = sym_to_string(item.ident.name).to_ascii_lowercase();
234-
if !BLACK_LISTED_LINTS.contains(&lint_name.as_str());
235-
// metadata extraction
236-
if let Some(group) = get_lint_group_or_lint(cx, &lint_name, item);
237-
if let Some(docs) = extract_attr_docs_or_lint(cx, item);
238-
then {
239-
self.lints.push(LintMetadata::new(
240-
lint_name,
241-
SerializableSpan::from_item(cx, item),
242-
group,
243-
docs,
244-
));
228+
if let ItemKind::Static(ref ty, Mutability::Not, _) = item.kind {
229+
// Normal lint
230+
if_chain! {
231+
// item validation
232+
if is_lint_ref_type(cx, ty);
233+
// blacklist check
234+
let lint_name = sym_to_string(item.ident.name).to_ascii_lowercase();
235+
if !BLACK_LISTED_LINTS.contains(&lint_name.as_str());
236+
// metadata extraction
237+
if let Some(group) = get_lint_group_or_lint(cx, &lint_name, item);
238+
if let Some(docs) = extract_attr_docs_or_lint(cx, item);
239+
then {
240+
self.lints.push(LintMetadata::new(
241+
lint_name,
242+
SerializableSpan::from_item(cx, item),
243+
group,
244+
docs,
245+
));
246+
}
247+
}
248+
249+
if_chain! {
250+
if is_deprecated_lint(cx, ty);
251+
// blacklist check
252+
let lint_name = sym_to_string(item.ident.name).to_ascii_lowercase();
253+
if !BLACK_LISTED_LINTS.contains(&lint_name.as_str());
254+
// Metadata the little we can get from a deprecated lint
255+
if let Some(docs) = extract_attr_docs_or_lint(cx, item);
256+
then {
257+
self.lints.push(LintMetadata::new(
258+
lint_name,
259+
SerializableSpan::from_item(cx, item),
260+
DEPRECATED_LINT_GROUP_STR.to_string(),
261+
docs,
262+
));
263+
}
245264
}
246265
}
247266
}
@@ -268,7 +287,7 @@ impl<'hir> LateLintPass<'hir> for MetadataCollector {
268287
// - src/misc.rs:734:9
269288
// - src/methods/mod.rs:3545:13
270289
// - src/methods/mod.rs:3496:13
271-
// We are basically unable to resolve the lint name it self.
290+
// We are basically unable to resolve the lint name itself.
272291
return;
273292
}
274293

@@ -347,6 +366,16 @@ fn get_lint_group(cx: &LateContext<'_>, lint_id: LintId) -> Option<String> {
347366
None
348367
}
349368

369+
fn is_deprecated_lint(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> bool {
370+
if let hir::TyKind::Path(ref path) = ty.kind {
371+
if let hir::def::Res::Def(DefKind::Struct, def_id) = cx.qpath_res(path, ty.hir_id) {
372+
return match_def_path(cx, def_id, &DEPRECATED_LINT_TYPE);
373+
}
374+
}
375+
376+
false
377+
}
378+
350379
// ==================================================================
351380
// Lint emission
352381
// ==================================================================

0 commit comments

Comments
 (0)