Skip to content

Commit 9435077

Browse files
committed
New internal lint to make clippy::version attribute mandatory
1 parent 94bc0a1 commit 9435077

29 files changed

+129
-31
lines changed

clippy_lints/src/lib.register_internal.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ store.register_group(true, "clippy::internal", Some("clippy_internal"), vec![
1313
LintId::of(utils::internal_lints::INVALID_PATHS),
1414
LintId::of(utils::internal_lints::LINT_WITHOUT_LINT_PASS),
1515
LintId::of(utils::internal_lints::MATCH_TYPE_ON_DIAGNOSTIC_ITEM),
16+
LintId::of(utils::internal_lints::MISSING_CLIPPY_VERSION_ATTRIBUTE),
1617
LintId::of(utils::internal_lints::OUTER_EXPN_EXPN_DATA),
1718
LintId::of(utils::internal_lints::PRODUCE_ICE),
1819
LintId::of(utils::internal_lints::UNNECESSARY_SYMBOL_STR),

clippy_lints/src/lib.register_lints.rs

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ store.register_lints(&[
2424
#[cfg(feature = "internal-lints")]
2525
utils::internal_lints::MATCH_TYPE_ON_DIAGNOSTIC_ITEM,
2626
#[cfg(feature = "internal-lints")]
27+
utils::internal_lints::MISSING_CLIPPY_VERSION_ATTRIBUTE,
28+
#[cfg(feature = "internal-lints")]
2729
utils::internal_lints::OUTER_EXPN_EXPN_DATA,
2830
#[cfg(feature = "internal-lints")]
2931
utils::internal_lints::PRODUCE_ICE,

clippy_lints/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ macro_rules! declare_clippy_lint {
153153

154154
#[cfg(feature = "metadata-collector-lint")]
155155
mod deprecated_lints;
156+
#[cfg_attr(
157+
any(feature = "internal-lints", feature = "metadata-collector-lint"),
158+
allow(clippy::missing_clippy_version_attribute)
159+
)]
156160
mod utils;
157161

158162
// begin lints modules, do not remove this comment, it’s used in `update_lints`

clippy_lints/src/utils/internal_lints.rs

+18
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,15 @@ declare_clippy_lint! {
328328
"found an invalid `clippy::version` attribute"
329329
}
330330

331+
declare_clippy_lint! {
332+
/// ### What it does
333+
/// Checks for declared clippy lints without the `clippy::version` attribute.
334+
///
335+
pub MISSING_CLIPPY_VERSION_ATTRIBUTE,
336+
internal,
337+
"found clippy lint without `clippy::version` attribute"
338+
}
339+
331340
declare_lint_pass!(ClippyLintsInternal => [CLIPPY_LINTS_INTERNAL]);
332341

333342
impl EarlyLintPass for ClippyLintsInternal {
@@ -492,6 +501,15 @@ fn check_invalid_clippy_version_attribute(cx: &LateContext<'_>, item: &'_ Item<'
492501
"please use a valid sematic version, see `doc/adding_lints.md`",
493502
);
494503
}
504+
} else {
505+
span_lint_and_help(
506+
cx,
507+
MISSING_CLIPPY_VERSION_ATTRIBUTE,
508+
item.span,
509+
"this lint is missing the `clippy::version` attribute or version value",
510+
None,
511+
"please use a `clippy::version` attribute, see `doc/adding_lints.md`",
512+
);
495513
}
496514
}
497515

tests/ui-internal/check_clippy_version_attribute.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,34 @@ declare_tool_lint! {
5454
}
5555

5656
///////////////////////
57-
// Ignored attributes
57+
// Missing attribute test
5858
///////////////////////
5959
declare_tool_lint! {
6060
#[clippy::version]
61-
pub clippy::IGNORED_ONE,
61+
pub clippy::MISSING_ATTRIBUTE_ONE,
6262
Warn,
63-
"ONE",
63+
"Two",
6464
report_in_external_macro: true
6565
}
6666

67-
declare_lint_pass!(Pass2 => [VALID_ONE, VALID_TWO, VALID_THREE, INVALID_ONE, INVALID_TWO, IGNORED_ONE]);
67+
declare_tool_lint! {
68+
pub clippy::MISSING_ATTRIBUTE_TWO,
69+
Warn,
70+
"Two",
71+
report_in_external_macro: true
72+
}
73+
74+
#[allow(clippy::missing_clippy_version_attribute)]
75+
mod internal_clippy_lints {
76+
declare_tool_lint! {
77+
pub clippy::ALLOW_MISSING_ATTRIBUTE_ONE,
78+
Warn,
79+
"Two",
80+
report_in_external_macro: true
81+
}
82+
}
83+
84+
use crate::internal_clippy_lints::ALLOW_MISSING_ATTRIBUTE_ONE;
85+
declare_lint_pass!(Pass2 => [VALID_ONE, VALID_TWO, VALID_THREE, INVALID_ONE, INVALID_TWO, MISSING_ATTRIBUTE_ONE, MISSING_ATTRIBUTE_TWO, ALLOW_MISSING_ATTRIBUTE_ONE]);
6886

6987
fn main() {}

tests/ui-internal/check_clippy_version_attribute.stderr

+36-1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,40 @@ LL | | }
3434
= help: please use a valid sematic version, see `doc/adding_lints.md`
3535
= note: this error originates in the macro `$crate::declare_tool_lint` (in Nightly builds, run with -Z macro-backtrace for more info)
3636

37-
error: aborting due to 2 previous errors
37+
error: this lint is missing the `clippy::version` attribute or version value
38+
--> $DIR/check_clippy_version_attribute.rs:59:1
39+
|
40+
LL | / declare_tool_lint! {
41+
LL | | #[clippy::version]
42+
LL | | pub clippy::MISSING_ATTRIBUTE_ONE,
43+
LL | | Warn,
44+
LL | | "Two",
45+
LL | | report_in_external_macro: true
46+
LL | | }
47+
| |_^
48+
|
49+
note: the lint level is defined here
50+
--> $DIR/check_clippy_version_attribute.rs:1:9
51+
|
52+
LL | #![deny(clippy::internal)]
53+
| ^^^^^^^^^^^^^^^^
54+
= note: `#[deny(clippy::missing_clippy_version_attribute)]` implied by `#[deny(clippy::internal)]`
55+
= help: please use a `clippy::version` attribute, see `doc/adding_lints.md`
56+
= note: this error originates in the macro `$crate::declare_tool_lint` (in Nightly builds, run with -Z macro-backtrace for more info)
57+
58+
error: this lint is missing the `clippy::version` attribute or version value
59+
--> $DIR/check_clippy_version_attribute.rs:67:1
60+
|
61+
LL | / declare_tool_lint! {
62+
LL | | pub clippy::MISSING_ATTRIBUTE_TWO,
63+
LL | | Warn,
64+
LL | | "Two",
65+
LL | | report_in_external_macro: true
66+
LL | | }
67+
| |_^
68+
|
69+
= help: please use a `clippy::version` attribute, see `doc/adding_lints.md`
70+
= note: this error originates in the macro `$crate::declare_tool_lint` (in Nightly builds, run with -Z macro-backtrace for more info)
71+
72+
error: aborting due to 4 previous errors
3873

tests/ui-internal/collapsible_span_lint_calls.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-rustfix
22
#![deny(clippy::internal)]
3+
#![allow(clippy::missing_clippy_version_attribute)]
34
#![feature(rustc_private)]
45

56
extern crate clippy_utils;

tests/ui-internal/collapsible_span_lint_calls.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-rustfix
22
#![deny(clippy::internal)]
3+
#![allow(clippy::missing_clippy_version_attribute)]
34
#![feature(rustc_private)]
45

56
extern crate clippy_utils;

tests/ui-internal/collapsible_span_lint_calls.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this call is collapsible
2-
--> $DIR/collapsible_span_lint_calls.rs:35:9
2+
--> $DIR/collapsible_span_lint_calls.rs:36:9
33
|
44
LL | / span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| {
55
LL | | db.span_suggestion(expr.span, help_msg, sugg.to_string(), Applicability::MachineApplicable);
@@ -14,31 +14,31 @@ LL | #![deny(clippy::internal)]
1414
= note: `#[deny(clippy::collapsible_span_lint_calls)]` implied by `#[deny(clippy::internal)]`
1515

1616
error: this call is collapsible
17-
--> $DIR/collapsible_span_lint_calls.rs:38:9
17+
--> $DIR/collapsible_span_lint_calls.rs:39:9
1818
|
1919
LL | / span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| {
2020
LL | | db.span_help(expr.span, help_msg);
2121
LL | | });
2222
| |__________^ help: collapse into: `span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, Some(expr.span), help_msg)`
2323

2424
error: this call is collapsible
25-
--> $DIR/collapsible_span_lint_calls.rs:41:9
25+
--> $DIR/collapsible_span_lint_calls.rs:42:9
2626
|
2727
LL | / span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| {
2828
LL | | db.help(help_msg);
2929
LL | | });
3030
| |__________^ help: collapse into: `span_lint_and_help(cx, TEST_LINT, expr.span, lint_msg, None, help_msg)`
3131

3232
error: this call is collspible
33-
--> $DIR/collapsible_span_lint_calls.rs:44:9
33+
--> $DIR/collapsible_span_lint_calls.rs:45:9
3434
|
3535
LL | / span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| {
3636
LL | | db.span_note(expr.span, note_msg);
3737
LL | | });
3838
| |__________^ help: collapse into: `span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, Some(expr.span), note_msg)`
3939

4040
error: this call is collspible
41-
--> $DIR/collapsible_span_lint_calls.rs:47:9
41+
--> $DIR/collapsible_span_lint_calls.rs:48:9
4242
|
4343
LL | / span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| {
4444
LL | | db.note(note_msg);

tests/ui-internal/custom_ice_message.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// normalize-stderr-test: "', .*clippy_lints" -> "', clippy_lints"
55

66
#![deny(clippy::internal)]
7+
#![allow(clippy::missing_clippy_version_attribute)]
78

89
fn it_looks_like_you_are_trying_to_kill_clippy() {}
910

tests/ui-internal/default_lint.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![deny(clippy::internal)]
2+
#![allow(clippy::missing_clippy_version_attribute)]
23
#![feature(rustc_private)]
34

45
#[macro_use]

tests/ui-internal/default_lint.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: the lint `TEST_LINT_DEFAULT` has the default lint description
2-
--> $DIR/default_lint.rs:17:1
2+
--> $DIR/default_lint.rs:18:1
33
|
44
LL | / declare_tool_lint! {
55
LL | | pub clippy::TEST_LINT_DEFAULT,

tests/ui-internal/if_chain_style.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![warn(clippy::if_chain_style)]
2-
#![allow(clippy::no_effect, clippy::nonminimal_bool)]
2+
#![allow(clippy::no_effect, clippy::nonminimal_bool, clippy::missing_clippy_version_attribute)]
33

44
extern crate if_chain;
55

tests/ui-internal/interning_defined_symbol.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-rustfix
22
#![deny(clippy::internal)]
3+
#![allow(clippy::missing_clippy_version_attribute)]
34
#![feature(rustc_private)]
45

56
extern crate rustc_span;

tests/ui-internal/interning_defined_symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-rustfix
22
#![deny(clippy::internal)]
3+
#![allow(clippy::missing_clippy_version_attribute)]
34
#![feature(rustc_private)]
45

56
extern crate rustc_span;

tests/ui-internal/interning_defined_symbol.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: interning a defined symbol
2-
--> $DIR/interning_defined_symbol.rs:17:13
2+
--> $DIR/interning_defined_symbol.rs:18:13
33
|
44
LL | let _ = Symbol::intern("f32");
55
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `rustc_span::sym::f32`
@@ -12,19 +12,19 @@ LL | #![deny(clippy::internal)]
1212
= note: `#[deny(clippy::interning_defined_symbol)]` implied by `#[deny(clippy::internal)]`
1313

1414
error: interning a defined symbol
15-
--> $DIR/interning_defined_symbol.rs:20:13
15+
--> $DIR/interning_defined_symbol.rs:21:13
1616
|
1717
LL | let _ = sym!(f32);
1818
| ^^^^^^^^^ help: try: `rustc_span::sym::f32`
1919

2020
error: interning a defined symbol
21-
--> $DIR/interning_defined_symbol.rs:23:13
21+
--> $DIR/interning_defined_symbol.rs:24:13
2222
|
2323
LL | let _ = Symbol::intern("proc-macro");
2424
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `rustc_span::sym::proc_dash_macro`
2525

2626
error: interning a defined symbol
27-
--> $DIR/interning_defined_symbol.rs:26:13
27+
--> $DIR/interning_defined_symbol.rs:27:13
2828
|
2929
LL | let _ = Symbol::intern("self");
3030
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `rustc_span::symbol::kw::SelfLower`

tests/ui-internal/invalid_paths.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(clippy::internal)]
2+
#![allow(clippy::missing_clippy_version_attribute)]
23

34
mod paths {
45
// Good path

tests/ui-internal/invalid_paths.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error: invalid path
2-
--> $DIR/invalid_paths.rs:17:5
2+
--> $DIR/invalid_paths.rs:18:5
33
|
44
LL | pub const BAD_CRATE_PATH: [&str; 2] = ["bad", "path"];
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::invalid-paths` implied by `-D warnings`
88

99
error: invalid path
10-
--> $DIR/invalid_paths.rs:20:5
10+
--> $DIR/invalid_paths.rs:21:5
1111
|
1212
LL | pub const BAD_MOD_PATH: [&str; 2] = ["std", "xxx"];
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

tests/ui-internal/lint_without_lint_pass.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![deny(clippy::internal)]
2+
#![allow(clippy::missing_clippy_version_attribute)]
23
#![feature(rustc_private)]
34

45
#[macro_use]

tests/ui-internal/lint_without_lint_pass.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: the lint `TEST_LINT` is not added to any `LintPass`
2-
--> $DIR/lint_without_lint_pass.rs:11:1
2+
--> $DIR/lint_without_lint_pass.rs:12:1
33
|
44
LL | / declare_tool_lint! {
55
LL | | pub clippy::TEST_LINT,

tests/ui-internal/match_type_on_diag_item.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![deny(clippy::internal)]
2+
#![allow(clippy::missing_clippy_version_attribute)]
23
#![feature(rustc_private)]
34

45
extern crate clippy_utils;

tests/ui-internal/match_type_on_diag_item.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: usage of `clippy_utils::ty::match_type()` on a type diagnostic item
2-
--> $DIR/match_type_on_diag_item.rs:30:17
2+
--> $DIR/match_type_on_diag_item.rs:31:17
33
|
44
LL | let _ = match_type(cx, ty, &OPTION);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `clippy_utils::ty::is_type_diagnostic_item(cx, ty, sym::Option)`
@@ -12,13 +12,13 @@ LL | #![deny(clippy::internal)]
1212
= note: `#[deny(clippy::match_type_on_diagnostic_item)]` implied by `#[deny(clippy::internal)]`
1313

1414
error: usage of `clippy_utils::ty::match_type()` on a type diagnostic item
15-
--> $DIR/match_type_on_diag_item.rs:31:17
15+
--> $DIR/match_type_on_diag_item.rs:32:17
1616
|
1717
LL | let _ = match_type(cx, ty, &["core", "result", "Result"]);
1818
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `clippy_utils::ty::is_type_diagnostic_item(cx, ty, sym::Result)`
1919

2020
error: usage of `clippy_utils::ty::match_type()` on a type diagnostic item
21-
--> $DIR/match_type_on_diag_item.rs:34:17
21+
--> $DIR/match_type_on_diag_item.rs:35:17
2222
|
2323
LL | let _ = clippy_utils::ty::match_type(cx, ty, rc_path);
2424
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `clippy_utils::ty::is_type_diagnostic_item(cx, ty, sym::Rc)`

tests/ui-internal/outer_expn_data.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// run-rustfix
22

33
#![deny(clippy::internal)]
4+
#![allow(clippy::missing_clippy_version_attribute)]
45
#![feature(rustc_private)]
56

67
extern crate rustc_hir;

tests/ui-internal/outer_expn_data.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// run-rustfix
22

33
#![deny(clippy::internal)]
4+
#![allow(clippy::missing_clippy_version_attribute)]
45
#![feature(rustc_private)]
56

67
extern crate rustc_hir;

tests/ui-internal/outer_expn_data.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: usage of `outer_expn().expn_data()`
2-
--> $DIR/outer_expn_data.rs:24:34
2+
--> $DIR/outer_expn_data.rs:25:34
33
|
44
LL | let _ = expr.span.ctxt().outer_expn().expn_data();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `outer_expn_data()`

tests/ui-internal/unnecessary_symbol_str.fixed

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// run-rustfix
22
#![feature(rustc_private)]
33
#![deny(clippy::internal)]
4-
#![allow(clippy::unnecessary_operation, unused_must_use)]
4+
#![allow(
5+
clippy::unnecessary_operation,
6+
unused_must_use,
7+
clippy::missing_clippy_version_attribute
8+
)]
59

610
extern crate rustc_span;
711

tests/ui-internal/unnecessary_symbol_str.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// run-rustfix
22
#![feature(rustc_private)]
33
#![deny(clippy::internal)]
4-
#![allow(clippy::unnecessary_operation, unused_must_use)]
4+
#![allow(
5+
clippy::unnecessary_operation,
6+
unused_must_use,
7+
clippy::missing_clippy_version_attribute
8+
)]
59

610
extern crate rustc_span;
711

0 commit comments

Comments
 (0)