Skip to content

Commit

Permalink
New internal lint to make clippy::version attribute mandatory
Browse files Browse the repository at this point in the history
  • Loading branch information
xFrednet committed Nov 11, 2021
1 parent 94bc0a1 commit 8c45fd8
Show file tree
Hide file tree
Showing 29 changed files with 130 additions and 32 deletions.
1 change: 1 addition & 0 deletions clippy_lints/src/lib.register_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ store.register_group(true, "clippy::internal", Some("clippy_internal"), vec![
LintId::of(utils::internal_lints::INVALID_PATHS),
LintId::of(utils::internal_lints::LINT_WITHOUT_LINT_PASS),
LintId::of(utils::internal_lints::MATCH_TYPE_ON_DIAGNOSTIC_ITEM),
LintId::of(utils::internal_lints::MISSING_CLIPPY_VERSION_ATTRIBUTE),
LintId::of(utils::internal_lints::OUTER_EXPN_EXPN_DATA),
LintId::of(utils::internal_lints::PRODUCE_ICE),
LintId::of(utils::internal_lints::UNNECESSARY_SYMBOL_STR),
Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/lib.register_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ store.register_lints(&[
#[cfg(feature = "internal-lints")]
utils::internal_lints::MATCH_TYPE_ON_DIAGNOSTIC_ITEM,
#[cfg(feature = "internal-lints")]
utils::internal_lints::MISSING_CLIPPY_VERSION_ATTRIBUTE,
#[cfg(feature = "internal-lints")]
utils::internal_lints::OUTER_EXPN_EXPN_DATA,
#[cfg(feature = "internal-lints")]
utils::internal_lints::PRODUCE_ICE,
Expand Down
4 changes: 4 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ macro_rules! declare_clippy_lint {

#[cfg(feature = "metadata-collector-lint")]
mod deprecated_lints;
#[cfg_attr(
any(feature = "internal-lints", feature = "metadata-collector-lint"),
allow(clippy::missing_clippy_version_attribute)
)]
mod utils;

// begin lints modules, do not remove this comment, it’s used in `update_lints`
Expand Down
20 changes: 19 additions & 1 deletion clippy_lints/src/utils/internal_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,15 @@ declare_clippy_lint! {
"found an invalid `clippy::version` attribute"
}

declare_clippy_lint! {
/// ### What it does
/// Checks for declared clippy lints without the `clippy::version` attribute.
///
pub MISSING_CLIPPY_VERSION_ATTRIBUTE,
internal,
"found clippy lint without `clippy::version` attribute"
}

declare_lint_pass!(ClippyLintsInternal => [CLIPPY_LINTS_INTERNAL]);

impl EarlyLintPass for ClippyLintsInternal {
Expand Down Expand Up @@ -365,7 +374,7 @@ pub struct LintWithoutLintPass {
registered_lints: FxHashSet<Symbol>,
}

impl_lint_pass!(LintWithoutLintPass => [DEFAULT_LINT, LINT_WITHOUT_LINT_PASS, INVALID_CLIPPY_VERSION_ATTRIBUTE]);
impl_lint_pass!(LintWithoutLintPass => [DEFAULT_LINT, LINT_WITHOUT_LINT_PASS, INVALID_CLIPPY_VERSION_ATTRIBUTE, MISSING_CLIPPY_VERSION_ATTRIBUTE]);

impl<'tcx> LateLintPass<'tcx> for LintWithoutLintPass {
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
Expand Down Expand Up @@ -492,6 +501,15 @@ fn check_invalid_clippy_version_attribute(cx: &LateContext<'_>, item: &'_ Item<'
"please use a valid sematic version, see `doc/adding_lints.md`",
);
}
} else {
span_lint_and_help(
cx,
MISSING_CLIPPY_VERSION_ATTRIBUTE,
item.span,
"this lint is missing the `clippy::version` attribute or version value",
None,
"please use a `clippy::version` attribute, see `doc/adding_lints.md`",
);
}
}

Expand Down
26 changes: 22 additions & 4 deletions tests/ui-internal/check_clippy_version_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,34 @@ declare_tool_lint! {
}

///////////////////////
// Ignored attributes
// Missing attribute test
///////////////////////
declare_tool_lint! {
#[clippy::version]
pub clippy::IGNORED_ONE,
pub clippy::MISSING_ATTRIBUTE_ONE,
Warn,
"ONE",
"Two",
report_in_external_macro: true
}

declare_lint_pass!(Pass2 => [VALID_ONE, VALID_TWO, VALID_THREE, INVALID_ONE, INVALID_TWO, IGNORED_ONE]);
declare_tool_lint! {
pub clippy::MISSING_ATTRIBUTE_TWO,
Warn,
"Two",
report_in_external_macro: true
}

#[allow(clippy::missing_clippy_version_attribute)]
mod internal_clippy_lints {
declare_tool_lint! {
pub clippy::ALLOW_MISSING_ATTRIBUTE_ONE,
Warn,
"Two",
report_in_external_macro: true
}
}

use crate::internal_clippy_lints::ALLOW_MISSING_ATTRIBUTE_ONE;
declare_lint_pass!(Pass2 => [VALID_ONE, VALID_TWO, VALID_THREE, INVALID_ONE, INVALID_TWO, MISSING_ATTRIBUTE_ONE, MISSING_ATTRIBUTE_TWO, ALLOW_MISSING_ATTRIBUTE_ONE]);

fn main() {}
37 changes: 36 additions & 1 deletion tests/ui-internal/check_clippy_version_attribute.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,40 @@ LL | | }
= help: please use a valid sematic version, see `doc/adding_lints.md`
= note: this error originates in the macro `$crate::declare_tool_lint` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors
error: this lint is missing the `clippy::version` attribute or version value
--> $DIR/check_clippy_version_attribute.rs:59:1
|
LL | / declare_tool_lint! {
LL | | #[clippy::version]
LL | | pub clippy::MISSING_ATTRIBUTE_ONE,
LL | | Warn,
LL | | "Two",
LL | | report_in_external_macro: true
LL | | }
| |_^
|
note: the lint level is defined here
--> $DIR/check_clippy_version_attribute.rs:1:9
|
LL | #![deny(clippy::internal)]
| ^^^^^^^^^^^^^^^^
= note: `#[deny(clippy::missing_clippy_version_attribute)]` implied by `#[deny(clippy::internal)]`
= help: please use a `clippy::version` attribute, see `doc/adding_lints.md`
= note: this error originates in the macro `$crate::declare_tool_lint` (in Nightly builds, run with -Z macro-backtrace for more info)

error: this lint is missing the `clippy::version` attribute or version value
--> $DIR/check_clippy_version_attribute.rs:67:1
|
LL | / declare_tool_lint! {
LL | | pub clippy::MISSING_ATTRIBUTE_TWO,
LL | | Warn,
LL | | "Two",
LL | | report_in_external_macro: true
LL | | }
| |_^
|
= help: please use a `clippy::version` attribute, see `doc/adding_lints.md`
= note: this error originates in the macro `$crate::declare_tool_lint` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 4 previous errors

1 change: 1 addition & 0 deletions tests/ui-internal/collapsible_span_lint_calls.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// run-rustfix
#![deny(clippy::internal)]
#![allow(clippy::missing_clippy_version_attribute)]
#![feature(rustc_private)]

extern crate clippy_utils;
Expand Down
1 change: 1 addition & 0 deletions tests/ui-internal/collapsible_span_lint_calls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// run-rustfix
#![deny(clippy::internal)]
#![allow(clippy::missing_clippy_version_attribute)]
#![feature(rustc_private)]

extern crate clippy_utils;
Expand Down
10 changes: 5 additions & 5 deletions tests/ui-internal/collapsible_span_lint_calls.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this call is collapsible
--> $DIR/collapsible_span_lint_calls.rs:35:9
--> $DIR/collapsible_span_lint_calls.rs:36:9
|
LL | / span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| {
LL | | db.span_suggestion(expr.span, help_msg, sugg.to_string(), Applicability::MachineApplicable);
Expand All @@ -14,31 +14,31 @@ LL | #![deny(clippy::internal)]
= note: `#[deny(clippy::collapsible_span_lint_calls)]` implied by `#[deny(clippy::internal)]`

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

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

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

error: this call is collspible
--> $DIR/collapsible_span_lint_calls.rs:47:9
--> $DIR/collapsible_span_lint_calls.rs:48:9
|
LL | / span_lint_and_then(cx, TEST_LINT, expr.span, lint_msg, |db| {
LL | | db.note(note_msg);
Expand Down
1 change: 1 addition & 0 deletions tests/ui-internal/custom_ice_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// normalize-stderr-test: "', .*clippy_lints" -> "', clippy_lints"

#![deny(clippy::internal)]
#![allow(clippy::missing_clippy_version_attribute)]

fn it_looks_like_you_are_trying_to_kill_clippy() {}

Expand Down
1 change: 1 addition & 0 deletions tests/ui-internal/default_lint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![deny(clippy::internal)]
#![allow(clippy::missing_clippy_version_attribute)]
#![feature(rustc_private)]

#[macro_use]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-internal/default_lint.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: the lint `TEST_LINT_DEFAULT` has the default lint description
--> $DIR/default_lint.rs:17:1
--> $DIR/default_lint.rs:18:1
|
LL | / declare_tool_lint! {
LL | | pub clippy::TEST_LINT_DEFAULT,
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-internal/if_chain_style.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![warn(clippy::if_chain_style)]
#![allow(clippy::no_effect, clippy::nonminimal_bool)]
#![allow(clippy::no_effect, clippy::nonminimal_bool, clippy::missing_clippy_version_attribute)]

extern crate if_chain;

Expand Down
1 change: 1 addition & 0 deletions tests/ui-internal/interning_defined_symbol.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// run-rustfix
#![deny(clippy::internal)]
#![allow(clippy::missing_clippy_version_attribute)]
#![feature(rustc_private)]

extern crate rustc_span;
Expand Down
1 change: 1 addition & 0 deletions tests/ui-internal/interning_defined_symbol.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// run-rustfix
#![deny(clippy::internal)]
#![allow(clippy::missing_clippy_version_attribute)]
#![feature(rustc_private)]

extern crate rustc_span;
Expand Down
8 changes: 4 additions & 4 deletions tests/ui-internal/interning_defined_symbol.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: interning a defined symbol
--> $DIR/interning_defined_symbol.rs:17:13
--> $DIR/interning_defined_symbol.rs:18:13
|
LL | let _ = Symbol::intern("f32");
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `rustc_span::sym::f32`
Expand All @@ -12,19 +12,19 @@ LL | #![deny(clippy::internal)]
= note: `#[deny(clippy::interning_defined_symbol)]` implied by `#[deny(clippy::internal)]`

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

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

error: interning a defined symbol
--> $DIR/interning_defined_symbol.rs:26:13
--> $DIR/interning_defined_symbol.rs:27:13
|
LL | let _ = Symbol::intern("self");
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `rustc_span::symbol::kw::SelfLower`
Expand Down
1 change: 1 addition & 0 deletions tests/ui-internal/invalid_paths.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![warn(clippy::internal)]
#![allow(clippy::missing_clippy_version_attribute)]

mod paths {
// Good path
Expand Down
4 changes: 2 additions & 2 deletions tests/ui-internal/invalid_paths.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error: invalid path
--> $DIR/invalid_paths.rs:17:5
--> $DIR/invalid_paths.rs:18:5
|
LL | pub const BAD_CRATE_PATH: [&str; 2] = ["bad", "path"];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::invalid-paths` implied by `-D warnings`

error: invalid path
--> $DIR/invalid_paths.rs:20:5
--> $DIR/invalid_paths.rs:21:5
|
LL | pub const BAD_MOD_PATH: [&str; 2] = ["std", "xxx"];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions tests/ui-internal/lint_without_lint_pass.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![deny(clippy::internal)]
#![allow(clippy::missing_clippy_version_attribute)]
#![feature(rustc_private)]

#[macro_use]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-internal/lint_without_lint_pass.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: the lint `TEST_LINT` is not added to any `LintPass`
--> $DIR/lint_without_lint_pass.rs:11:1
--> $DIR/lint_without_lint_pass.rs:12:1
|
LL | / declare_tool_lint! {
LL | | pub clippy::TEST_LINT,
Expand Down
1 change: 1 addition & 0 deletions tests/ui-internal/match_type_on_diag_item.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![deny(clippy::internal)]
#![allow(clippy::missing_clippy_version_attribute)]
#![feature(rustc_private)]

extern crate clippy_utils;
Expand Down
6 changes: 3 additions & 3 deletions tests/ui-internal/match_type_on_diag_item.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: usage of `clippy_utils::ty::match_type()` on a type diagnostic item
--> $DIR/match_type_on_diag_item.rs:30:17
--> $DIR/match_type_on_diag_item.rs:31:17
|
LL | let _ = match_type(cx, ty, &OPTION);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `clippy_utils::ty::is_type_diagnostic_item(cx, ty, sym::Option)`
Expand All @@ -12,13 +12,13 @@ LL | #![deny(clippy::internal)]
= note: `#[deny(clippy::match_type_on_diagnostic_item)]` implied by `#[deny(clippy::internal)]`

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

error: usage of `clippy_utils::ty::match_type()` on a type diagnostic item
--> $DIR/match_type_on_diag_item.rs:34:17
--> $DIR/match_type_on_diag_item.rs:35:17
|
LL | let _ = clippy_utils::ty::match_type(cx, ty, rc_path);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `clippy_utils::ty::is_type_diagnostic_item(cx, ty, sym::Rc)`
Expand Down
1 change: 1 addition & 0 deletions tests/ui-internal/outer_expn_data.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// run-rustfix

#![deny(clippy::internal)]
#![allow(clippy::missing_clippy_version_attribute)]
#![feature(rustc_private)]

extern crate rustc_hir;
Expand Down
1 change: 1 addition & 0 deletions tests/ui-internal/outer_expn_data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// run-rustfix

#![deny(clippy::internal)]
#![allow(clippy::missing_clippy_version_attribute)]
#![feature(rustc_private)]

extern crate rustc_hir;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-internal/outer_expn_data.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: usage of `outer_expn().expn_data()`
--> $DIR/outer_expn_data.rs:24:34
--> $DIR/outer_expn_data.rs:25:34
|
LL | let _ = expr.span.ctxt().outer_expn().expn_data();
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `outer_expn_data()`
Expand Down
6 changes: 5 additions & 1 deletion tests/ui-internal/unnecessary_symbol_str.fixed
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// run-rustfix
#![feature(rustc_private)]
#![deny(clippy::internal)]
#![allow(clippy::unnecessary_operation, unused_must_use)]
#![allow(
clippy::unnecessary_operation,
unused_must_use,
clippy::missing_clippy_version_attribute
)]

extern crate rustc_span;

Expand Down
6 changes: 5 additions & 1 deletion tests/ui-internal/unnecessary_symbol_str.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// run-rustfix
#![feature(rustc_private)]
#![deny(clippy::internal)]
#![allow(clippy::unnecessary_operation, unused_must_use)]
#![allow(
clippy::unnecessary_operation,
unused_must_use,
clippy::missing_clippy_version_attribute
)]

extern crate rustc_span;

Expand Down

0 comments on commit 8c45fd8

Please sign in to comment.