From 23bd7ac5ee5956fc9b0681a8de1e4abe4203d5ea Mon Sep 17 00:00:00 2001 From: binarycat Date: Tue, 4 Jun 2024 11:55:05 -0400 Subject: [PATCH 1/6] Add wildcard_let lint Fixes #4090 --- clippy_lints/src/declared_lints.rs | 1 + clippy_lints/src/lib.rs | 2 ++ clippy_lints/src/wildcard_let.rs | 39 ++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 clippy_lints/src/wildcard_let.rs diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index a4948e36fbe2..dfb6367bfeaa 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -749,6 +749,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::visibility::PUB_WITH_SHORTHAND_INFO, crate::wildcard_imports::ENUM_GLOB_USE_INFO, crate::wildcard_imports::WILDCARD_IMPORTS_INFO, + crate::wildcard_let::WILDCARD_LET_INFO, crate::write::PRINTLN_EMPTY_STRING_INFO, crate::write::PRINT_LITERAL_INFO, crate::write::PRINT_STDERR_INFO, diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 3328d642bd85..750e17ea10da 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -374,6 +374,7 @@ mod vec; mod vec_init_then_push; mod visibility; mod wildcard_imports; +mod wildcard_let; mod write; mod zero_div_zero; mod zero_repeat_side_effects; @@ -1165,6 +1166,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) { ..Default::default() }) }); + store.register_early_pass(|| Box::new(wildcard_let::WildcardLet{})); // add lints here, do not remove this comment, it's used in `new_lint` } diff --git a/clippy_lints/src/wildcard_let.rs b/clippy_lints/src/wildcard_let.rs new file mode 100644 index 000000000000..a71126b61efe --- /dev/null +++ b/clippy_lints/src/wildcard_let.rs @@ -0,0 +1,39 @@ +use clippy_config::msrvs::{self, Msrv}; +use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::diagnostics::span_lint_and_help; +use clippy_utils::source::{trim_span, walk_span_to_context}; +use rustc_ast::ast::{Expr, ExprKind, LitKind, Pat, PatKind, RangeEnd, RangeLimits}; +use rustc_errors::Applicability; +use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; +use rustc_middle::lint::in_external_macro; +use rustc_session::impl_lint_pass; +use rustc_span::Span; +use rustc_ast::ast::Local; + +declare_clippy_lint! { + /// ### What it does + /// check for `let _ = ...`. + /// + /// this may be used by crates that with to force `#[must_use]` + /// values to actually used, along with `#[forbid(unused_must_use)]`. + pub WILDCARD_LET, + restriction, + "wildcard let" +} +impl_lint_pass!(WildcardLet => [WILDCARD_LET]); + +pub struct WildcardLet {} + +impl EarlyLintPass for WildcardLet { + fn check_local(&mut self, cx: &EarlyContext<'_>, local: &Local) { + let span = local.pat.span; + if in_external_macro(cx.sess(), span) { + return + } + if let PatKind::Wild = local.pat.kind { + span_lint_and_help( + cx, WILDCARD_LET, span, "wildcard let", None, + "remove this binding or handle the value"); + } + } +} From ad2f078e3899b93f2ee6f9e183f26257302dac26 Mon Sep 17 00:00:00 2001 From: binarycat Date: Fri, 7 Jun 2024 21:11:39 -0400 Subject: [PATCH 2/6] fix compiler warnings in wildcard_let.rs --- clippy_lints/src/wildcard_let.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/clippy_lints/src/wildcard_let.rs b/clippy_lints/src/wildcard_let.rs index a71126b61efe..7336fa159d97 100644 --- a/clippy_lints/src/wildcard_let.rs +++ b/clippy_lints/src/wildcard_let.rs @@ -1,13 +1,8 @@ -use clippy_config::msrvs::{self, Msrv}; -use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::diagnostics::span_lint_and_help; -use clippy_utils::source::{trim_span, walk_span_to_context}; -use rustc_ast::ast::{Expr, ExprKind, LitKind, Pat, PatKind, RangeEnd, RangeLimits}; -use rustc_errors::Applicability; +use rustc_ast::ast::PatKind; use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; use rustc_middle::lint::in_external_macro; use rustc_session::impl_lint_pass; -use rustc_span::Span; use rustc_ast::ast::Local; declare_clippy_lint! { From 6cb4285a89d7c437ce950a20b588aa5159bee70c Mon Sep 17 00:00:00 2001 From: binarycat Date: Fri, 7 Jun 2024 21:13:55 -0400 Subject: [PATCH 3/6] add ui test for wildcard_let lint --- tests/ui/wildcard_let.rs | 5 +++++ tests/ui/wildcard_let.stderr | 15 +++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 tests/ui/wildcard_let.rs create mode 100644 tests/ui/wildcard_let.stderr diff --git a/tests/ui/wildcard_let.rs b/tests/ui/wildcard_let.rs new file mode 100644 index 000000000000..d8a56f3c09f3 --- /dev/null +++ b/tests/ui/wildcard_let.rs @@ -0,0 +1,5 @@ +#![forbid(clippy::wildcard_let)] + +fn main() { + let _ = 1; +} diff --git a/tests/ui/wildcard_let.stderr b/tests/ui/wildcard_let.stderr new file mode 100644 index 000000000000..3d9ad8655182 --- /dev/null +++ b/tests/ui/wildcard_let.stderr @@ -0,0 +1,15 @@ +error: wildcard let + --> tests/ui/wildcard_let.rs:4:6 + | +LL | let _ = 1; + | ^ + | + = help: remove this binding or handle the value +note: the lint level is defined here + --> tests/ui/wildcard_let.rs:1:11 + | +LL | #![forbid(clippy::wildcard_let)] + | ^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 1 previous error + From 1b6a1f015c14b3422ae6795b4bdc87a047e01124 Mon Sep 17 00:00:00 2001 From: binarycat Date: Fri, 7 Jun 2024 21:20:34 -0400 Subject: [PATCH 4/6] autoformat wildcard_let --- clippy_lints/src/lib.rs | 2 +- clippy_lints/src/wildcard_let.rs | 46 +++++++++++++++++--------------- tests/ui/wildcard_let.rs | 2 +- tests/ui/wildcard_let.stderr | 2 +- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 750e17ea10da..a55a6d701213 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -1166,7 +1166,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) { ..Default::default() }) }); - store.register_early_pass(|| Box::new(wildcard_let::WildcardLet{})); + store.register_early_pass(|| Box::new(wildcard_let::WildcardLet {})); // add lints here, do not remove this comment, it's used in `new_lint` } diff --git a/clippy_lints/src/wildcard_let.rs b/clippy_lints/src/wildcard_let.rs index 7336fa159d97..2f4696a9dfe7 100644 --- a/clippy_lints/src/wildcard_let.rs +++ b/clippy_lints/src/wildcard_let.rs @@ -1,34 +1,38 @@ use clippy_utils::diagnostics::span_lint_and_help; -use rustc_ast::ast::PatKind; +use rustc_ast::ast::{Local, PatKind}; use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; use rustc_middle::lint::in_external_macro; use rustc_session::impl_lint_pass; -use rustc_ast::ast::Local; declare_clippy_lint! { - /// ### What it does - /// check for `let _ = ...`. - /// - /// this may be used by crates that with to force `#[must_use]` - /// values to actually used, along with `#[forbid(unused_must_use)]`. - pub WILDCARD_LET, - restriction, - "wildcard let" + /// ### What it does + /// check for `let _ = ...`. + /// + /// this may be used by crates that with to force `#[must_use]` + /// values to actually used, along with `#[forbid(unused_must_use)]`. + pub WILDCARD_LET, + restriction, + "wildcard let" } impl_lint_pass!(WildcardLet => [WILDCARD_LET]); pub struct WildcardLet {} impl EarlyLintPass for WildcardLet { - fn check_local(&mut self, cx: &EarlyContext<'_>, local: &Local) { - let span = local.pat.span; - if in_external_macro(cx.sess(), span) { - return - } - if let PatKind::Wild = local.pat.kind { - span_lint_and_help( - cx, WILDCARD_LET, span, "wildcard let", None, - "remove this binding or handle the value"); - } - } + fn check_local(&mut self, cx: &EarlyContext<'_>, local: &Local) { + let span = local.pat.span; + if in_external_macro(cx.sess(), span) { + return; + } + if let PatKind::Wild = local.pat.kind { + span_lint_and_help( + cx, + WILDCARD_LET, + span, + "wildcard let", + None, + "remove this binding or handle the value", + ); + } + } } diff --git a/tests/ui/wildcard_let.rs b/tests/ui/wildcard_let.rs index d8a56f3c09f3..eb0ed5134585 100644 --- a/tests/ui/wildcard_let.rs +++ b/tests/ui/wildcard_let.rs @@ -1,5 +1,5 @@ #![forbid(clippy::wildcard_let)] fn main() { - let _ = 1; + let _ = 1; } diff --git a/tests/ui/wildcard_let.stderr b/tests/ui/wildcard_let.stderr index 3d9ad8655182..0c76d15e7484 100644 --- a/tests/ui/wildcard_let.stderr +++ b/tests/ui/wildcard_let.stderr @@ -1,5 +1,5 @@ error: wildcard let - --> tests/ui/wildcard_let.rs:4:6 + --> tests/ui/wildcard_let.rs:4:9 | LL | let _ = 1; | ^ From 537174cdb76ec72684cefe2f38935480247ce3fd Mon Sep 17 00:00:00 2001 From: binarycat Date: Fri, 7 Jun 2024 21:25:11 -0400 Subject: [PATCH 5/6] add wildcard_let link to changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0f511742fa1..3c311879cb17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5920,6 +5920,7 @@ Released 2018-09-13 [`wildcard_enum_match_arm`]: https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_enum_match_arm [`wildcard_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_imports [`wildcard_in_or_patterns`]: https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_in_or_patterns +[`wildcard_let`]: https://rust-lang.github.io/rust-clippy/master/index.html#wildcard_let [`write_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#write_literal [`write_with_newline`]: https://rust-lang.github.io/rust-clippy/master/index.html#write_with_newline [`writeln_empty_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#writeln_empty_string From 67415414d270f6fb0fd1e76ce7ba47efba957dc5 Mon Sep 17 00:00:00 2001 From: binarycat Date: Fri, 7 Jun 2024 21:33:35 -0400 Subject: [PATCH 6/6] add clippy::version for wildcard_let --- clippy_lints/src/wildcard_let.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/clippy_lints/src/wildcard_let.rs b/clippy_lints/src/wildcard_let.rs index 2f4696a9dfe7..a0ef462aa983 100644 --- a/clippy_lints/src/wildcard_let.rs +++ b/clippy_lints/src/wildcard_let.rs @@ -10,6 +10,7 @@ declare_clippy_lint! { /// /// this may be used by crates that with to force `#[must_use]` /// values to actually used, along with `#[forbid(unused_must_use)]`. + #[clippy::version = "1.80.0"] pub WILDCARD_LET, restriction, "wildcard let"